Highways
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 27493 |
|
Accepted: 12554 |
Description
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia have no public highways. So the traffic are difficult in Flatopia. The Flatopian government is aware of this problem. They ' re planning to build some highways so it'll be possible to drive between any pair of towns without leaving the Highway System.
Flatopian towns is numbered from 1 to N. Each highway connects exactly and towns. All highways follow straight lines. All highways can is used in both directions. Highways can freely cross all other, but a driver can only switch between highways at a town that's located at the end O F 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 are highway-reachable from every the other town.
Input
The first line of input is a integer T, which tells how many test cases followed.
The first line of all case was an integer n (3 <= n <=), 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 are the distance (the distance s Hould is an integer within [1, 65536]) between village I and village J. There is a empty line after all test case.
Output
For each test case, you should output a line contains an integer, which was the length of the longest road to be built such That all the villages was connected, and this value is minimum.
Sample Input
Sample Output
1
3
0 990 692
990 0 179
692 179 0
692
Hint
Huge input,scanf is recommended.
Source
POJ Contest,author:[email protected]
The main idea: to connect all the cities and build highways, the cost of the most expensive highway is the least expensive.
Idea: Kruskal greedy for maximum edge, code implementation is very simple.
AC Code:
#include <stdio.h> #include <string.h> #include <algorithm>using namespace std;struct path{int x,y,w;} A[500*500+50];int f[5000];int cmp (path A,path b) {return A.W<B.W;} int find (int a) {int r=a; while (F[R]!=R) r=f[r]; int i=a; Int J; while (i!=r) {j=f[i]; F[i]=r; I=j; } return R;} void merge (int a,int b) {int A, B; A=find (a); B=find (b); if (a!=b) f[b]=a;} int main () {int t; scanf ("%d", &t); while (t--) {int n; int cont=0; scanf ("%d", &n); for (int i=0;i<n;i++) {f[i]=i; for (int j=0;j<n;j++) {int k; scanf ("%d", &k); A[cont].x=i; A[cont].y=j; A[cont++].w=k; }} int output; int tmp=0; Sort (a,a+cont,cmp); for (int i=0;i<cont;i++) {if (Find (a[i].x)!=find (A[I].Y)) { Merge (A[I].X,A[I].Y); tmp++; if (tmp==n-1)//If it is the last entry edge, then this edge must be the weight of the largest side {OUTPUT=A[I].W; }}} printf ("%d\n", output); }}
POJ 2485 Highways "minimum spanning tree"