POJ_2485(Prim算法)
来源:互联网 发布:常用的排序算法 编辑:程序博客网 时间:2024/05/17 20:30
Highways
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 18042 Accepted: 8381
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.
求MST中最长的一条边。
#include <stdio.h>#include<string.h>const int INF = 65537;const int MAXV = 501;int visit[MAXV];int lowcost[MAXV];int matrix[MAXV][MAXV];int nodeNum,ans;void prim(){int i,j;memset(visit,false,sizeof(visit));for(i=1;i<=nodeNum;i++)lowcost[i]=matrix[1][i];visit[1]=true;for(i=1;i<=nodeNum;++i){int temp =INF;int u;for(j=1;j<=nodeNum;++j){if(!visit[j]&&lowcost[j]<temp){temp = lowcost[j];u=j;}}visit[u]=true;if(temp==INF) break;if(temp>ans)ans = temp;for(j=1;j<=nodeNum;++j){if(!visit[j]&&lowcost[j]>matrix[u][j])lowcost[j]=matrix[u][j];}}}int main(){int testcase,i,j;//FILE *fp = freopen("data.txt","r",stdin);scanf("%d",&testcase);while(testcase--){scanf("%d",&nodeNum);for(i=1;i<=nodeNum;++i)for(j=1;j<=nodeNum;++j){scanf("%d",&matrix[i][j]);if(i==j) matrix[i][j]=INF;}ans=0;prim();printf("%d\n",ans);}return 0;}
- POJ_2485(Prim算法)
- poj_2485 Prim算法应用
- Highways(POJ_2485)
- poj_2485
- Prim算法 (改进)
- hdu1863(prim算法)
- 普里姆(Prim)算法
- poj_1258(Prim算法)
- POJ_1789(Prim算法)
- 普利姆(Prim)算法
- prim算法(poj1789)
- 普里姆算法(Prim)
- 普里姆(prim)算法
- 普里姆(Prim)算法
- prim算法(普里姆算法)
- Prim算法(普里姆算法)
- Prim算法(普里姆算法)加点法-Prim算法
- Prim算法(全程注释)
- eclipse快捷键
- 利用MVC模式开发Java应用程序(转自网络,作者龚赤兵)
- 黑马程序员_java基础之二
- woj 1049 dp
- PCRE函数简介和使用示例
- POJ_2485(Prim算法)
- SOAP tomcat7.0 HelloWorld
- PHP 5.3中的命名空间:你用过了么?
- hdu 1394 Minimum Inversion Number(线段树解法)
- ubuntu 安装
- 浏览器的同源策略类别及处理
- js取小数点后几位
- 一个解析xml的简单方法(记录)
- 前端大牛 Addy Osmani 的15个精彩PPT