POJ 2485 Highways 最小生成树 prim算法
来源:互联网 发布:网络架构是什么 编辑:程序博客网 时间:2024/05/01 04:03
Highways
Time Limit: 1000MS
Memory Limit: 65536KTotal Submissions: 23253
Accepted: 10721
Memory Limit: 65536KTotal Submissions: 23253
Accepted: 10721
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.
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.
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
130 990 692990 0 179692 179 0
Sample Output
692
Hint
Huge input,scanf is recommended.
Source
POJ Contest,Author:Mathematica@ZSU
忘了加输入到文件结束的结束条件,结果TLE,TLE,TLE,TLE,TLE,。。。。才AC。。。。
#include <stdio.h>#include <string.h>int mp[510][510];int dis[510];bool vis[510];const int inf = 0x3f3f3f3f;void prim(int n){ int mx = -1; for(int i = 0; i <= n; i++) dis[i] = mp[1][i]; dis[1] = inf; memset(vis,false,sizeof(vis)); vis[1] = true; for(int i = 1; i <= n - 1; i++) { int mn = inf; int v = 0; for(int j = 1; j <= n; j++) { if(!vis[j] && mn > dis[j]) { mn = dis[j]; v = j; } } if(mx < mn) mx = mn; vis[v] = true; for(int j = 1; j <= n; j++) { if(!vis[j] && dis[j] > mp[v][j]) dis[j] = mp[v][j]; } } printf("%d\n",mx);}int main(){ int n,t; while(~scanf("%d",&t)) { while(t--) { scanf("%d",&n); memset(mp,inf,sizeof(mp)); for(int i = 1;i <= n;i++) { for(int j = 1;j <= n;j++) { scanf("%d",&mp[i][j]); } } prim(n); } } return 0;}
0 0
- 最小生成树Prim算法 Highways POJ
- POJ 2485-Highways 最小生成树Prim算法
- POJ 2485 Highways 图论 prim算法 最小生成树
- POJ 2485 Highways 最小生成树 prim算法
- POJ 2485 Highways(最小生成树prim算法)
- poj 2485 Highways prim最小生成树
- POJ 2485 Highways (prim最小生成树)
- POJ 2485 Highways 【最小生成树Prim】
- poj 1751 Highways ( 最小生成树--prim算法 )
- POJ 1751 Highways(最小生成树prim)
- poj 1751 Highways 最小生成树 prim
- POJ 2485 Highways(prim 最小生成树)
- POJ 2485 Highways(最小生成树 Prim)
- poj 2485 Highways(kruscal or prim最小生成树)
- POJ 2485:Highways:典型prim最小生成树(2)
- poj 2485 Highways(prim求最小生成树)
- POJ 2485:Highways(最小生成树&&prim)
- poj 2485 Highways(最小生成树,prim)
- 1074.Reversing Linked List
- 解决pyside-uic编译QBottonGroup的报错:unexpected error in pyside-uic
- 1075.PAT Judge
- 1076.Forwards on Weibo
- #286 (Div. 2) C. Mr. Kitayuta, the Treasure Hunter
- POJ 2485 Highways 最小生成树 prim算法
- 为什么数据库要有主键
- poj 1936 贪心(s串是否为t的子串)
- 几种启动方式
- Linux块设备驱动程序原理
- 链接 autolink
- Linux安装mysql
- IDEA 环境常用设置整理
- iOS开发-内存管理