最小生成树-poj-3228 Gold Transportation

Gold Transportation
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3590 Accepted: 1284

Description

Recently, a number of gold mines have been discovered in Zorroming State. To protect this treasure, we must transport this gold to the storehouses as quickly as possible. Suppose that the Zorroming State consists of N towns and there are M bidirectional roads among these towns. The gold mines are only discovered in parts of the towns, while the storehouses are also owned by parts of the towns. The storage of the gold mine and storehouse for each town is finite. The truck drivers in the Zorroming State are famous for their bad temper that they would not like to drive all the time and they need a bar and an inn available in the trip for a good rest. Therefore, your task is to minimize the maximum adjacent distance among all the possible transport routes on the condition that all the gold is safely transported to the storehouses.

Input

The input contains several test cases. For each case, the first line is integer N(1<=N<=200). The second line is N integers associated with the storage of the gold mine in every towns .The third line is also N integers associated with the storage of the storehouses in every towns .Next is integer M(0<=M<=(n-1)*n/2).Then M lines follow. Each line is three integers x y and d(1<=x,y<=N,0<d<=10000), means that there is a road between x and y for distance of d. N=0 means end of the input.

Output

For each case, output the minimum of the maximum adjacent distance on the condition that all the gold has been transported to the storehouses or "No Solution".

Sample Input

4
3 2 0 0
0 0 3 3
6
1 2 4
1 3 10
1 4 12
2 3 6
2 4 8
3 4 5
0

Sample Output

6


这破题..我找了2个小时的bug..


题目大意:有n个城镇,在这n个城镇中,有些城镇有金矿,有些城镇有仓库(不排除同时存在),
仓库是用来存金矿的,你需要把金矿运到仓库里,求所有金矿都放到仓库中后,运输过程中,
所经过的最大的路径(经过的两个城市的最大距离),同时你应该使得这个最大路径尽量的小;

解题思路:当总的金子大于仓库总存储时,应该输出“No Solution”;
还有一种情况就是,每条边上的金矿都能被自己的仓库所存储完,这个时候不需要
运输,输出“0”;
排除以上情况后,我们利用最小生成树,首先对边进行排序,然后依此加边;
用coun[i]代表当前边的集合里拥有的总的仓库,gold[i]代表当前边的集合里
拥有的总的金矿;每次加入边的时候,我们应该更新该边的父节点的coun值和
gold值,然后判断此时是否每个边集合是否都满足仓库数大于金矿数,如果满足,
此时加入的边的边长就应该为题目所求;  
  
       

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define INF 0x3fffffff
using namespace std;

const int maxn=210;
int n,m;
int gold[maxn],coun[maxn],f[maxn];

struct Edge
{
    int x,y;
    int d;
} edge[maxn*maxn];
bool cmp(Edge A,Edge B)
{
    return A.d<B.d;
}
int find(int x)
{
    return x==f[x]?x:find(f[x]);
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        for(int i=0; i<=n; i++)
            f[i]=i;
        int sum1=0,sum2=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&gold[i]);
            sum1+=gold[i];
        }
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&coun[i]);
            sum2+=coun[i];
        }
        scanf("%d",&m);
        for(int i=1; i<=m; i++)
            scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].d);
        if(sum1>sum2)
        {
            printf("No Solution\n");
            continue;
        }
        sort(edge+1,edge+m+1,cmp);
        int flag=0;
        for(int i=1; i<=n; i++)
        {
            int u=find(i);
            if(gold[u]>coun[u])
            {
                flag=1;
                break;
            }
        }
        if(flag==0)
        {
            printf("0\n");
            continue;
        }
        int k=0,flag1=0;

        for(int i=1; i<=m; i++)
        {
            int s=find(edge[i].x);
            int e=find(edge[i].y);
            if(s!=e)
            {
                f[s]=e;

                coun[e]+=coun[s];
                coun[s]=0;

                gold[e]+=gold[s];
                gold[s]=0;

                k=edge[i].d;
                flag1=0;
                for(int j=1; j<=n; j++)
                {
                    int z=find(j);
                    if(gold[z]>coun[z])
                    {
                        flag1=1;
                        break;
                    }
                }
                if(flag1==0)
                    break;
            }
        }
        if(flag1==0)
            printf("%d\n",k);

        else
            printf("No Solution\n");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值