POJ 3228-Gold Transportation(网络流_最大流+二分查找)

Gold Transportation

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 2995   Accepted: 1065

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

题意:有n个城镇,有的城镇里面有金矿,有的城镇里面有金库。第二行有n个数,代表第i个城镇里的金矿的存储量,第三行有n个数,代表第i个城镇里金库的容量。接下来有m条双向路径,每行输入三个数U,V,W,代表U和V之间的距离W。要求最长相邻距离中的最短距离。

思路:这个题由于是要求相邻距离的,所以不需要拆点。建图还是建立一个超级源点和 一个超级汇点,将金矿与源点相连,将金库与汇点相连。然后二分最大距离,小于二分的距离的连边,最后判断是否满流。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>

using namespace std;

const int inf=0x3f3f3f3f;
int head[510],num[510],d[510],pre[510],cur[510],q[510];
int n,cnt,s,t,nv,sum;
struct node {
    int u,v,cap;
    int next;
} edge[1000010];

void add(int u, int v, int cap)
{
    edge[cnt].v=v;
    edge[cnt].cap=cap;
    edge[cnt].next=head[u];
    head[u]=cnt++;

    edge[cnt].v=u;
    edge[cnt].cap=cap;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}

void bfs()
{
    memset(num,0,sizeof(num));
    memset(d,-1,sizeof(d));
    int f1=0, f2=0, i;
    q[f1++]=t;
    num[0]=1;
    d[t]=0;
    while(f1>=f2) {
        int u=q[f2++];
        for(i=head[u]; i!=-1; i=edge[i].next) {
            int v=edge[i].v;
            if(d[v]!=-1) continue;
            d[v]=d[u]+1;
            num[d[v]]++;
            q[f1++]=v;
        }
    }
}

int isap()
{
    memcpy(cur,head,sizeof(cur));
    int flow=0, u=pre[s]=s, i;
    bfs();
    while(d[s]<nv) {
        if(u==t) {
            int f=inf, pos;
            for(i=s; i!=t; i=edge[cur[i]].v) {
                if(f>edge[cur[i]].cap) {
                    f=edge[cur[i]].cap;
                    pos=i;
                }
            }
            for(i=s; i!=t; i=edge[cur[i]].v) {
                edge[cur[i]].cap-=f;
                edge[cur[i]^1].cap+=f;
            }
            flow+=f;
            if(flow>=sum)
                return flow;
            u=pos;
        }
        for(i=cur[u]; i!=-1; i=edge[i].next) {
            if(d[edge[i].v]+1==d[u]&&edge[i].cap)
                break;
        }
        if(i!=-1) {
            cur[u]=i;
            pre[edge[i].v]=u;
            u=edge[i].v;
        } else {
            if(--num[d[u]]==0) break;
            int mind=nv;
            for(i=head[u]; i!=-1; i=edge[i].next) {
                if(mind>d[edge[i].v]&&edge[i].cap) {
                    mind=d[edge[i].v];
                    cur[u]=i;
                }
            }
            d[u]=mind+1;
            num[d[u]]++;
            u=pre[u];
        }
    }
    return flow;
}

int main()
{
    int m,i,j;
    int sum1;
    int u,v,w;
    int g[510],s1[510],mp[510][510];
    while(~scanf("%d",&n)){
        if(n==0) break;
        sum=sum1=0;
        for(i=1;i<=n;i++){
            scanf("%d",&g[i]);
            sum+=g[i];
        }
        for(i=1;i<=n;i++){
            scanf("%d",&s1[i]);
            sum1+=s1[i];
        }
        scanf("%d",&m);
        memset(mp,inf,sizeof(mp));
        while(m--){
            scanf("%d %d %d",&u,&v,&w);
            if(mp[u][v]>w)
                mp[u][v]=mp[v][u]=w;
        }
        if(sum1<sum){
            printf("No Solution\n");
            continue ;
        }
        int low=1,high=10010,mid;
        int ans=-1,x;
        while(low<=high){
            mid=(low+high)/2;
            s=0;
            t=2*n+1;
            nv=t+1;
            cnt=0;
            memset(head,-1,sizeof(head));
            for(i=1;i<=n;i++){
                add(s,i,g[i]);
                add(i,t,s1[i]);
                for(j=1;j<i;j++){
                    if(mp[i][j]<=mid)
                        add(i,j,inf);
                }
            }
            x=isap();
            if(x>=sum){
                ans=mid;
                high=mid-1;
            }
            else
                low=mid+1;
        }
        if(ans!=-1)
            printf("%d\n",ans);
        else
             printf("No Solution\n");;
    }
    return 0;
}
时间: 2024-11-07 21:18:07

POJ 3228-Gold Transportation(网络流_最大流+二分查找)的相关文章

POJ 2455-Secret Milking Machine(网络流_最大流+二分查找)

Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10119   Accepted: 2973 Description Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within

POJ 3228 -- Gold Transportation【二分 &amp;&amp; 最大流】

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

POJ 3228 Gold Transportation

Gold Transportation Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 322864-bit integer IO format: %lld      Java class name: Main Recently, a number of gold mines have been discovered in Zorroming State. To p

poj 3228 Gold Transportation 并查集

题意: 有n和城市和m条路,每个城市都有产生金量和收集金量,现在要把所有黄金收集,求经过的最短边是多少. 分析: 二分+最大流或用并查集合并等价类. //poj 3228 //sep9 #include <iostream> #include <algorithm> using namespace std; const int maxN=256; const int maxM=10024; int p[maxN],sum[maxN]; struct Edge { int u,v,w

poj 3228 Gold Transportation 二分+最大流

http://poj.org/problem?id=3228 题意:有n个城市,每个城市有一定量的金子,我们需要把所有的金子都存到城市中的仓库中,有一些道路连通这些城市,每条道路都有自己的花费,要求的是,把所有的金子都运到仓库中,所走的那些道路,其中花费的最大值最小是多少. 思路,二分答案,把花费比答案小的那些道路建成一个图,再建立一个源点和所有城市相连,流量是每个城市的金子数量,再建立一个汇点,连接所有的城市,流量是每个城市金子的最大存储量.再跑一遍最大流,如果流量等于所有的金子和,那么就可以

POJ 3189 Steady Cow Assignment(网络流之最大流+二分构图)

题目地址:POJ 3189 我晕啊...飞快的把白天的任务完成又有什么用...节省下来的时间活生生的被我的手残给全浪费掉了...又调了一整天,问题居然是一个地方的n和m写反了!!!反思..反思...面壁去... 这题就是二分区间,然后枚举区间位置.然后建图就行了.不多说.. 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include

HDU 3667 Transportation(网络流之费用流)

题目地址:HDU 3667 这题的建图真是巧妙...为了保证流量正好达到k,需要让每一次增广到的流量都是1,这就需要把每一条边的流量都是1才行.但是每条边的流量并不是1,该怎么办呢.这个时候可以拆边,反正c最多只有5,拆成5条流量为1的边.但是这时候费用怎么办呢,毕竟平方的关系不能简单把每一条边加起来.这时候可以把拆的边的流量设为1,3,5,7,9.如果经过了3个流量,那就肯定会流1,3,5,费用为9,是3的平方,同理,其他的也是如此.然后按照给出的边建图跑一次费用流就可以了. 代码如下: #i

POJ 2516 Minimum Cost(网络流之费用流)

题目地址:POJ 2516 我晕啊...这题一上来就想到了对每种货物分开求..但是马上就放弃了..感觉这样求50次费用流太耗时..后来就果断拆点,拆了好长时间,一直TLE..即使降到了2600个点也TLE..然后又想起了这个分开求的方法,又突然觉得100个点的费用流几乎不费什么时间..最多也只是求50次而已,还是可以试试的..于是一试居然还真过了... 说到这里,思路应该已经知道了吧.就是对每种货物分开求,因为每种货物是相互独立的.每一次的建图思路就是: 源点与供应商连边,流量权值为供应商这种货

POJ 2112-Optimal Milking(网络流_最大流+floyd+二分查找)

Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12810   Accepted: 4632 Case Time Limit: 1000MS Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) co