hdu4463Outlets

来源:互联网 发布:世界秩序 知乎 编辑:程序博客网 时间:2024/06/05 09:20

Outlets

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2700    Accepted Submission(s): 1259



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.
 

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
42 30 01 00 -1 1 -10
 

Sample Output
3.41
 

Source
2012 Asia Hangzhou Regional Contest

题目意思很简单,就是叫你求固定一条边之后的最小生成树
有两种写法,一是Kruskal,没有什么坑点
而是prime,dis初始化时应该是到每个点到两个点较小的距离
#include <map>#include <set>#include <stack>#include <queue>#include <cmath>#include <ctime>#include <vector>#include <cstdio>#include <cctype>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define inf -0x3f3f3f3f#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define mem0(a) memset(a,0,sizeof(a))#define mem1(a) memset(a,-1,sizeof(a))#define mem(a, b) memset(a, b, sizeof(a))typedef long long ll;const int maxn=110;double mp[maxn][maxn];int vis[maxn];double dis[maxn];struct node{    int x,y;}line[maxn];void prime(int n,int s,int t){    double ans=0;    mem0(vis);    vis[s]=1,vis[t]=1;    for(int i=1;i<=n;i++)        dis[i]=min(mp[s][i],mp[t][i]);    for(int i=1;i<n-1;i++){        double minv=INF;        int p=-1;        for(int j=1;j<=n;j++){            if(!vis[j]&&minv>dis[j]){                minv=dis[j];                p=j;            }        }        if(minv==INF)            return ;        ans+=minv;        vis[p]=1;        for(int j=1;j<=n;j++)            if(!vis[j]&&dis[j]>mp[p][j])                dis[j]=mp[p][j];    }    printf("%.3lf\n",ans);}int main(){    int n,s,t;    while(scanf("%d",&n)!=EOF){        if(n==0)            break;        scanf("%d%d",&s,&t);        for(int i=1;i<=n;i++){            scanf("%d%d",&line[i].x,line[i].y);        }        for(int i=1;i<=n;i++)            for(int j=1;j<=n;j++)                mp[i][j]=sqrt((double)((line[i].x-line[j].x)*(line[i].x-line[j].x)+(line[i].y-line[j].y)*(line[i].y-line[j].y)));        prime(n,s,t);    }    return 0;}


 
0 0
原创粉丝点击