POJ 3228 Gold Transportation 【并查集】

传送门 

 

  

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".

思路:枚举最大边权值,然后将小于等于这个边权的所有边用并查集合并,然后我们会得到几个图(可能是一个,也可能十多个),怎样判断这个状态是否合理?我们已经假设这条边为最大边了,图中的路径可以随便用,我们只需要将一个图中的黄金总和仓库容量总和求出来,如果黄金数量总和>仓库总和,则说明在这个图中无法将黄金放置在仓库里,反之则可以。

给一组样例:

4
3 0 3 0
0 3 0 3
2
1 2 10
3 4 5

///ans=10

贴一下代码:(其实可以写带权并查集)

///#include<bits/stdc++.h>
///#include<unordered_map>
///#include<unordered_set>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<bitset>
#include<set>
#include<stack>
#include<map>
#include<list>
#include<new>
#include<vector>
#define MT(a,b) memset(a,b,sizeof(a));
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pai=acos(-1.0);
const double E=2.718281828459;
const ll mod=998424353;
const int INF=0x3f3f3f3f;

int n,m;
int st[205],en[205];
int p[205], v ;


struct node
{
    int s;
    int e;
    int c;
    bool friend operator<(node a,node b)
    {
        return a.c<b.c;
    }
}edge[10005];

int find(int x)
{
    return p[x]==x?x:p[x]=find(p[x]);
}

void init()
{
    for(int i=1;i<=n;i++)
        p[i]=i;
}

int judge()
{
    int sum[205];
    for(int i=1;i<=n;i++)
        sum[i]=0;
    for(int i=1;i<=n;i++)
    {
        int a=find(i);
        sum[a]+=st[i]-en[i];
    }
    for(int i=1;i<=n;i++)
        if(sum[i]>0)
            return 0;
    return 1;
}

int main()
{
    while(scanf("%d",&n)!=EOF,n)
    {
        for(int i=1; i<=n; i++)
            scanf("%d",&st[i]);
        for(int i=1; i<=n; i++)
            scanf("%d",&en[i]);
        scanf("%d",&m);
        for(int i=1; i<=m; i++)
            scanf("%d %d %d",&edge[i].s,&edge[i].e,&edge[i].c);
        sort(edge+1,edge+1+m);
        int flag=1;
        for(int i=1;i<=m;i++)
        {
            init();
            for(int j=1;j<=i;j++)
            {
                int x=find(edge[j].s);
                int y=find(edge[j].e);
                if(x!=y)
                    p[x]=y;
            }
            if(judge())
            {
                flag=0;
                printf("%d\n",edge[i].c);
                break;
            }
        }
        if(flag)
            printf("No Solution\n");
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值