您的位置:首页 > 其它

poj 2485 Highways

2016-08-04 16:00 316 查看
Highways
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 28525 Accepted: 13006
DescriptionThe 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 possibleto 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 townthat 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.InputThe 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]) betweenvillage i and village j. There is an empty line after each test case.OutputFor 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
这题就是找最短路中的最大长度
还是用prim算法
下面是代码
#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>using namespace std;int a[10002][10002];int book[10002];int dis[10002];int main(){    int n,m,i,j;    scanf("%d",&m);    while(m--)    {    scanf("%d",&n);        for(i = 0;i < n;i++)        {            for(j = 0;j < n; j++)            {                scanf("%d",&a[i][j]);            }        }        for(i = 0;i < n; i++)        {            dis[i] = a[0][i];        }        memset(book,0,sizeof(book));        book[0] = 1;        int max1 = 0;        int k;        for(i = 0;i < n-1; i++)        {            int min1 = (1<<20);             for(j = 0;j < n; j++)             {                 if(dis[j] < min1&&book[j] == 0)                 {                     k = j;                     min1 = dis[j];                 }             }             max1 = max(max1,min1);     //更新最大边就行             book[k] = 1;             for(j = 0;j < n; j++)             {                 if(dis[j] > a[j][k])                 {                     dis[j] = a[j][k];                 }             }        }        printf("%d\n",max1);    }}
代码菜鸟,如有错误,请多包涵!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: