【POJ】Highways
来源:互联网 发布:淘宝联盟编辑分享文案 编辑:程序博客网 时间:2024/06/07 20:51
Problem here
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:Mathematica@ZSU
Solution
#include <string>#include <cstdio>#include <stdio.h>#define MAX_NUM 0x3f3f3f3fusing namespace std;int map[501][501];int dis[501];bool visit[501];int n, cnt, min_num, j, sum;void prim(){ while(cnt < n){ min_num = MAX_NUM; for(int i = 1; i <= n; i++){ if(visit[i] == false && dis[i] < min_num){ min_num = dis[i]; j = i; } } visit[j] = true; cnt++; sum = max(sum, dis[j]); for(int i = 1; i <= n; i++){ if(visit[i] == false && dis[i] > map[j][i]){ dis[i] = map[j][i]; } } }}int main(){ int kase; scanf("%d", &kase); while(kase--){ scanf("%d", &n); for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ scanf("%d", &map[i][j]); } } for(int i = 2; i <= n; i++){ dis[i] = map[1][i]; visit[i] = false; } sum = 0; visit[1] = true; dis[1] = 0; cnt = 1; prim(); printf("%d\n", sum); } return 0;}
- POJ Highways
- Highways Poj
- 【POJ】Highways
- Highways POJ
- Highways POJ
- poj poj 2485 Highways
- POJ 2485 Highways
- poj 1751 Highways
- poj 2485 Highways
- POJ-2485 highways
- POJ 2485 Highways Kruskal
- poj 2485 Highways
- Poj 2485 Highways
- POJ 2485 Highways
- poj 2485 Highways
- poj 1751 highways
- Poj 1751 Highways(prim)
- poj 2485 Highways
- Python的集合set
- 子查询中空值问题
- 列举几种进程的同步机制及优缺点
- treap
- mysql 中 unix_timestamp和from_unixtime 时间戳函数
- 【POJ】Highways
- Android Studio生成jar包,其他工程调用。(res文件)
- java web之jsp初学习
- if-else语句
- Java中的接口回调
- mysql分区表简单例子
- 【POJ】A Knight's Journey
- 【垂直搜索引擎搭建00】说在前面的话
- AsyncTask原理分析