hdu3080The plan of city rebuild

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3080

题目如下:

The plan of city rebuild

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


Problem Description
News comes!~City W will be rebuilt with the expectation to become a center city. There are some villages and roads in the city now, however. In order to make the city better, some new villages should be built and some old ones should be destroyed. Then the officers have to make a new plan, now you , as the designer, have the task to judge if the plan is practical, which means there are roads(direct or indirect) between every two villages(of course the village has not be destroyed), if the plan is available, please output the minimum cost, or output"what a pity!".
 

Input
Input contains an integer T in the first line, which means there are T cases, and then T lines follow.
Each case contains three parts. The first part contains two integers l(0<l<100), e1, representing the original number of villages and roads between villages(the range of village is from 0 to l-1), then follows e1 lines, each line contains three integers a, b, c (0<=a, b<l, 0<=c<=1000), a, b indicating the village numbers and c indicating the road cost of village a and village b . The second part first contains an integer n(0<n<100), e2, representing the number of new villages and roads(the range of village is from l to l+n-1), then follows e2 lines, each line contains three integers x, y, z (0<=x, y<l+n, 0<=z<=1000), x, y indicating the village numbers and z indicating the road cost of village x and village y. The third part contains an integer m(0<m<l+n), representing the number of deserted villages, next line comes m integers, p1,p2,…,pm,(0<=p1,p2,…,pm<l+n) indicating the village number. 
Pay attention: if one village is deserted, the roads connected are deserted, too.
 

Output
For each test case, If all villages can connect with each other(direct or indirect), output the minimum cost, or output "what a pity!".
 

Sample Input
  
  
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
 

Sample Output
  
  
70 160
 

Author
wangjing
 

Source
 
这个题目的输入比较复杂,但是只要认真读题就能解决。。开始有l个城市,e1条路,然后告诉n个新城市,e2条新路,然后又告诉要拆毁的城市。。因为拆毁了,所以我引入一个标记数组,如果被拆毁,就标记为1,预处理完毕后,用kruscal算法,如果遇到任意一条边的一个端点为1,则跳过。。。最后如果得到的边为l+n+m-1,则说,说明最小生成树形成,返回ans。。。。

代码如下::

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值