POJ 1861 Network
来源:互联网 发布:js 数组 查找 编辑:程序博客网 时间:2024/04/19 13:00
最小生成树是满足条件的一种树,并不是唯一解
调用qsort后数组从零开始计数。。。被这个搞死了
#include<stdio.h>#include<stdlib.h>#include <cmath>#include <iostream>#include <cstdlib>using namespace std;int parent[15001];int k;struct edge{ int u,v,w;} edges[15001];int compare(const void *a, const void *b){return (*(edge *)a).w - (*(edge *)b).w;}int findset(int x){ int tmp,s; s=x; while (parent[s]>=0) s=parent[s]; while (s!=x) { tmp=parent[x]; parent[x]=s; x=tmp; } return s;}void unionset(int r1,int r2){ int rr1,rr2; rr1=findset(r1); rr2=findset(r2); int tmp=parent[rr1]+parent[rr2]; if (rr1==rr2) return; if (parent[rr1]>parent[rr2]) { parent[rr1]=rr2; parent[rr2]=tmp; } else { parent[rr2]=rr1; parent[rr1]=tmp; }}int main(){ int a,b,c,n,m,t,ans; edge s[10001]; scanf("%d%d",&n,&m); for (int i=0;i<=n;i++) parent[i]=-1; for (int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); edges[i].u=a; edges[i].v=b; edges[i].w=c; } qsort(edges,m,sizeof(edges[0]),compare); ans=0; for (int i=0;i<m;i++) { if (findset(edges[i].u)!=findset(edges[i].v)) { ans=ans+1; s[ans].u=edges[i].u; s[ans].v=edges[i].v; if (ans==(n-1)) t=edges[i].w; unionset(edges[i].u,edges[i].v); } if (ans>=n-1) break; } printf("%d\n",t); printf("%d\n",ans); for(int i=1;i<=ans;i++) printf("%d %d\n",s[i].u,s[i].v);}
- POJ 1861 Network
- poj 1861 Network
- POJ 1861 Network
- poj 1861 network
- poj 1861 network
- poj 1861 network
- POJ 1861 Network(Kruskal)
- poj 1861 Network MST
- POJ 1861 Network
- POJ 1861 Network
- POJ 1861 Network
- poj 1861 Network
- POJ-1861-Network
- POJ 1861 Network
- POJ 1861 Network(Kruskal)
- POJ 1861 Network
- poj 1861 Network
- POJ:1861 Network
- 国际化动态文件和其他标签的实例应用
- Java_11_15课堂总结
- 大文本和二进制操作
- 【二十三】 Linux网络编程——广播与组播
- js 加“...”的地方,考虑过长溢出省略处理
- POJ 1861 Network
- JSTL<fmt:parseNumber>标签
- java 线程基础
- u-boot bootm命令与内核传参过程
- Windows Debuggers: Part 1: A WinDbg Tutorial
- NDK编译错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
- android命令集合
- vimtutor中的基本命令
- div和table的区别(1)