(贪心+并查集) poj 3228

Gold Transportation
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3060   Accepted: 1089

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

最小生成树的思想
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
int n,m;
int fa[205],w[205];
int find(int x)
{
    if(x!=fa[x])
        fa[x]=find(fa[x]);
    return fa[x];
}
void init()
{
    for(int i=0;i<=n;i++)
        fa[i]=i;
    for(int i=1;i<=n;i++)
        w[i]=0;
}
struct node
{
    int x,y,z;
}e[20005];
bool cmp(node a,node b)
{
    return a.z<b.z;
}
bool check()
{
    for(int i=1;i<=n;i++)
    {
        if(fa[i]==i&&w[i]<0)
            return false;
    }
    return true;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        init();
        int temp;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&temp);
            w[i]-=temp;
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&temp);
            w[i]+=temp;
        }
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].z);
        }
        sort(e+1,e+1+m,cmp);
        int pos;
        for(pos=1;pos<=m;pos++)
        {
            int fx,fy;
            fx=find(e[pos].x),fy=find(e[pos].y);
            if(fx==fy)
                continue;
            fa[fx]=fy;
            w[fy]+=w[fx];
            if(check())
                break;
        }
        if(pos==m+1)
            printf("No Solution\n");
        else
            printf("%d\n",e[pos].z);
    }
    return 0;
}

  

posted @ 2015-05-08 11:34  waterfull  阅读(110)  评论(0编辑  收藏  举报