博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj_3371Connect the Cities(最小生成树)
阅读量:4992 次
发布时间:2019-06-12

本文共 2974 字,大约阅读时间需要 9 分钟。

Connect the Cities

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5793    Accepted Submission(s): 1701


Problem Description
In 2100, since the sea level rise, most of the cities disappear. Though some survived cities are still connected with others, but most of them become disconnected. The government wants to build some roads to connect all of these cities again, but they don’t want to take too much money.  
 

Input
The first line contains the number of test cases.
Each test case starts with three integers: n, m and k. n (3 <= n <=500) stands for the number of survived cities, m (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and k (0 <= k <= 100) stands for the number of still connected cities.
To make it easy, the cities are signed from 1 to n.
Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.
Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities.
 

Output
For each case, output the least money you need to take, if it’s impossible, just output -1.
 

Sample Input
 
1 6 4 3 1 4 2 2 6 1 2 3 5 3 4 33 2 1 2 2 1 3 3 4 5 6
 

Sample Output
 
1

开始判断sum值,wa了,看题目(0 <= c <= 1000),并不能从sum=0判断的=。=

#include 
#include
#include
using namespace std;#pragma warning(disable : 4996)#define MAX 25005typedef struct edge{ int x, y; int w;}edge;edge e[MAX];int father[MAX], ranks[MAX];bool cmp(edge a,edge b){ return a.w < b.w;}void Make_Set(int n){ for(int i = 1; i <= n; i++) { father[i] = i; ranks[i] = 0; }}int Find_Set(int x){ if(x != father[x]) father[x] = Find_Set(father[x]); return father[x];}void Merge_Set(int x, int y){ x = Find_Set(x); y = Find_Set(y); if(x == y) return; if(ranks[x] > ranks[y]) { father[y] = x; } else if(ranks[x] < ranks[y]) { father[x] = y; } else { ranks[y]++; father[x] = y; }}int main(){ freopen("in.txt", "r", stdin); int t, i, j, n, m, k, sum, x, y, w, v; scanf("%d", &t); while (t--) { scanf("%d %d %d", &n, &m, &k); Make_Set(n); for(i = 0; i < m; i++) { scanf("%d %d %d", &x, &y, &w); e[i].x = x; e[i].y = y; e[i].w = w; } sort(e, e + m, cmp); for(i = 1; i <= k; i++) { scanf("%d", &v); if(v == 2) { cin >> x >> y; Merge_Set(x, y); } else { scanf("%d %d", &x, &y); Merge_Set(x, y); x = y; for(j = 2; j < v; j++) { scanf("%d", &y); Merge_Set(x, y); x = y; } } } sum = 0; for(i = 0; i < m; i++) { x = Find_Set(e[i].x); y = Find_Set(e[i].y); if(x != y) { Merge_Set(x, y); sum += e[i].w; } } int count = 0; for(i = 1; i <= n; i++) { if(i == father[i]) { count++; } } if(count > 1) { printf("-1\n"); } else { printf("%d\n", sum); } } return 0;}

转载于:https://www.cnblogs.com/lgh1992314/archive/2013/05/09/5835073.html

你可能感兴趣的文章
对测试人员或开发人员来说相互沟通有多重要?
查看>>
解释器、编译器以及他们之间的差别。
查看>>
MongoDB的快速手动安装
查看>>
JS制作简单的日历控件【JS Date对象操作实例演示】
查看>>
模板—树上倍增LCA
查看>>
高二小假期集训—D5
查看>>
EasyUI easyui-combobox 重复发送请求
查看>>
memcached-repcached
查看>>
[转]CentOS 5.3通过yum升级php到最新版本的方法
查看>>
UVA 11235 - Frequent values RMQ的应用
查看>>
大数据日志采集系统
查看>>
java 堆调优
查看>>
linux 安装JDK
查看>>
JAVA调用CMD命令
查看>>
weblogic的安装
查看>>
SSM框架中,controller的action返回参数给vue.js
查看>>
Mysql 基础3
查看>>
smartctl工具应用(转载整理)
查看>>
控件数据绑定总结
查看>>
HTTP协议
查看>>