码迷,mamicode.com
首页 > 编程语言 > 详细

Highways - poj 2485 (Prim 算法)

时间:2015-07-17 11:40:02      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

 
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24383   Accepted: 11243

Description

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They‘re planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system. 

Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways. 

The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.

Input

The first line of input is an integer T, which tells how many test cases followed. 
The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. There is an empty line after each test case.

Output

For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.

Sample Input

1

3
0 990 692
990 0 179
692 179 0

Sample Output

692

Hint

Huge input,scanf is recommended.
注意提示,开始我用的cin,cout输入输出,结果超时,看到提示用的scanf就AC了
 1 #include <stdio.h>
 2 #define INF 65537;
 3 int map[500][500];
 4 int vis[500];
 5 int dis[500];
 6 int n;
 7 int Prim(){
 8     for(int i=0;i<n;i++){
 9         vis[i]=0;
10         dis[i]=INF;
11     }
12     dis[0]=0;
13     for(int i=0;i<n;i++){
14         int p;
15         int mine=INF;
16         for(int j=0;j<n;j++){
17             if(!vis[j]&&mine>dis[j]){
18                 p=j;
19                 mine=dis[j];
20             }
21         }
22         vis[p]=1;
23         for(int j=0;j<n;j++){
24             if(!vis[j]&&dis[j]>map[p][j])
25                 dis[j]=map[p][j];
26         }
27     }
28     int maxe=0;
29     for(int i=0;i<n;i++){
30         if(maxe<dis[i])
31             maxe=dis[i];
32     }
33     return maxe;
34 }
35 int main() {
36 
37     int num;
38     scanf("%d",&num);
39     for(int i=0;i<num;i++){
40         scanf("%d",&n);
41         for(int j=0;j<n;j++){
42             for(int k=0;k<n;k++){
43                 scanf("%d",&map[j][k]);
44             }
45         }
46         int maxe=Prim();
47         printf("%d\n",maxe);
48     }
49     return 0;
50 }

 

Highways - poj 2485 (Prim 算法)

标签:

原文地址:http://www.cnblogs.com/sdxk/p/4649584.html

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