POJ3228- Gold Transportation (并查集+kruskal)

7 篇文章 0 订阅
4 篇文章 0 订阅

 

Gold Transportation

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3547 Accepted: 1262

 

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

题目:POJ3228

题意:现在有一些城市,在城市里,可能存在金矿和仓库,现在给出城市里的金矿数和仓库容量,问现在要把所有金矿都运到仓库中,问运输线路最长的一条最短是多少。

思路:题目只要求我们把金矿运到仓库的最长路最短,很容易想到最小生成树,但是题目只要求我们把金矿运到仓库,意味着我们可能并不需要得到一颗完整的生成树,可能是几颗树,所有我们只需要向树中添边,只要金矿都运送到仓库即可。此时的最大边即为答案。用kruskal 算法。

kruskal算法AC代码:

 

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define met(s,k) memset(s,k,sizeof s)
#define scan(a) scanf("%d",&a)
#define scanl(a) scanf("%lld",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannl(a,b) scanf("%lld%lld",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define prin(a) printf("%d\n",a)
#define prinl(a) printf("%lld\n",a)
using namespace std;
typedef long long ll;
const int maxn=205;
const double eps=1e-4;
int n,m,father[maxn][3],ranks[maxn],sumgold,sumstore,vis[maxn];
struct node
{
    int x,y;
} c[maxn];
struct edge
{
    int st,en,len;
    friend bool operator< (const edge &a,const edge &b)
    {
        return a.len<b.len;
    }
} e[maxn*maxn];
void init()
{
    for(int i=1; i<=n; i++)father[i][0]=i;
    sumgold=0;
    sumstore=0;
    met(ranks,0);
    met(vis,0);
}
int check()
{
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])if(father[i][1]>father[i][2])return 0;
    }
    return 1;
}
int findfather(int x)
{
    return father[x][0]==x?x:father[x][0]=findfather(father[x][0]);
}
int unite(int x,int y)
{
    x=findfather(x);
    y=findfather(y);
    if(ranks[x]<ranks[y])
    {
        vis[x]=1;
        father[x][0]=y;
        father[y][1]+=father[x][1];
        father[y][2]+=father[x][2];
        return y;
    }
    else
    {
        vis[y]=1;
        father[y][0]=x;
        father[x][1]+=father[y][1];
        father[x][2]+=father[y][2];
        if(ranks[x]==ranks[y])ranks[x]++;
        return x;
    }
}
int kruskar()
{
    for(int i=0; i<m; i++)
    {
        if(findfather(e[i].st)!=findfather(e[i].en))
        {
            unite(e[i].st,e[i].en);
            if(check())return e[i].len;
        }
    }
    return -1;
}
int main()
{
    while(scan(n),n)
    {
        int ans;
        init();
        for(int i=1;i<=n;i++)scan(father[i][1]),sumgold+=father[i][1];
        for(int i=1;i<=n;i++)scan(father[i][2]),sumstore+=father[i][2];
        scan(m);
        for(int i=0;i<m;i++)scannn(e[i].st,e[i].en,e[i].len);
        if(sumgold>sumstore)
        {
            printf("No Solution\n");
            continue;
        }
        sort(e,e+m);
        if((ans=kruskar())==-1)printf("No Solution\n");
        else prin(ans);

    }
    return 0;
}

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值