zoj1542||poj 1861(简单-MST)
来源:互联网 发布:php paste 编辑:程序博客网 时间:2024/05/02 00:59
同上,手写Kruskal水过。special judge,样例是错的,,囧 = =
zoj 170留念
#include<iostream>#include<cstring>#include<cstdlib>#define MAXN 1005#define MAXM 15000using namespace std;typedef struct edge{int u,v,w;}edge;edge edges[MAXM];int parent[MAXN];int N,M;int cmp(void const *a,const void *b){edge aa=*(const edge *)a;edge bb=*(const edge *)b;return aa.w-bb.w;}void UFset(){for(int i=0;i<=N;i++)parent[i]=-1;}int Find(int x){int s;for(s=x;parent[s]>=0;s=parent[s]) ;while(s!=x){int tmp=parent[x];parent[x]=s;x=tmp;}return s;}void Union(int R1,int R2){int r1=Find(R1);int r2=Find(R2);int tmp=parent[r1]+parent[r2];if(parent[r1]>parent[r2]){parent[r1]=r2;parent[r2]=tmp;}else {parent[r1]=tmp;parent[r2]=r1;}}void Kruskal(){int num=0;int maxV=0;int i,k=0;int u,v;int order[MAXM];UFset();for(i=0;i<M;i++){u=edges[i].u;v=edges[i].v;if(Find(u)!=Find(v)){order[k++]=i;if(maxV<edges[i].w) maxV=edges[i].w;num++;Union(u,v);}if(num>=N-1) break;}cout<<maxV<<endl;cout<<num<<endl;for(i=0;i<num;i++){cout<<edges[order[i]].u<<" "<<edges[order[i]].v<<endl;}}int main(){int u,v,w;int i;while(cin>>N>>M){for(i=0;i<M;i++){cin>>u>>v>>w;edges[i].u=u;edges[i].v=v;edges[i].w=w;}qsort(edges,M,sizeof(edges[0]),cmp);Kruskal();}return 0;}
- zoj1542||poj 1861(简单-MST)
- ZOJ1542
- POJ 1287 Networking (mst简单)
- poj 1861 Network MST
- poj-1861-MST
- POJ 1861 - MST - Krustral
- POJ 1861||ZOJ1542 kruskal求最小生成树(POJ秒杀啊)
- POJ 1861 Network(MST)
- POJ 1679 The Unique MST(简单题)
- [MST] poj 1679
- poj 1751 Highways MST
- POJ 3723 Conscription MST
- poj The Unique MST
- poj 1251 MST
- poj 1287 MST(kruskal)
- poj 3723 MST
- POJ 3625 - MST
- POJ 1251 MST Krustral
- 设置mysql允许外部IP连接的解决方法
- Delphi中ScriptControl的高级应用(二)
- HttpClient示例
- ONGL,JSTL中符号#,$,%的用法
- eclipse下安装ve
- zoj1542||poj 1861(简单-MST)
- 在 Windows 7 中通过 USB 连接线安装备用驱动程序
- BeanUtils.copyProperties()方法
- c语言中的输入与输出
- 从java project到dynamic web project
- 【线段树+离散化】Mayor's Posters
- POJ 3268 Silver Cow Party
- Windows与Linux下InputStream读取字节与字符的不同
- Java调用.NET webservice方法的几种方式