poj 2485 highway lightblueme
2013-01-22 17:16
447 查看
#include <stdlib.h> #include <stdio.h> #include <iostream> #include <algorithm> using namespace std; #define NUM 505 int father[NUM]; void initroot(){ for(int i=0;i<505;i++) father[i]=-1; } int findroot(int n){ int x=n; if(father[x]==-1){ return x; } x=findroot(father[x]); return x; } struct road{ int from,to; int cost; bool operator <(const road& p)const { return cost<p.cost; } }; struct road Roads[NUM*(NUM+1)/2]; int main(){ int T,N; scanf("%d",&T); for(int index=0;index<T;index++){ scanf("%d",&N); int roadi=0; int temp; initroot(); for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ scanf("%d",&temp); if(i>j){ Roads[roadi].from=i; Roads[roadi].to=j; Roads[roadi].cost=temp; roadi++; } } } int edgenum=0; sort(Roads,Roads+N*(N-1)/2); int maxl; for(int k=0;k<N*(N+1)/2;k++){ int from=Roads[k].from; int to=Roads[k].to; from=findroot(from); to=findroot(to); if(from!=to){//合并 father[from]=to; edgenum++; } if(edgenum==N-1){//当达到N-1条边是算法结束 maxl=Roads[k].cost; break; } } printf("%d\n",maxl); } return 0; }
相关文章推荐
- POJ 2255 Tree Recovery lightblueme
- POJ 1083 Moving Tables lightblueme
- poj 1258 prim lightblueme
- poj 1088 lightblueme 最佳的滑雪路径
- poj 2075 lightblueme double qsort cmp函数的写法
- POJ 2159 Ancient Cipher lightblueme
- POJ 1258 lightblueme Agri Net angry!89次的失误终于得到最后的成功
- poj 2262 Goldbach's Conjecture lightblueme
- poj 1163 lightblueme 三角形的最大路径和
- POJ 2485 Highway(Prim+邻接矩阵)
- POJ_2485 Highway_Kruskal
- 最小生成树--highway(poj 2485);
- poj 2485 Highway(Kruskal)
- POJ_2485 Highway_Prim
- poj 2485 Highway
- POJ - 1743 Musical Theme
- POJ 1743 Musical Theme 后缀数组
- POJ 2485 Highways【最小生成树最大权——简单模板】
- POJ 1743 Musical Theme 后缀数组
- prim最小生成树算法题poj2485