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. android之获得当前连接wifi的名字

    WifiManager wifiMgr = (WifiManager) mActivity.getSystemService(Context.WIFI_SERVICE); int wifiState ...

  2. 微商营销实战技巧分享,轻松月入10W

    如今能够说是移动互联时代.在这个时代,微信眼下能够说是当之无愧的移动应用,依据报道,眼下微信有7个多亿的用户,怪不得那么多人看到微商的时代,一大批人開始涌入微商,导致如今微信上卖产品都已经泛滥了,导致 ...

  3. 定义和使用EL函数

    EL为表达式语言,在EL中,允许定义和使用函数.下面将介绍如何定义和使用EL的函数. 1. 定义和使用函数 函数的定义和使用分为以下3个步骤: (1)编写一个Java类,并在该类中编写公用的静态方法, ...

  4. [2] 立方体(Box)图形的生成算法

    顶点数据的生成 bool YfBuildBoxVertices ( Yreal extentX, Yreal extentY, Yreal extentZ, YeOriginPose originPo ...

  5. 基于Qt的wifi智能小车的制作(一)

     基于Qt的wifi智能小车的制作(一) 好久不写博客了,真的是有点惭愧了.翻开上一次的博客,到现在已经2个多月了,只能说是自己太懒惰了!忙是另一回事!趁今天晚上有点时间回顾下这一段时间的收获以及做的 ...

  6. Container With Most Water leetcode java

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  7. Reorder List leetcode java

    题目: Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must d ...

  8. Chain of Responsibility 责任链模式 MD

    责任链模式 简介 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链,请求在这个链上[传递],直到链上的某一个对象决定处理此请求.发出这个请求的客户 ...

  9. 如何在MVC的ActionLink中应用Resource文件

    项目中建立Resources文件夹. 添加Resource文件,必须添加一个默认的,其他语言可以添加很多个.我这里只添加了一个中文的. 双击每个资源文件,将Access Modifier 设置成pub ...

  10. [Node.js] Stream all things!

    Node.js come alone with many Stream API. Stream is useful when handling large trunck of data. For ex ...