POJ2485 Highways 【MST】

Highways

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22842   Accepted: 10525

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.

Source

POJ Contest,Author:[email protected]

求MST中的最大边

#include <stdio.h>
#include <string.h>

#define maxn 505
#define inf 0x3f3f3f3f

int G[maxn][maxn], n;
int dis[maxn];
bool vis[maxn];

void getMap() {
	scanf("%d", &n);
	int i, j;
	for(i = 0; i < n; ++i)
		for(j = 0; j < n; ++j)
			scanf("%d", &G[i][j]);
}

int getNext() {
	int i, pos = -1, val = inf;
	for(i = 0; i < n; ++i)
		if(!vis[i] && dis[i] < val) {
			val = dis[i]; pos = i;
		}
	return pos;
}

void solve() {
	int i, u, ans = 0;
	for(i = 0; i < n; ++i) {
		vis[i] = 0; dis[i] = inf;
	}
	dis[u=0] = 0;
	while(u != -1) {
		vis[u] = 1;
		if(ans < dis[u]) ans = dis[u];
		for(i = 0; i < n; ++i)
			if(!vis[i] && dis[i] > G[u][i])
				dis[i] = G[u][i];
		u = getNext();
	}
	printf("%d\n", ans);
}

int main() {
	int t;
	scanf("%d", &t);
	while(t--) {
		getMap();
		solve();
	}
	return 0;
}
时间: 2024-10-12 12:55:21

POJ2485 Highways 【MST】的相关文章

POJ2485 Highways【Prim】

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

Bzoj1083 1083: [SCOI2005]繁忙的都市【MST】

大水题,真不知道出题者是怎么把这么水的题出的这么长的TAT 其实这题在于考语文水平,一共三个要求,前两个要求意思就是要选出的道路是树形的,最后一个要求就是要权值最小,于是整个题意说白了就是求一棵MST,以前向星的形式给出最容易想到kruskal算法,于是这题顺利结束,从看题一直到调试结束半个小时搞定…… #include<iostream> #include<cstdio> #include <math.h> using namespace std; intfather

POJ 2485 Highways【最小生成树】

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

POJ 1258 Agri-Net【MST】

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 49389   Accepted: 20511 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 nee

kruskal 【MST】

const int MAXN = 110;//点 const int MAXM = 10000;//边 int f[MAXN];//并查集使用 struct Edge { int u, v, w; }edge[MAXN]; int tol;//边数 初始化 0 void addedge(int u,int v,int w) { edge[tol].u = u; edge[tol].v = v; edge[tol++].w = w; } bool cmp(Edge a,Edge b) { retu

【HDU1102】Constructing Roads(MST基础题)

最小生成树水题.prim一次AC 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cctype> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 10 #define typec int

【HDU1162】Eddy&#39;s picture(MST基础题)

很基础的点坐标MST,一不留神就AC了, - - !! 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #include <numeric> 9 #include &

【KM】BZOJ1937 [Shoi2004]Mst 最小生成树

这道题拖了好久因为懒,结果1A了,惊讶∑( 口 || [题目大意] 给定一张n个顶点m条边的有权无向图.现要修改各边边权,使得给出n-1条边是这张图的最小生成树,代价为变化量的绝对值.求最小代价之和. [思路] 思路有点神,并不是我这种蒟蒻能够想到的XD 显然由贪心,树边必定变成wi-di,非树边必定变成wi+di (di≥0) 为了满足Mst的性质,考察一条非树边j,它加最小生成树后,必定构成一个环.对于环上的每一条树边i,有wi-di≤wj+dj,即di+dj≥wi-wj.这非常类似于KM的

【HDU1301】Jungle Roads(MST基础题)

爽爆.史上个人最快MST的记录7分40s..一次A. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #include <numeric> 9 10 #define