码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1301 Jungle Roads (Prim)

时间:2014-08-23 16:36:41      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   for   ar   div   

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301

 

 

依旧Prim............不多说了

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 
 5 using namespace std;
 6 
 7 #define MAX ‘Z‘+5
 8 #define inf 99999999
 9 
10 int map[MAX][MAX],node[MAX],vis[MAX],n,path;
11 
12 void Prim()
13 {
14     int ans=A;
15     node[ans]=0;
16     vis[ans]=1;
17     for(int k=A;k<(A+n);k++)
18     {
19         //cout<<k<<" "<<n<<endl;
20         int minn=inf;
21         for(int i=A;i<(A+n);i++)
22         if(!vis[i]&&minn>node[i])
23         {
24             minn=node[i];
25             ans=i;
26         }
27         //printf("%c %d\n",ans,minn);
28         vis[ans]=1;
29         path+=node[ans];
30         //cout<<"      "<<path<<endl;
31 
32         for(int i=A;i<(A+n);i++)
33         if(!vis[i]&&node[i]>map[ans][i])
34         node[i]=map[ans][i];
35     }
36 }
37 
38 int main()
39 {
40     while(cin>>n&&n)
41     {
42         for(int i=A;i<=Z;i++)
43         {
44             node[i]=inf;
45             vis[i]=0;
46             for(int j=A;j<=Z;j++)
47             map[i][j]=inf;
48         }
49         int N=n-1;
50         char a,b;
51         int t,dist;
52         while(N--)
53         {
54             cin>>a>>t;
55             while(t--)
56             {
57                 cin>>b>>dist;
58                 if(map[a][b]>dist)
59                 map[a][b]=map[b][a]=dist;
60             }
61         }
62         path=0;
63         Prim();
64         cout<<path<<endl;
65     }
66     return 0;
67 }

 

hdu1301 Jungle Roads (Prim)

标签:style   blog   http   color   os   io   for   ar   div   

原文地址:http://www.cnblogs.com/mis-xiao/p/3931357.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!