hdu 3371 Connect the Cities
来源:互联网 发布:spss 01数据转换格式 编辑:程序博客网 时间:2024/04/30 00:33
Connect the Cities
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6005 Accepted Submission(s): 1746
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.
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
16 4 31 4 22 6 12 3 53 4 332 1 22 1 33 4 5 6
Sample Output
1
Author
dandelion
Source
HDOJ Monthly Contest – 2010.04.04
Recommend
lcy
典型最小生成树问题
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int set[510];//用于建立并查集。判断是否以联通。和成环int sz[510];int sum;struct city{ int s,e,cost;} ct[25100];//接收花费信息int cmp(city a,city b)//对花费排序{ return b.cost>a.cost;}int getint()//GNU c++对正数处理比较坑。导致超时几次。最后自己写了个读整数的 //秒秒钟就搞定了{ char c = getchar(); int t = 0; while (c < '0' || c > '9') { c = getchar(); } while (c >= '0' && c <= '9') { t = t * 10 + c - '0'; c = getchar(); } return t;}int root(int s)//查找根结点。{ int j,t,i=s; while(set[i]!=i) i=set[i]; j=s; while(j!=i)//路径压缩 { t=set[j]; set[j]=i; j=t; } return i;}int toone(int a,int b)//合并两个两个集合。若成环返回0,否则返回1{ int r1,r2; r1=root(a); r2=root(b); if(r1==r2) return 0; if(sz[r1]>sz[r2]) { set[r2]=r1; sz[r1]+=sz[r2]; } else { set[r1]=r2; sz[r2]+=sz[r1]; } sum--; return 1;}int main(){ int n,m,k,T,t,i,j,id,cost,center; T=getint(); while(T--) { n=getint(); m=getint(); k=getint(); for(i=1; i<=n; i++) { set[i]=i; sz[i]=1; } cost=0;//记录总最小花费 sum=n; for(i=0; i<m; i++) { ct[i].s=getint(); ct[i].e=getint(); ct[i].cost=getint(); } for(i=1; i<=k; i++) { t=getint(); for(j=0; j<t; j++) { id=getint(); if(j==0) center=id; else toone(center,id);//记录已相通的城市 } } sort(ct,ct+m,cmp);//对路排序 for(i=0; i<m; i++) { if(toone(ct[i].s,ct[i].e)) cost+=ct[i].cost; } if(sum==1) printf("%d\n",cost); else printf("-1\n"); } return 0;}
- hdu 3371 Connect the Cities
- Hdu-3371 Connect the Cities
- HDU 3371 Connect the Cities
- HDU-3371-Connect the Cities
- hdu 3371 Connect the Cities
- HDU 3371 Connect the Cities
- Hdu 3371 - Connect the Cities
- hdu 3371 Connect the Cities
- hdu 3371 Connect the Cities
- hdu 3371 Connect the Cities
- HDU 3371 Connect the Cities
- hdu 3371 Connect the Cities
- hdu 3371 Connect the Cities
- hdu 3371 Connect the Cities
- hdu 3371 Connect the Cities
- hdu 3371 Connect the Cities
- HDU 3371 Connect the Cities
- hdu 3371 Connect the Cities
- PHP反射API
- Android WindowManager的使用 & 添加 遮罩层 实现夜间模式
- Using the Switch Statement
- GCJ-02(火星坐标) 和 BD-09 (百度坐标)
- sony闪存卡/内存卡格式化修复工具
- hdu 3371 Connect the Cities
- WinSock三种选择I/O模型
- PHP拾忆:heredoc
- 用火狐浏览器下载中文文件名的文件乱码及其解决
- 主引导区修复工具MbrFix:Function failed. Error 5: 拒绝访问。
- 与算法无关的机器学习方法之主动学习
- C++中的static关键字
- 遍历Propertie属性文件所有内容
- php isset函数的功能,你真的懂了吗