poj 2485 Highways(prime算法模板)
2016-08-04 09:16
253 查看
HighwaysTime Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %lluSubmit StatusDescriptionThe 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 planningto 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 townthat 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.InputThe 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]) betweenvillage i and village j. There is an empty line after each test case.OutputFor 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
1 3 0 990 692 990 0 179 692 179 0Sample Output
692
模板,,,,
代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define inf 0x3f3f3fint map[510][510];int vis[510];int dis[510];int ans;int maxn;int prime(int n){memset(vis,0,sizeof(vis));memset(dis,inf,sizeof(vis));dis[1]=0,maxn=0;for(int i=1;i<=n;i++){int k=-1,tp=inf;for(int j=1;j<=n;j++){if(!vis[j]&&dis[j]<tp){tp=dis[j];k=j;}}maxn=max(maxn,tp);vis[k]=1,ans+=tp;for(int j=1;j<=n;j++){if(!vis[j]&&map[k][j]<dis[j]){dis[j]=map[k][j];}}}return ans;}int main(){int t,n;scanf("%d",&t);while(t--){memset(map,inf,sizeof(map));scanf("%d",&n);for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){int w;scanf("%d",&w);map[i][j]=w;}}prime(n);printf("%d\n",maxn);}return 0;}
相关文章推荐
- prime算法(最小生成树模板)
- 最小生成树prime算法模板
- 最小生成树prime算法模板
- 关于Prime算法的从入门到升天的讲解(带模板)
- SPFA,dijskra,prime,topu四种算法的模板
- poj 1811 Prime Test_Pollard_rho算法模板
- 最小生成树Prime算法(模板)
- PKU1258,模板题,prime算法就可以直接套用这个模板
- POJ-1811 Prime Test(Miller_Rabin算法和Pollard_rho算法模板题)
- [prime模板]
- poj3164(最小树形图&朱刘算法模板)
- HDU 1016 Prime Ring Problem - DFS模板题
- (算法 第四版)排序算法类的模板
- AC自动机算法模板
- 划分树算法 模板
- 二分图最大匹配之匈牙利算法模板
- [搜索 剪枝] 算法马拉松25 E BigPrime
- 【有向图的最小树形图---朱刘算法】模板
- 图算法模板
- 图的邻接矩阵生成算法,深度优先遍历算法,广度优先遍历算法,Prime算法。):