POJ 3228 Gold Transportation 二分求最大最小边

点击打开链接
Gold Transportation
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 2721 Accepted: 964

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

Source


有n个小镇,m条路,有的小镇里面有一定数量的金矿,有的小镇里面有一定数量的storage,要求将所有的金矿都移到storage里面,要求在将所有金矿移到storage的基础上,使得最长路径最短。
二分路径长度求最短。
网络流建图:
源点和每个小镇相连,容量为每个小镇的金矿量。
汇点和每个小镇相连,容量为每个小镇的storage量。
然后路径长度小于mid的相连,容量为inf。因为每条路可以走无数次。

//804K	47MS	
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define inf 0x3f3f3f3f
#define M 1007
#define MIN(a,b) a>b?b:a;
using namespace std;
struct E
{
    int v,w,next;
} edg[1000000];
int dis[20000],gap[20000],head[20000],nodes;
int sourse,sink,nn;
int gold[2007],store[2007];
int u[21000],v[21000],c[21000];
int n,m,ans_gold=0;
void addedge(int u,int v,int w)
{
    edg[nodes].v=v;
    edg[nodes].w=w;
    edg[nodes].next=head[u];
    head[u]=nodes++;
    edg[nodes].v=u;
    edg[nodes].w=w;//因为是无向图,所以反向边权值也是w
    edg[nodes].next=head[v];
    head[v]=nodes++;
}
int dfs(int src,int aug)
{
    if(src==sink)return aug;
    int left=aug,mindis=nn;
    for(int j=head[src]; j!=-1; j=edg[j].next)
    {
        int v=edg[j].v;
        if(edg[j].w)
        {
            if(dis[v]+1==dis[src])
            {
                int minn=MIN(left,edg[j].w);
                minn=dfs(v,minn);
                edg[j].w-=minn;
                edg[j^1].w+=minn;
                left-=minn;
                if(dis[sourse]>=nn)return aug-left;
                if(left==0)break;
            }
            if(dis[v]<mindis)
                mindis=dis[v];
        }
    }
    if(left==aug)
    {
        if(!(--gap[dis[src]]))dis[sourse]=nn;
        dis[src]=mindis+1;
        gap[dis[src]]++;
    }
    return aug-left;
}
int sap(int s,int e)
{
    int ans=0;
    nn=e+1;
    memset(dis,0,sizeof(dis));
    memset(gap,0,sizeof(gap));
    gap[0]=nn;
    sourse=s;
    sink=e;
    while(dis[sourse]<nn)
        ans+=dfs(sourse,inf);
    return ans;
}
int build(int mid)
{
    memset(head,-1,sizeof(head));
    nodes=0;
    int s=0,t=n+1;
    for(int i=1;i<=n;i++)//将源点和gold相连
        addedge(s,i,gold[i]);
    for(int i=1;i<=n;i++)//将汇点和store相连
        addedge(i,t,store[i]);
    for(int i=1;i<=m;i++)//将距离小于mid的边相连
        if(c[i]<=mid)addedge(u[i],v[i],inf);
    int ans=sap(s,t);
    return ans==ans_gold;
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d",&n),n)
    {
        int maxx=-1,minn=inf;
        ans_gold=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&gold[i]);
            ans_gold+=gold[i];
        }
        for(int i=1;i<=n;i++)
            scanf("%d",&store[i]);
        scanf("%d",&m);
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d%d",&u[i],&v[i],&c[i]);
            maxx=max(maxx,c[i]);
            minn=min(minn,c[i]);
        }
        int l=minn,r=maxx,ans=0;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(build(mid)){r=mid-1;ans=mid;}//如果此长度满足,则继续缩小此长度
            else l=mid+1;
        }
        if(!ans)printf("No Solution\n");
        else printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值