Outlets
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 943 Accepted Submission(s): 424
Problem Description
In China, foreign brand commodities are often much more expensive than abroad. The main reason is that we Chinese people tend to think foreign things are better and we are willing to pay much for them. The typical example is, on the United Airline flight, they give you Haagendazs ice cream for free, but in China, you will pay $10 to buy just a little cup.
So when we Chinese go abroad, one of our most favorite activities is shopping in outlets. Some people buy tens of famous brand shoes and bags one time. In Las Vegas, the existing outlets can't match the demand of Chinese. So they want to build a new outlets in the desert. The new outlets consists of many stores. All stores are connected by roads. They want to minimize the total road length. The owner of the outlets just hired a data mining expert, and the expert told him that Nike store and Apple store must be directly connected by a road. Now please help him figure out how to minimize the total road length under this condition. A store can be considered as a point and a road is a line segment connecting two stores.
So when we Chinese go abroad, one of our most favorite activities is shopping in outlets. Some people buy tens of famous brand shoes and bags one time. In Las Vegas, the existing outlets can't match the demand of Chinese. So they want to build a new outlets in the desert. The new outlets consists of many stores. All stores are connected by roads. They want to minimize the total road length. The owner of the outlets just hired a data mining expert, and the expert told him that Nike store and Apple store must be directly connected by a road. Now please help him figure out how to minimize the total road length under this condition. A store can be considered as a point and a road is a line segment connecting two stores.
Input
There are several test cases. For each test case: The first line is an integer N( 3 <= N <= 50) , meaning there are N stores in the outlets. These N stores are numbered from 1 to N. The second line contains two integers p and q, indicating that the No. p store is a Nike store and the No. q store is an Apple store. Then N lines follow. The i-th line describes the position of the i-th store. The store position is represented by two integers x,y( -100<= x,y <= 100) , meaning that the coordinate of the store is (x,y). These N stores are all located at different place. The input ends by N = 0.
Output
For each test case, print the minimum total road length. The result should be rounded to 2 digits after decimal point.
Sample Input
4 2 3 0 0 1 0 0 -1 1 -1 0
Sample Output
3.41
Source
终于把一道区域赛的水题给A了,现在在学最小生成树的prim算法,真心表示没有kruskal好写,但毕竟是图论的一个算法,说不准什么时候有道卡数据的题优化一下可以过呢!
这道题首先说了nike 和apple必须直接相连,然后这个就相当于prim算法的第一步已经预先执行过了,根据prim算法的执行过程,下一步是找最段的距离,因此先在执行一下a,端点,再找b端点的最小值,取二者较小即可;
附加数据一组:
5
2 3
0 0
1 1
1 -1
-1 -1
-1 1
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,a,b,vis[210];
double mp[105][105];
const int INF = 0x7fffffff;
struct Point
{
int x,y;
}pt[105];
double prim()
{
int cnt;
double ans,lowcost[105],min;
vis[a]=1,vis[b]=1;
ans += mp[a][b];
for(int i=1;i<=n;i++)
lowcost[i]=mp[a][i];
for(int j=1;j<=n;j++)
{
if(lowcost[j]>mp[b][j])
lowcost[j]=mp[b][j];
}
for(int i=1;i<n;i++)
{
min=1.0*INF,cnt=-1;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&min>lowcost[j])
{
min=lowcost[j];
cnt=j;
}
}
//printf("_cnt = %d,min=%d\n",cnt,min);
if(cnt == -1) return ans;
ans+=min;
vis[cnt]=1;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&lowcost[j]>mp[cnt][j])
lowcost[j]=mp[cnt][j];
}
}
printf("%.2lf\n",ans);
}
int main()
{
while(scanf("%d",&n),n)
{
scanf("%d%d",&a,&b);
for(int i=1;i<=n;i++)
scanf("%d%d",&pt[i].x,&pt[i].y);
for(int i=1;i<=n;i++)
{
mp[i][i]=INF;
for(int j=i+1;j<=n;j++)
{
mp[i][j]=mp[j][i]=(double)sqrt(1.0*(pt[i].x -pt[j].x)*(pt[i].x-pt[j].x )+1.0*(pt[i].y-pt[j].y )*(pt[i].y -pt[j].y ));
//printf("mp[%d][%d]=%.2lf ",i,j,mp[i][j]);
}
//printf("\n");
}
//
memset(vis,0,sizeof(vis));
printf("%.2lf\n",prim());
}
return 0;
}