POJ:3228-Gold Transportation(要求最小生成树最大边最小)

Gold Transportation

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3079 Accepted: 1101

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

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


解题心得:

  1. 题意就是很多个点,每个点有等待放置的金子也有可以储存的金子的量,给你一些边,你选择这些边连接的点的金子可以相互转移,最后需要让所有的金子都能够被储存,并且要求选择出来的边要最大的边最小。
  2. 这个就是一个最小生成树,记录一个点有的金子为正值,可以储存的值为负值,然后选择边将点连接起来,连接起来的点要将值相加,最后只要所有连接起来的点没有大于0的值就可以停止加边了。
  3. 关于最大的边最小的问题,可以直接使用Kruskal算法,在Kruskal的算法之中,添加的边都是从小开始添加的,所以使用Kruskal算法,得到的最小生成树最大的边就是最大边中的最小。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
using  namespace std;
const int maxn = 1000;
int res[maxn],father[maxn],Sum,n,m;
struct NODE
{
    int s,e,len;
} node[maxn*maxn];

bool cmp(NODE a,NODE b)
{
    return a.len < b.len;
}

void init()
{
    memset(res,0,sizeof(res));
    memset(node,0,sizeof(node));
    for(int i=0;i<maxn;i++)
        father[i] = i;
    //有金子的量为正,可以储存的量为负
    for(int i=1; i<=n; i++)
        scanf("%d",&res[i]);
    for(int i=1; i<=n; i++)
    {
        int x;
        scanf("%d",&x);
        res[i] -= x;
    }
    scanf("%d",&m);
    for(int i=0; i<m; i++)
        scanf("%d%d%d",&node[i].s,&node[i].e,&node[i].len);
    sort(node,node+m,cmp);
}

int find(int x)
{
    if(x == father[x])
        return x;
    return father[x] = find(father[x]);
}

bool check()//检查是不是所有的点都是不大于0的
{
    for(int i=1;i<=n;i++)
    {
        int fx = find(i);
        if(res[fx] > 0)
            return false;
    }
    return true;
}

void solve()
{
    int Max_path = 0;
    for(int i=0; i<m; i++)
    {
        if(check())//如果出现的点全是小于等于0的直接跳出
            break;
        int s = node[i].s;
        int e = node[i].e;
        int fx = find(s);
        int fy = find(e);
        if(fx == fy)
            continue;
        int len = node[i].len;
        if(len > Max_path)
            Max_path = len;
        int Min = min(res[fx],res[fy]);
        int Max = max(res[fx],res[fy]);
        father[fx] = fy;
        res[fy] = Min+Max;//合并之后将值也合并
    }
    if(!check())
        printf("No Solution\n");
    else
        printf("%d\n",Max_path);
}

int main()
{
    while(cin>>n && n)
    {
        init();
        solve();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值