Connect the Cities
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24992 Accepted Submission(s): 5773
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
Author
dandelion
Source
HDOJ Monthly Contest – 2010.04.04
Recommend
lcy
思路
WA Code
(以下是我自己初次写的代码,和下面的代码感觉没啥差别啊,总是WA,望好心大佬赐教!不胜感激!!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
const int nmax=500+50;
const int mmax=25500+50;
int n,m,k;
int cnt;//生成树的有效合并次数
struct Edge{
int u,v;
ll val;
}edge[mmax];
bool cmp(Edge a,Edge b){
return a.val<b.val;
}
int father[nmax];
int findFather(int u){
if(u==father[u]) return u;
else{
int f=findFather(father[u]);
father[u]=f;
return f;
}
}
void init(int n){
for(int i=1;i<=n;i++){
father[i]=i;
}
}
void Kruskal(int cnt){
init(n);
sort(edge,edge+m,cmp);
ll ans=0;
for(int i=0;i<m;i++){
int fu=findFather(edge[i].u);
int fv=findFather(edge[i].v);
//printf("fu:%d fv:%d\n",fu,fv);
if(fu!=fv){
father[fv]=fu;
ans+=edge[i].val;
cnt++;
//printf("%d\n",cnt);
if(cnt==n-1)
break;
}
}
if(cnt==n-1)
printf("%lld\n",ans);
else
printf("-1\n");
}
int main(int argc, char** argv) {
int T;
while(scanf("%d",&T)!=EOF){
while(T--){
memset(father,0,sizeof(father));
memset(edge,0,sizeof(edge));
scanf("%d %d %d",&n,&m,&k);
init(n);
cnt=0;
for(int i=0;i<m;i++){
scanf("%d %d %lld",&edge[i].u,&edge[i].v,&edge[i].val);
}
for(int i=0;i<k;i++){
int t,u,v;
scanf("%d %d",&t,&u);
//对于已经连接的边,cnt++;
int fu=findFather(u);
t--;
//printf("fu:%d ",fu);
while(t--){
scanf("%d",&v);
int fv=findFather(v);
//printf("fv:%d\n",fv);
if(fu!=fv){
father[fv]=fu;
cnt++;//
//printf("cnt1:%d\n",cnt);
}
}//printf("\n");
}
//sort(edge,edge+m,cmp);
//qsort(edge,edge+m,sizeof(edge),cmp);
Kruskal(cnt);
}
}
return 0;
}
AC Code(我改的别人的Code)
#include<iostream>
#include<cstdio>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int n,m,cnt;
struct Edge
{
int u;
int v;
int val;
}edge[25500];
bool cmp(Edge a,Edge b)
{
return a.val < b.val;
}
int father[550];//,ans;
int findFather(int u){
if(u==father[u]) return u;
else{
int f=findFather(father[u]);
father[u]=f;
return f;
}
}
void Kruskal(int cnt)
{ int ans=0;
sort(edge,edge+m,cmp);
for(int i = 0; i < m; i++)
{
int fu = findFather(edge[i].u);
int fv = findFather(edge[i].v);
if(fu != fv)
{
//father[fu] = fv;//WA
father[fv] = fu;
cnt++;
ans += edge[i].val;
if(cnt == n-1)
break;
}
}
if(cnt == n-1)
printf("%d\n",ans);
else
printf("-1\n");
}
int main()
{
int T,k,t;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i = 1; i <= n; i++)
father[i] = i;
for(int i = 0; i < m; i++)
{
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].val);
}
cnt = 0;//ans = 0;
int u,v;
for(int i = 0; i < k; i++)
{
scanf("%d%d",&t,&u);
int fu = findFather(u);
t--;
while(t--)
{
scanf("%d",&v);
int fv = findFather(v);
if(fu != fv)
{
father[fv] = fu;
cnt++;
}
}
}
Kruskal(cnt);
}
return 0;
}