HDU 4463 Outlets【Prim】
2016-08-09 17:53
381 查看
Outlets
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 44 Accepted Submission(s) : 31
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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, theygive 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 storeis 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
2012 Asia Hangzhou Regional ContestPrim:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<climits> #include<string> #include<algorithm> #include<queue> #include<stack> #include<set> #include<map> using namespace std; #define rep(i,j,k)for(i=j;i<k;i++) #define per(i,j,k)for(i=j;i>k;i++) #define MS(x,y)memset(x,y,sizeof(x)) #define MC(x,y)memcpy(x,y,sizeof(y)) typedef long long LL; const int INF=0x7FFFFFFF; const int low(int x){return x&-x;} const int M=100+1; double mp[M][M]; int vis[M]; double v[M]; int flag[M][M],mark[M]; int i,j,k,n,m,t; double x[M],y[M]; int a,b; void init() { MS(vis,0); MS(x,0),MS(y,0); } void Prim() { rep(i,1,n+1) v[i]=mp[1][i]; double sum=0;vis[1]=1; rep(i,1,n){ double mx=INF; int mk=1; rep(j,1,n+1) if(!vis[j]&&v[j]<mx) mx=v[j],mk=j; sum+=mx; vis[mk]=1; rep(j,1,n+1) if(!vis[j]&&mp[mk][j]<v[j]) v[j]=mp[mk][j]; } double u=sqrt(pow(abs(x[a]-x[b]),2)+pow(abs(y[a]-y[b]),2)); printf("%.2lf\n",sum+u); return; } int main() { while(~scanf("%d",&n),n) { init(); scanf("%d%d",&a,&b); rep(i,1,n+1) scanf("%lf%lf",&x[i],&y[i]); rep(i,1,n+1) rep(j,1,n+1){ double d=sqrt(pow(abs(x[i]-x[j]),2)+pow(abs(y[i]-y[j]),2)); mp[i][j]=mp[j][i]=d; } mp[a][b]=mp[b][a]=0; Prim(); } return 0; }
相关文章推荐
- hdu 4463 Outlets Prim 次小生成树 简单题
- hdu 4463 Outlets(prim)
- 【HDU 4463 Outlets】最小生成树(prim,kruscal都可)
- hdu 4463 Outlets(最小生成树)
- HDU 4463 Outlets(一条边固定的最小生成树)
- hdu 4463 Outlets
- hdu 4463Outlets题解(Dijkstra)
- hdu 4463 Outlets(最小生成树,kruskal,前向星)
- HDU 4463 Outlets
- HDU 4463 Outlets
- HDU 4463 Outlets (枚举)
- HDU-4463 Outlets 最小生成树
- hdu 4463 Outlets
- HDU 4463 Outlets 最小生成树
- hdu 4463 最小生成树 prim
- HDU-4463-Outlets
- hdu 4463 Outlets 解题报告
- HDU 4463 Outlets 最小生成树Kr~
- hdu 4463 Outlets
- HDU 4463 Outlets