Gold Transportation【并查集】

版权声明:https://blog.csdn.net/qq_41730082 https://blog.csdn.net/qq_41730082/article/details/86744483

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

  一开始在思考并查集上面的时候除了少许问题,然后一开始的想法就是Kruskal,但是确实就是套用这个思维去做的,然后就想到带权去思考,既然有的地方是有仓库的,有的地方是有金子的,也许有的地方又有金子又有仓库,所以处理一下,尽可能先就近原则的放满,然后再往后去找就是了。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 205;
int N, M, root[maxN], sum_gold, box[maxN];
struct Eddge
{
    int u, v, val;
    Eddge(int a=0, int b=0, int c=0):u(a), v(b), val(c) {}
    friend bool operator < (Eddge e1, Eddge e2) { return e1.val < e2.val; }
}edge[maxN*maxN/2];
int fid(int x) { return x == root[x] ? x : (root[x] = fid(root[x])); }
bool differ(int x, int y) { return x * y < 0; }  //符号不同就是有抵消
void Kruskal()
{
    int ans = 0;
    int link = 0;
    for(int i=1; i<=M; i++)
    {
        if(link == N - 1) break;
        if(sum_gold <= 0) break;
        int x = edge[i].u, y = edge[i].v;
        int u = fid(x), v = fid(y);
        if(u != v)
        {
            link++;
            if(differ(box[u], box[v]))
            {
                if(box[u] + box[v] > 0) //异号但是不完全消耗
                {
                    sum_gold += min(box[u], box[v]);
                }
                else    //完全消耗了
                {
                    sum_gold -= max(box[u], box[v]);
                }
            }
            root[u] = v;
            box[v] += box[u];
            ans = edge[i].val;
        }
    }
    if(sum_gold <= 0) printf("%d\n", ans);
    else printf("No Solution\n");
}
void init()
{
    sum_gold = 0;
    for(int i=1; i<=N; i++) root[i] = i;
}
int main()
{
    while(scanf("%d", &N) && N)
    {
        init();
        for(int i=1; i<=N; i++) scanf("%d", &box[i]);
        for(int i=1; i<=N; i++)
        {
            int e1; scanf("%d", &e1);
            box[i] -= e1;
        }
        for(int i=1; i<=N; i++) if(box[i] > 0) sum_gold += box[i];
        scanf("%d", &M);
        for(int i=1; i<=M; i++) scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].val);
        sort(edge + 1, edge + M + 1);
        Kruskal();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41730082/article/details/86744483