您的位置:首页 > 其它

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

2016-05-19 23:16 393 查看
                                                                  
Highways

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 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

题意:  太裸露了这题 , 城市之间修建道路 , 要求联通全部点 , 最短路程就完成这件事
,

     
   
 统计最大的一条路.

解题思路:

     
     
    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;

    int
ww;

    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;

  
   
 }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: