POJ 2485 Highways

Highways

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on  PKU. Original ID: 2485
64-bit integer IO format: %lld      Java class name: Main
 
 
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.
 

Source

 
解题:求最小生成树中的最大边。
 
 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cmath>

 5 #include <algorithm>

 6 #include <climits>

 7 #include <vector>

 8 #include <queue>

 9 #include <cstdlib>

10 #include <string>

11 #include <set>

12 #include <stack>

13 #define LL long long

14 #define pii pair<int,int>

15 #define INF 0x3f3f3f3f

16 using namespace std;

17 const int maxn = 510;

18 struct arc {

19     int to,w;

20     arc(int x = 0,int y = 0):to(x),w(y) {}

21 };

22 vector<arc>g[maxn];

23 int n,d[maxn];

24 bool done[maxn];

25 priority_queue< pii,vector< pii >,greater< pii > >q;

26 int prim() {

27     while(!q.empty()) q.pop();

28     d[1] = 0;

29     q.push(make_pair(d[1],1));

30     int ans = 0;

31     while(!q.empty()){

32         int u = q.top().second;

33         int w = q.top().first;

34         q.pop();

35         if(done[u]) continue;

36         done[u] = true;

37         ans = max(w,ans);

38         for(int i = 0; i < g[u].size(); i++){

39             if(d[g[u][i].to] > g[u][i].w){

40                 d[g[u][i].to] = g[u][i].w;

41                 q.push(make_pair(d[g[u][i].to],g[u][i].to));

42             }

43         }

44     }

45     return ans;

46 }

47 int main() {

48     int t,i,j,w;

49     scanf("%d",&t);

50     while(t--) {

51         scanf("%d",&n);

52         for(i = 0; i <= n; i++) {

53             g[i].clear();

54             d[i] = INF;

55             done[i] = false;

56         }

57         for(i = 1; i <= n; i++) {

58             for(j = 1; j <= n; j++) {

59                 scanf("%d",&w);

60                 if(i != j) g[i].push_back(arc(j,w));

61             }

62         }

63         printf("%d\n",prim());

64     }

65     return 0;

66 }
View Code

 

你可能感兴趣的:(poj)