http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1004&ojid=2&cid=963&hide=0
2011-11-03 19:45
375 查看
#include<iostream> #include<cmath> #define N 101 #define M 99999999.0 #define FOR(i,s,t) for(int i=(s);i<=(t);++i) #include<cstdio> using namespace std; double map ; double dist ; bool visit ; struct point{ double x,y; }aa ; int n,tot; inline double distan(point& a,point& b ) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } void prim() { FOR(i,1,n) { dist[i]=M; visit[i]=true; } int now=1; dist[now]=0.0; visit[now]=false; double ans=0.0; FOR(i,1,n) { ans+=dist[now]; FOR(j,1,n) if(visit[j]&&dist[j]>map[now][j]) dist[j]=map[now][j]; double minx=M; FOR(j,1,n) if(visit[j]&&minx>dist[j]) minx=dist[now=j]; visit[now]=false; } if(tot) cout<<endl; 这个地方最容易出错,,,,,, printf("Case #%d:\nThe minimal distance is: %.2lf\n",++tot,ans); } int main() { int tot=0; while(~scanf("%d",&n)!=EOF&&n) { for(int i=1;i<=n;i++) for(int j=1;j<=n;++j) map[i][j]=M; for(int i=1;i<=n;i++) ~scanf("%lf%lf",&aa[i].x,&aa[i].y); for(int i=1;i<n;++i) for(int j=i+1;j<=n;++j) map[i][j]=map[j][i]=distan(aa[i],aa[j]); prim(); } return 0; }
相关文章推荐
- http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1004&ojid=1&cid=6866&hide=1&problem=Pro
- http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?cid=791&pid=1001&ojid=0
- http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?cid=963&pid=1019&ojid=1
- http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?&&次小生成树
- http://acm.hdu.edu.cn/showproblem.php?pid=3584
- http://acm.hdu.edu.cn/showproblem.php?pid=4477
- 最短路径问题 多加了一个费用 http://acm.hdu.edu.cn/showproblem.php?pid=3790
- 寒冰王座(http://acm.hdu.edu.cn/showproblem.php?pid=1248)
- http://acm.hdu.edu.cn/showproblem.php?pid=1211 (水)
- http://acm.hdu.edu.cn/showproblem.php?pid=2136
- http://acm.hdu.edu.cn/showproblem.php?pid=1452 因子求和
- http://acm.hdu.edu.cn/showproblem.php?pid=1874
- 完数 &&http://acm.hdu.edu.cn/showproblem.php?pid=1406
- http://acm.hdu.edu.cn/showproblem.php?pid=1055&&Color a Tree
- 最小路径覆盖&&http://acm.hdu.edu.cn/showproblem.php?pid=4160
- 饭卡 (背包01 一维数组) http://acm.hdu.edu.cn/showproblem.php?pid=2546
- http://acm.hdu.edu.cn/showproblem.php?pid=2612
- 换角度思考:http://acm.hdu.edu.cn/showproblem.php?pid=3602
- http://acm.hdu.edu.cn/showproblem.php?pid=2842
- http://acm.hdu.edu.cn/showproblem.php?pid=2604