poj2485(Highways)

题目大意:

        建造公路,将几个城市联系起来,给你每个城市之间公路建造的费用,让你找出将每个城市联系起来所使用最小的费用。然后输出建造公路中花费最多的那条公路的费用。

 

解题思路:

        根据测试数据建图,将各个城市的公路费用作为cost值,然后找出最小生成树,然后再找出花费最多的那条公路的费用即可。简单的dijstra。

 

代码:

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <sstream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cstdio>
 7 #include <string>
 8 #include <bitset>
 9 #include <vector>
10 #include <queue>
11 #include <stack>
12 #include <cmath>
13 #include <list>
14 //#include <map>
15 #include <set>
16 using namespace std;
17 /***************************************/
18 #define ll long long
19 #define int64 __int64
20 /***************************************/
21 const int INF = 0x7f7f7f7f;
22 const double eps = 1e-8;
23 const double PIE=acos(-1.0);
24 const int d1x[]= {0,-1,0,1};
25 const int d1y[]= {-1,0,1,0};
26 const int d2x[]= {0,-1,0,1};
27 const int d2y[]= {1,0,-1,0};
28 const int fx[]= {-1,-1,-1,0,0,1,1,1};
29 const int fy[]= {-1,0,1,-1,1,-1,0,1};
30 /***************************************/
31 void openfile()
32 {
33     freopen("data.in","rb",stdin);
34     freopen("data.out","wb",stdout);
35 }
36 /**********************华丽丽的分割线,以上为模板部分*****************/
37 #define N 600
38 int map[N][N];
39 int lowcost[N];
40 int vis[N];
41 int n;
42 int ce;
43 int maax;
44 int dijstra()
45 {
46     int i,j,k=0;
47     int cnt=0;
48     int min;
49     for(i=1; i<=n; i++)
50     {
51         lowcost[i]=map[1][i];
52         vis[i]=0;
53     }
54     vis[1]=1;
55     for(i=1; i<n; i++)
56     {
57         min=INF;
58         for(j=1; j<=n; j++)
59         {
60             if (!vis[j]&&lowcost[j]<min)
61             {
62                  min=lowcost[j];
63                  k=j;
64             }
65         }
66         vis[k]=1;
67         if (cnt<min)
68             cnt=min;
69         for(j=1;j<=n;j++)
70             if(!vis[j]&&lowcost[j]>map[k][j])
71                 lowcost[j]=map[k][j];
72     }
73     return cnt;
74 }
75 int main()
76 {
77     int cas;
78     scanf("%d",&cas);
79     while(cas--)
80     {
81         int i,j;
82         scanf("%d",&n);
83         for(i=0;i<=N;i++)
84             for(j=0;j<=N;j++)
85                 map[i][j]=INF;
86         int x;
87         for(i=1;i<=n;i++)
88         {
89             for(j=1;j<=n;j++)
90             {
91                 scanf("%d",&x);
92                 map[i][j]=x;
93             }
94         }
95         maax=dijstra();
96         printf("%d\n",maax);
97     }
98     return 0;
99 }
View Code

 

posted @ 2014-07-10 08:51  kinghold  Views(224)  Comments(0Edit  收藏  举报