POJ 2485 Highways 图论 prim算法 最小生成树
来源:互联网 发布:网络调查公司 编辑:程序博客网 时间:2024/05/01 02:37
Highways
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 19136 Accepted: 8871
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.
这道题就是最典型的最小生成树问题,对于一个城市,用最少的路径把所有城市连接在一起,直接使用prim算法,得到每个点之间路径以后,通过prim模板直接算出最小路径,并且在prim模板中,每次计算出一个节点路径时,判断这条路径是否最长,然后返回最小生成树中的最长路径,输出此数,此题就可以AC了。。。很简答很基础的。
下面是AC代码:
#include<cstdio>#include<iostream>using namespace std;#define infinity 100000#define max_vertexes 505 int G[max_vertexes][max_vertexes];int prim(int vcount){ int i,j,k,longest=0; int lowcost[max_vertexes],closeset[max_vertexes],used[max_vertexes]; for (i=0;i<vcount;i++) { lowcost[i]=G[0][i]; closeset[i]=0; used[i]=0; } used[0]=1; for (i=1;i<vcount;i++) { j=0; while (used[j]) j++; for (k=0;k<vcount;k++) if ((!used[k])&&(lowcost[k]<lowcost[j])) j=k; if(G[j][closeset[j]]>longest)longest=G[j][closeset[j]]; used[j]=1; for (k=0;k<vcount;k++) if (!used[k]&&(G[j][k]<lowcost[k])) { lowcost[k]=G[j][k]; closeset[k]=j; } }return longest;}int main(){int i,j,t,n;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=0;i<n;i++)for(j=0;j<n;j++)scanf("%d",&G[i][j]);printf("%d\n",prim(n));}return 0;}
- POJ 2485 Highways 图论 prim算法 最小生成树
- 最小生成树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 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)
- Combo Box控件使用详解
- js 获取当前国家 省份 城市等信息
- Cocos2d-x学习之创建Android工程和编译
- android打开系统程序
- 项目开发过程--总结
- POJ 2485 Highways 图论 prim算法 最小生成树
- LCD单层菜单翻滚的实现
- MVVM框架介绍
- hive 加载数据后在HDFS上的文件及其整合
- Android permission 访问权限大全
- 插入排序
- LeetCode 31: Next Permutation
- android程序如何签名和发布应用程序
- mysql last_insert_id 的问题