POJ 2485 Highways(最小生成树prim算法)
来源:互联网 发布:电驴下载软件 编辑:程序博客网 时间:2024/04/30 14:54
Highways
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 25017 Accepted: 11541
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
题意:给出n个城市间n*n的距离表格。第i行第j列的数i号城市与j号城市的距离。要实现n个城市畅通,且总公路长度最短。输出满足以上条件下被建设起来的两个城市间最长的公路。
由于直接给出了城市与城市间的距离表,用prim算法比较简单。
代码如下:
<span style="font-size:12px;">#include<cstdio>#include<cstring>#define INF 0x3f3f3fint map[510][510],n;void prim(){int maxlen=-1;int i,j,next,min;int lowcost[510],visit[510];memset(visit,0,sizeof(visit));for(i=1;i<=n;++i) lowcost[i]=map[1][i];visit[1]=1;for(i=2;i<=n;++i) {min=INF;for(j=1;j<=n;++j){if(!visit[j]&&min>lowcost[j]){min=lowcost[j];next=j;}}if(min!=INF&&min>maxlen)//记录出现的最长公路 maxlen=min;visit[next]=1;for(j=1;j<=n;++j){if(!visit[j]&&lowcost[j]>map[next][j]) lowcost[j]=map[next][j];}}printf("%d\n",maxlen);}int main(){int t,i,j,d;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=1;i<=n;++i){for(j=1;j<=n;++j){scanf("%d",&d);map[i][j]=map[j][i]=d;}}prim();}}</span>
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)
- 南宁代办医院诊断书
- 柳州代办医院诊断书
- leetcode 114: Flatten Binary Tree to Linked List
- 桂林代办医院诊断书
- 【usaco/codevs2033/codevs1047/NOIP1999TG】 邮票问题浅谈
- POJ 2485 Highways(最小生成树prim算法)
- 梧州代办医院诊断书
- 钦州代办医院诊断书
- 贵港代办医院诊断书
- Docker之weave工具
- 百色代办医院诊断书
- 常见验证码的弱点与验证码识别
- Javascript 遍历对象的属性
- mysql主从--主库故障,从库切主库