Connect the Cities
来源:互联网 发布:cf出现36 1网络异常 编辑:程序博客网 时间:2024/06/05 19:05
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#include<stdio.h>#include<stdlib.h>#include<string.h>struct node{int l,r,val;}s[30000];int father[1100],xxx[1100];void build( int n ){int i;for( i=1;i<=n;i++ )father[i]=i;}int find( int a ){return a==father[a]?a:find( father[a] );}void Union( int a,int b ){int x,y;x=find( a );y=find( b );if( x> y ){father[x]=y;}elsefather[y]=x;}int cmp( const void *a ,const void * b ){return ( *( struct node * )a ).val>( *( struct node * )b ).val?1:-1;}int main(){int i, j ,k, n ,m, t;int a,b,c;scanf( "%d",&t );while( t-- ){int count=0,sum=0;scanf( "%d%d%d",&n,&m,&k );for( i=0;i<m;i++ ){scanf( "%d%d%d",&s[i].l,&s[i].r,&s[i].val );}build( n );for( i=0;i<k;i++ ){scanf( "%d",&a );for( j=0;j<a;j++ ){scanf( "%d",&xxx[j] );if( j&&find( xxx[j] )!=find( xxx[j-1] ) ){Union( xxx[j],xxx[j-1] );count++;}}}qsort( s,m,sizeof( s[0] ),cmp );for( i=0;i<m;i++ ){if( count>=n-1 )break;if( find( s[i].l )!=find( s[i].r ) ){sum+=s[i].val;Union( s[i].l,s[i].r );count++;}}if( count==n-1 )printf( "%d\n",sum );elseprintf( "-1\n" );}return 0;}
0 0
- Connect the Cities
- hdu3371 Connect the Cities
- Connect the Cities
- HDU3371--Connect the Cities
- hdu_3371 Connect the Cities
- hdu3371 Connect the Cities
- J - Connect the Cities
- Connect the Cities
- hud Connect the Cities
- HDU3371 Connect the Cities
- Connect the Cities
- hdu3371: Connect the Cities
- Connect the Cities
- hdu_3371 Connect the Cities
- HDU3371--- Connect the Cities
- Connect the Cities 3371
- Connect the Cities--hdoj
- HDU3371-Connect the Cities
- 1.4.2.1 阅读函数式程序
- 的鬼地方郭德纲的观点搞的
- 函数参数取地址实现参数自增
- SRM551 题解(T3 除外)
- PHP的CURL常用方法
- Connect the Cities
- 记第七届全国大学生信息安全技术大赛暨四川省大学生信息安全竞赛
- Linux学习
- POJ 3292 Semi-prime H-numbers
- 设计模式学习笔记之组合模式
- trie图的构建------易理解版
- PHP数组排序 array_multisort函数详细用法跟排序方法
- U盘装CentOS 6.4经验分享
- Ubuntu 14.04 64bit上磁力链爬虫dht部署指南