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

POJ 2485 Highways

时间:2014-08-28 10:58:29      阅读:238      评论:0      收藏:0      [点我收藏+]

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

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

 
解题:求最小生成树中的最大边。
 
bubuko.com,布布扣
 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 2485 Highways

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

原文地址:http://www.cnblogs.com/crackpotisback/p/3940844.html

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