Highways - poj 2485 (Prim 算法)

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24383   Accepted: 11243

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

Hint

Huge input,scanf is recommended.

注意提示,开始我用的cin,cout输入输出,结果超时,看到提示用的scanf就AC了

 1 #include <stdio.h>
 2 #define INF 65537;
 3 int map[500][500];
 4 int vis[500];
 5 int dis[500];
 6 int n;
 7 int Prim(){
 8     for(int i=0;i<n;i++){
 9         vis[i]=0;
10         dis[i]=INF;
11     }
12     dis[0]=0;
13     for(int i=0;i<n;i++){
14         int p;
15         int mine=INF;
16         for(int j=0;j<n;j++){
17             if(!vis[j]&&mine>dis[j]){
18                 p=j;
19                 mine=dis[j];
20             }
21         }
22         vis[p]=1;
23         for(int j=0;j<n;j++){
24             if(!vis[j]&&dis[j]>map[p][j])
25                 dis[j]=map[p][j];
26         }
27     }
28     int maxe=0;
29     for(int i=0;i<n;i++){
30         if(maxe<dis[i])
31             maxe=dis[i];
32     }
33     return maxe;
34 }
35 int main() {
36
37     int num;
38     scanf("%d",&num);
39     for(int i=0;i<num;i++){
40         scanf("%d",&n);
41         for(int j=0;j<n;j++){
42             for(int k=0;k<n;k++){
43                 scanf("%d",&map[j][k]);
44             }
45         }
46         int maxe=Prim();
47         printf("%d\n",maxe);
48     }
49     return 0;
50 }
时间: 2024-10-12 13:21:03

Highways - poj 2485 (Prim 算法)的相关文章

poj 2485(prim最小生成树)

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

POJ 2485(Kruskal算法)

第一道Kruskal算法题 #include <iostream>#include <algorithm>#include <cstdio>using namespace std;#define max 505int f[max],maxw;struct edge{    int st,en,w;}ed[max*max/2]; int  find(int k){    if(k!=f[k])f[k]=find(f[k]);    return f[k];}void co

Highways POJ-1751 最小生成树 Prim算法

Highways POJ-1751 最小生成树 Prim算法 题意 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多少的边能使得该无向图连通.输出需要添加边的两端点编号即可. 解题思路 这个可以使用最短路里面的Prim算法来实现,对于已经连接的城市,处理方式是令这两个城市之间的距离等于0即可. prim算法可以实现我们具体的路径输出,Kruskal算法暂时还不大会. 代码实现 #include<cstdio> #include<cs

poj 2485 Prim裸

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34492   Accepted: 15570 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Fl

Truck History - poj 1789 (Prim 算法)

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20884   Accepted: 8075 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company

Agri-Net - poj 1258 (Prim 算法)

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44373   Accepted: 18127 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your h

POJ 2485 Prim 找最长的边

A国没有高速公路,因此A国的交通很困难.政府意识到了这个问题并且计划建造一些高速公路,以至于可以在不离开高速公路的情况下在任意两座城镇之间行驶. A国的城镇编号为1到N, 每条高速公路连接这两个城镇,所有高速公路都可以在两个方向上使用.高速公路可以自由的相互交叉. A国政府希望尽量减少最长高速公路的建设时间(使建设的最长的高速公路最短),但是他们要保证每个城镇都可以通过高速公路到达任意一座城镇. Input 第一个输入的数字T,代表着T组样例. 接下来输入一个N, 代表一共有N个城镇. 然后读入

POJ 2485 Highways (kruskal 最小生成树)

Highways POJ 2485so 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 ca

poj 2485 Highways (最小生成树)

链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要加个推断 比較最小生成树每条边的大小即可 kruskal算法 #include<cstdio> #include<algorithm> using namespace std; int f[510],n,m; struct stu { int a,b,c; }t[20100]; int