Connect the Cities

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

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

代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 25005
typedef struct Side
{
int vs;
int ve;
int cost;
}side;
side sta[maxn];
int father[],rank[];
void init(int n)
{
for(int i= ;i<n ; i++)
{
father[i]=i;
rank[i]=;
}
} int setfind(int x)
{ if(x!=father[x])
father[x]=setfind(father[x]);
return father[x]; //这里不能用x
}
void krusal(int a,int b)
{
int x=setfind(a);
int y=setfind(b);
if(x==y) return ;
if(x<y)
{
father[y]=x;
rank[x]+=rank[y];
}
else
{
father[x]=y;
rank[y]+=rank[x];
}
}
int cmp(const void* a , const void* b)
{
return (*(side*)a).cost-(*(side*)b).cost;
}
int main()
{
int test,n,m,k,i,ans;
scanf("%d",&test);
while(test--)
{
ans=;
scanf("%d%d%d",&n,&m,&k);
init(n);
for(i=; i<m ;i++)
{
scanf("%d%d%d",&sta[i].vs,&sta[i].ve,&sta[i].cost);
}
int a,b,c;
for(i=;i<k ;i++)
{
scanf("%d",&c);
scanf("%d",&a);
while(c-- >)
{
scanf("%d",&b);
krusal(a,b);
}
}
qsort(sta,m,sizeof(sta[]),cmp);
for(i=; i<m ;i++)
{
int tem1=setfind(sta[i].vs);
int tem2=setfind(sta[i].ve);
if(tem1 != tem2)
{
krusal(tem1,tem2);
ans+=sta[i].cost;
}
} if(rank[]==n)
printf("%d\n",ans);
else
printf("-1\n");
}
return ;
}

HDUOJ---3371Connect the Cities的更多相关文章

  1. Connect the Cities[HDU3371]

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...

  2. hduoj 1455 && uva 243 E - Sticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...

  3. codeforces 613D:Kingdom and its Cities

    Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. Ho ...

  4. CF449B Jzzhu and Cities (最短路)

    CF449B CF450D http://codeforces.com/contest/450/problem/D http://codeforces.com/contest/449/problem/ ...

  5. hdu 2874 Connections between cities [LCA] (lca->rmq)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  6. Connect the Cities(MST prim)

    Connect the Cities Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  7. HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑

    这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...

  8. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

  9. hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  10. hduoj 4706 Herding 2013 ACM/ICPC Asia Regional Online —— Warmup

    hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup Herding Time Limit: 2000/1000 ...

随机推荐

  1. AIX加入�能够telnet远程连接账号方法

    AIX 中加入�账号能够使用命令mkuser 和 SMIT 两种方式,这里介绍SMIT方式 1.使用root 账号登录AIX 2.输入 smitty user 3.选择Add a User 4.输入& ...

  2. 【BZOJ】【2242】【SDOI2011】计算器

    快速幂/扩展欧几里得/BSGS 经典好例题!! 三个问题三种算法…… 算法:白书(算法竞赛入门经典——训练指南)全有…… /************************************** ...

  3. Spring 事务模板

    最近项目开发中需要用到单机事务,因为项目中使用了Spring和Mybatis框架,所以通过Spring来进行事务的管理,并且记录一下事务配置的过程 第一步:配置DataSource <!-- 发 ...

  4. go语言基础之copy的使用

    1.copy的使用 示例: package main //必须有个main包 import "fmt" func main() { srcSlice := []int{1, 2} ...

  5. python部分重点底层源码剖析

    Python源码剖析—Set容器(hashtable实现) python源码剖析(内存管理和垃圾回收)

  6. SQL Server基础知识三十三问 (15-21)

    15. 存储过程可以调用自己么, 或者说可能有递归的存储过程么? SP nesting最多可以到多少层? 答: 可以的. 因为Transact-SQL 支持递归, 你可以编写可以调用自己的存储过程. ...

  7. ExcelReader(解析Excel的工具类)

    package cn.com.css.common.util; import java.io.IOException; import java.io.InputStream; import java. ...

  8. ASP.NET WebServices 因 URL 意外地以“/HelloWorld”结束,请求格式无法识别。

    今天在使用Asp.net做一个WebServices时通过Get方式调用方法时却出现了以下错误(仅在Get请求下,POST下正常)   在网络和MSDN上了解到WebServices默认只启用 Htt ...

  9. java代码在开始事务后,先做了一个查询,再insert,此时会报: java.sql.SQLException: could not retrieve transation read-only status server

    解决过程: 查看mysql的事物隔离级别 SHOW VARIABLES LIKE '%iso%'; 返回结果: REPEATABLE-READ 把这个改成:READ-COMMITTED 就好了: SE ...

  10. java基础-类变量,类方法