1090. Highways
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
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 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.
Output
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.
This problem contains multiple test cases!
The first line of a multiple input is an integer T, then a blank line followed by T input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of T output blocks. There is a blank line between output blocks.
Sample Input
1 3 0 990 692 990 0 179 692 179 0
Sample Output
692
只说一句话:输出格式神坑
看好再写,不要对着 AC 代码 debug 浪费一堆时间。
(说得好像「写这道题」这种行为本身不是在浪费时间一样
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #define rep(i,l,r) for(int i = l; i <= r; i++) 6 #define clr(x,y) memset(x,y,sizeof(x)) 7 #define travel(x) for(Edge *p = last[x]; p; p = p -> pre) 8 using namespace std; 9 const int maxn = 510; 10 inline int read(){ 11 int ans = 0, f = 1; char c = getchar(); 12 for(; !isdigit(c); c = getchar()) if (c == '-') f = -1; 13 for(; isdigit(c); c = getchar()) ans = ans * 10 + c - '0'; 14 return ans * f; 15 } 16 struct Edge{ 17 int from, to, dis; 18 inline bool operator < (const Edge &_Tp) const{ 19 return dis < _Tp.dis; 20 } 21 }edge[130010]; 22 int n, cnt, fa[maxn]; 23 int getfa(int x){ 24 return fa[x] == x ? x : fa[x] = getfa(fa[x]); 25 } 26 void work(){ 27 n = read(); rep(i,1,n) fa[i] = i; cnt = 0; 28 rep(i,1,n) rep(j,1,n){ 29 int x = read(); 30 if (i < j){ 31 edge[++cnt].from = i; edge[cnt].to = j; 32 edge[cnt].dis = x; 33 } 34 } 35 sort(edge+1,edge+cnt+1); 36 int tot = 0, ans; 37 rep(i,1,cnt){ 38 int a = getfa(edge[i].from), b = getfa(edge[i].to); 39 if (a == b) continue; 40 fa[a] = b; ans = edge[i].dis; 41 if (++tot == n-1){ 42 printf("%d ",ans); return; 43 } 44 } 45 } 46 int main(){ 47 int T = read(); 48 rep(i,1,T){ 49 work(); 50 if (i != T) printf(" "); 51 } 52 return 0; 53 }