标签:des style blog http java color
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3080
题目如下:
2 4 5 0 1 10 0 2 20 2 3 40 1 3 10 1 2 70 1 1 4 1 60 2 2 3 3 3 0 1 20 2 1 40 2 0 70 2 3 0 3 10 1 4 90 2 4 100 0
70 160
代码如下::
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define maxn 40005
#define INF 0x3f
using namespace std;
int T,l,e1,n,e2,m;
int root[maxn],vis[maxn];
struct edge
{
int u,v,w;
}e[maxn];
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
int findroot(int x)
{
if(root[x]!=x)
root[x]=findroot(root[x]);
return root[x];
}
int kruscal()
{
int i,fx,fy,edge=0,ans=0;
for(i=1;i<=(e1+e2);i++)
{
if(vis[e[i].u]==1||vis[e[i].v]==1)
continue;
else
{
fx=findroot(e[i].u);
fy=findroot(e[i].v);
if(fx==fy)
continue;
else
{
edge++;
if(fx<fy)
root[fx]=fy;
else
root[fy]=fx;
ans=ans+e[i].w;
if(edge==l+n-m-1)
return ans;
}
}
}
return -1;
}
int main()
{
int i,newvillage,j,old,ans,pos;
scanf("%d",&T);
ind:while(T--)
{
memset(vis,0,sizeof(vis));
scanf("%d%d",&l,&e1);
for(i=0;i<l;i++)
root[i]=i;
for(i=1;i<=e1;i++)
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
scanf("%d%d",&n,&e2);
for(i=l;i<l+n;i++)
{
root[i]=i;
}
for(i=e1+1;i<=e1+e2;i++)
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%d",&old);
vis[old]=1;
}
sort(e+1,e+1+e1+e2,cmp);
ans=kruscal();
if(ans==-1)
printf("what a pity!\n");
else
printf("%d\n",ans);
}
return 0;
}
标签:des style blog http java color
原文地址:http://blog.csdn.net/u014303647/article/details/24660411