【POJ2485】prim算法
来源:互联网 发布:java程序员工资 编辑:程序博客网 时间:2024/06/07 06:19
Highways
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %lluDescription
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
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define N 510 #define inf 0x3f3f3f3f//无穷大 int map[N][N]; //两点间距离 int dis[N]; // 到N点的最短路 int vis[N]; //标记 int n; void prim(int u) { int i,j,k,start,min,max; memset(vis,0,sizeof(vis)); //初始化 for(i=1;i<=n;i++) if(i!=u) dis[i]=map[1][i]; //1到i的距离 vis[1]=true; //加入点1,设为起点 k=0; min=0;max=0; for(i=1;i<n;i++) //n-1条边 { min=inf; //不能直接连接的设为无穷大 for(j=1;j<=n;j++) if(dis[j]<min&&!vis[j]) { min=dis[j]; k=j; //加入点 } if(min>max) max=min; vis[k]=true; for(j=1;j<=n;j++) if(dis[j]>map[k][j]&&!vis[j]) { dis[j]=map[k][j]; } } printf("%d\n",max); } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d",&n); memset(map,inf,sizeof(map));//初始化 for(int i=1;i<=n;i++) //两端点 for(int j=1;j<=n;j++) { scanf("%d",&map[i][j]); } prim(1); } return 0; }
0 0
- 【POJ2485】prim算法
- prim最小生成树算法题poj2485
- POJ2485 prim算法最小生成树
- 最小生成树,MST,Prim算法,poj2485
- poj2485 kruskal与prim
- POJ2485《Highways》方法:Prim
- POJ2485 Highways【Prim】
- poj2485 Highways(prim java)
- POJ2485 prim 最小生成树
- Prim POJ1258 POJ2485 POJ1789 POJ1751
- poj2485 prim最小生成树
- poj2485 highways 之prim解法
- dijkstra算法 poj2485
- POJ2485,Highways,最小生成树继续Prim
- poj2485 prime算法+距离表
- poj2485
- poj2485
- poj2485
- Scala学习笔记5 - 特质
- 窗体调用KindEditor
- C++指针详解
- leetcode:单链表之Remove Nth Node From End of List
- ListFragment的使用
- 【POJ2485】prim算法
- poj 2253(最短路径)
- [转载] Nginx在Linux下的安装
- 手机的分辨率
- Linux中iptables设置详解
- 数据结构实验之串三:KMP应用
- windows服务器自动运行某个PHP的bat
- Codeforces Round #286 (Div. 2) C dp
- php的curl提交总结