ACM: 图论题 poj2485 (crazy now~!…

来源:互联网 发布:淘宝网店加盟哪家好 编辑:程序博客网 时间:2024/05/18 23:25

                                                                  Highways

Description

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

Flatopian towns are numbered from 1 to N. Each highway connectsexactly two towns. All highways follow straight lines. All highwayscan be used in both directions. Highways can freely cross eachother, but a driver can only switch between highways at a town thatis located at the end of both highways.

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

Input

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

Output

For each test case, you should output a line contains aninteger, which is the length of the longest road to be built suchthat 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

题意:  太裸露了这题 , 城市之间修建道路 , 要求联通全部点 , 最短路程就完成这件事,
         统计最大的一条路.
解题思路:
              1. 输入直接是邻接矩阵 , 用kruskal算法 +并查集.

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 250005

int n , m;
int u[MAX] , v[MAX] , w[MAX];
int p[MAX] , r[MAX];


int read_graph()
{
   scanf("%d",&n);
    int i ,j;
    intww;
    m = 0;
    for(i = 1; i<= n; ++i)
    {
      for(j = 1; j <= n; ++j)
      {
         scanf("%d",&ww);   
         if(i == j)
            continue;
         else
         {
            u[++m] = i;
            v[m] = j;
            w[m] = ww;
         }
      }
    }
}

int cmp(const int i,const int j)
{
    return w[i]< w[j];
}

int find(int x)
{
    return p[x]== x ? x : p[x] = find(p[x]);
}

int kruskal()
{
    int i;
    int ans =0;
    for(i = 1; i<= n; ++i)   p[i] = i;
    for(i = 1; i<= m; ++i) r[i] = i;
   sort(r,r+m,cmp);
    
    for(i = 1; i<= m; ++i)
    {
      int e = r[i];
      int x = find(u[e]);
      int y = find(v[e]);
      if(x != y)
      {
         if(ans < w[e])
            ans = w[e];
         p[x] = y;
      }
    }
    
    returnans;
}

int main()
{
//   freopen("input.txt","r",stdin);
    intnum;
   scanf("%d",&num);
   while(num--)
    {
      int result = 0;
      read_graph();
      result = kruskal();
      
      printf("%d\n",result);
    }
    
    return0;
}

0 0