zoukankan      html  css  js  c++  java
  • (贪心+并查集) 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
    ");
            else
                printf("%d
    ",e[pos].z);
        }
        return 0;
    }
    

      

  • 相关阅读:
    c++ 函数中的部分代码执行一次
    如何限制对象只能建立在堆上或者栈上
    FFMPEG Qt视频播放器
    C/C++中带可变参数的函数
    柔性数组
    压缩图片网站
    vscode存盘时格式化
    两个i标签之间有缝隙
    node 中process进程argv,argv0,execArgv,execPath
    chalk插件 使终端输出的字带颜色
  • 原文地址:https://www.cnblogs.com/water-full/p/4487214.html
Copyright © 2011-2022 走看看