九度:1024<并查集>
来源:互联网 发布:报表软件架构 编辑:程序博客网 时间:2024/05/22 14:07
浙大2007年研究生机试
畅通工程
#include <stdio.h>#include <iostream>#include <algorithm>using namespace std;int fa[110];struct Cost{int s;int e;int cost;bool operator < (const Cost &A) const {return cost < A.cost;}};int Getfa(int x){if (x == fa[x])return x;fa[x] = Getfa(fa[x]);return fa[x];}int main(){#ifdef ONLINE_JUDGE#else freopen("E:\\in.txt", "r", stdin);#endifint n, m; //n 道路数,m村庄数while(scanf("%d %d", &n, &m) && n){Cost buf[110];int i;for(i=1; i<=m; i++){fa[i] = i;}for(i=0; i<n; i++){scanf("%d %d %d", &buf[i].s, &buf[i].e, &buf[i].cost);}sort(buf, buf+n);int ans=0;for(i=0; i<n; i++){if(Getfa(buf[i].s) != Getfa(buf[i].e)){fa[Getfa(buf[i].s)] = Getfa(buf[i].e);ans += buf[i].cost;}}int j, flag=1;for(i=1; i<=m; i++){for(j=i+1; j<=m; j++){if(Getfa(i) != Getfa(j)){flag = 0;break;}}}if(flag == 0)//资料不足printf("?\n");elseprintf("%d\n", ans);}//whilereturn 0;}
0 0
- 九度:1024<并查集>
- 【九度】并查集题目整理
- 九度OJ 朋友圈 -- 并查集
- 九度:1012<并查集>
- 九度:1028<并查集>
- 九度:1445<并查集>
- 九度 1035 并查集相关
- 九度OJ 朋友圈 并查集
- 九度1012(并查集)
- 并查集--九度1444
- 九度OJ题目 1526 朋友圈 并查集
- 九度1012 畅通工程---简单并查集应用
- 九度:1017<并查集><Kruskal><MST>
- 九度oj 1526 朋友圈 并查集
- 九度OJ 题目1526:朋友圈(并查集)
- 九度1012畅通工程(并查集)
- 【九度OJ】:1526 并查集 朋友圈问题
- 并查集--九度1012 & 1109 [路径压缩]
- 九度:1017<并查集><Kruskal><MST>
- 05——android项目的启动过程及安装过程笔记
- cocos2d-x 2.2.2 创建项目
- POJ2081:Recaman's Sequence
- Ubuntu安装及ubuntu系统使用菜岛教程
- 九度:1024<并查集>
- 图像处理实用资源
- 杭电OJ 1029:Ignatius and the Princess IV
- PHP读取CURL模拟登录时生成Cookie文件
- LEX YACC
- 九度:1028<并查集>
- vc中调用Com组件的方法详解
- Deep Learning(深度学习)
- lex yaac