HDU 1863 畅通工程
来源:互联网 发布:淘宝买迷你睡莲哪家好 编辑:程序博客网 时间:2024/05/17 21:08
畅通工程系列题目
用Krustral算法就可以过
#include<iostream>#include<cstdio>#include<cstring>#include<functional>#include<algorithm>#include<cstdlib>using namespace std;struct node{int from,to;int cost;}e[10005];int fa[105];int cmp(struct node a,struct node b){return a.cost<b.cost;}int n,m,sum,cnt,i;void init(){sum=0;cnt=0;for(i=1;i<=m;i++)fa[i]=i;}int getfa(int x){if(x==fa[x])return x;elsereturn fa[x]=getfa(fa[x]);}int merge(int u,int v){int t1,t2;t2=getfa(u);t1=getfa(v);if(t1!=t2){fa[t1]=t2;return 1;}return 0;}int main(){while(scanf("%d%d",&n,&m),n){init();for(i=1;i<=n;i++)scanf("%d%d%d",&e[i].from,&e[i].to,&e[i].cost);sort(e+1,e+n+1,cmp);for(i=1;i<=n;i++){if(merge(e[i].from,e[i].to))cnt++,sum+=e[i].cost;}//printf("cnt=%d sum=%d\n",cnt,sum);if(cnt==m-1)printf("%d\n",sum);elseprintf("?\n");}}
阅读全文
1 0
- HDU 1863 畅通工程
- hdu 1863 畅通工程
- hdu 1863 畅通工程
- HDU 1863 畅通工程
- hdu 1863 畅通工程
- hdu 1863 畅通工程
- hdu 1863 畅通工程
- HDU 1863 畅通工程
- hdu 1863 畅通工程
- HDU 1863 畅通工程
- hdu 1863 畅通工程
- hdu 1863 畅通工程
- hdu 1863 畅通工程
- HDU-畅通工程-1863
- HDU 1863 畅通工程
- hdu 1863 畅通工程
- HDU 1863 畅通工程
- hdu 1863 畅通工程
- mysql中key 、primary key 、unique key 与index区别
- ORM (Object Relational Mapping 对象关系映射)
- python基础知识点
- MUI-页面传参数
- CSS3的【transition-duration】属性
- HDU 1863 畅通工程
- 基础:Linux 内核剖析
- 开发网络常识
- 梯度下降
- HDU6098-Inversion
- 【JZOJ 5250】【GDOI2018模拟8.11】质数
- 数字证书及其认证过程
- node并发控制
- 初识nodejs14