POJ2485 Highways(高速公路)

1 篇文章 0 订阅
1 篇文章 0 订阅

Highways

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 29729

Accepted: 13532

Description

The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publichighways. So the traffic is difficult in Flatopia. The Flatopian government isaware of this problem. They're planning to build some highways so that it willbe possible to drive between any pair of towns without leaving the highwaysystem. 

Flatopian towns are numbered from 1 to N. Each highway connects exactly twotowns. All highways follow straight lines. All highways can be used in bothdirections. Highways can freely cross each other, but a driver can only switchbetween 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 tobe built. However, they want to guarantee that every town is highway-reachablefrom every other town.

Input

The firstline 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 thenumber 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 aninteger within [1, 65536]) between village i and village j. There is an emptyline after each test case.

Output

For eachtest case, you should output a line contains an integer, which is the length ofthe longest road to be built such that all the villages are connected, and thisvalue is minimum.

Sample Input

1

 

3

0 990 692

990 0 179

692 179 0

Sample Output

692

Hint

Hugeinput,scanf is recommended.

Source

POJ Contest,Author:Mathematica@ZSU

 

【题目大意】:

岛国(^_^)Flatopia非常平坦,不幸的是,Flatopia却没有公共的高速公路,所以在岛上的交通很困难。Flatopia政府也意识到了这个问题,所以正在计划建立高速公路,使居民可以随意地往来于各个城镇之间而不用离开高速公路。
Flatopia的城镇从1到N编号,每条高速公路连接两个城镇。每条高速公路都是直线,并且可以双向通行。高速公路可以互相交叉,但是司机只能在高速公路的末端,即城镇,转到其他的高速公路。
Flatopia政府想使最长的那条公路的长度最小化,但是也要保证每两个城镇之间都能够通过高速公路到达。

【输入】

数据的第一行是一个整数T,表示有T组测试数据。
每组数据的第一行是一个整数N(3≤N≤500),表示城镇的个数。接下来的N行,其中第i行包含N个整数,这N个整数其中的第j个整数表示i号城镇到j号城镇的距离,这个距离的大小范围是[1, 65536],每组数据后面有一个空行。

【输出】

对于每组数据,输出一行包含一个整数,表示最长的那条高速公路的长度,使得所有的城镇都能通过高速公路到达,并且这条最长的公路的长度是最小的。

【样例输入】

1
 
3
0 990 692
990 0 179
692 179 0

【样例输出】

692

【提示】

数据量庞大,建议使用scanf。

【解题思路】:

最小生成树问题,但不是求边的权值之和,而是求最小生成树中最长的那条边的长度。用Prim算法和Kruskal算法均可以。由于题目的测试数据是以邻接矩阵的形式给出的,因此可以考虑优先使用Prim算法。如果使用Kruskal算法,还需要把邻接矩阵中的顶点和边分离出来。

(1)Prim算法代码

#include <stdio.h>

 

const intN = 501; //最大顶点数

const intINF = 999999999;  //无穷大

int e[N][N];   //邻接矩阵

int dis[N];    //边的权值数组:存放最小生成树中边的权值:其中一个顶点已经在最小生成树

int visited[N];   //已经访问的顶点集合:0表示顶点没有访问,1表示顶点已经访问

 

int main()

{

   int t;

   while (scanf("%d", &t) != EOF){ //实例个数

      while (t--) {

         int n, i, j;

         scanf("%d", &n);  //顶点个数

         for (i = 1; i <= n; ++i){

            for (j = 1; j <= n; ++j){

                scanf("%d", &e[i][j]);  //输入邻接矩阵

            }

         }

         for (i = 1; i <= n; ++i){ //初始化

            dis[i]= e[1][i];     //初始化dis数组

            visited[i]= 0;       //初始时所有顶点均未访问

         }

 

         //Prim算法

         int sum = 0;//最小生成树边的权值之和

         int ans = 0;//存储最小生成树的最大边

         int count = 0; //已经访问的顶点数

 

         visited[1]= 1; //从顶点1开始使用Prim算法构造最小生成树

         count++;    //顶点1已经访问,所以count1

         while (count < n) {  //最小生成树包含n个顶点,count还没有到n

            int min = INF;

            for (i = 1; i <= n; i++){

                if (visited[i] == 0&& dis[i] < min) {

                   min= dis[i];//更新min

                   j= i;      //从顶点i开始扩展

                }

            }

            visited[j]= 1;    //从顶点j开始扩展,顶点j已经访问

            count++;           //顶点j已经访问,所以count1

            sum+= dis[j];     //最小生成树边的权值之和,本题不用

            if (ans < dis[j]) {  //求最小生成树的最大边

                ans= dis[j];   //更新

            }

            for (int k = 1; k <= n; k++){

                if (visited[k] == 0&& dis[k] > e[j][k]) {

                   dis[k]= e[j][k];

                }

            }

         }

         printf("%d\n", ans);//打印

         //printf("%d\n", sum);//打印

      }

   }

   return 0;

}

(2)Kruskal算法代码

#include <stdio.h>

#include <algorithm>//sort

using namespacestd;

 

const intN = 501; //最大顶点数

int e[N][N];   //邻接矩阵

int Tree[N];   //并查集数组

 

struct Edge

{

   int a, b; //边的两个顶点

   int cost; //边的权值

   bool operator<(constEdge &A)const {//重载运算符<使得边的权值可以从小到大排列

      return cost < A.cost;

   }

} edges[N * N];

 

int findRoot(int x) {         //递归查找顶点x所在树的根

   if (Tree[x] == -1)return x;//若为-1,x的根就是自身

   else {                  //否则

      int tmp =findRoot(Tree[x]);//递归查找x的父亲Tree[x]的根,tmp为最终的树根

      Tree[x]= tmp;           //查找过程中进行路径压缩:x到根之间遇到的所有顶点的父亲设为tmp

      return tmp;             //返回树根

   }

}

 

int main()

{

   int t;

   while (scanf("%d", &t) != EOF){//实例个数     

      while (t--) {

         int n;

         scanf("%d", &n);  //顶点个数

         for (int i = 1; i <= n; ++i){

            for (int j = 1; j <= n; ++j){

                scanf("%d", &e[i][j]);  //输入邻接矩阵

            }

         }

         for (int i = 1; i <= n; ++i){//初始化并查集数组

            Tree[i]= -1;            //开始时所有的顶点都是孤立的集合

         }

         int size = 0;              //边的条数

         for (int i = 2; i <= n; ++i){

            for (int j = 1; j < i; ++j) {

                edges[size].a = i;      //从邻接矩阵中读出顶点

                edges[size].b = j;

                edges[size].cost = e[i][j];//从邻接矩阵中读出边的权值

                size++;            //边数增加1

            }

         }

         sort(edges,edges + size);//边的权值按从小到大递增排列

        

         //Kruskal算法

         int ans = 0;

         for (int i = 0; i < size;++i) {//按边的权值递增的顺序依次选一条边

            int a = findRoot(edges[i].a);//一条边的顶点a

            int b = findRoot(edges[i].b);//一条边的另一个顶点b

            if (a != b) {              //顶点ab不在同一个集合中

                Tree[a]= b;             //合并

                if (ans < edges[i].cost) {//求最小生成树的最大边

                   ans= edges[i].cost//更新

                }

            }

         }

         printf("%d\n", ans); //打印

      }

   }

   return 0;

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值