码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-23 13:32:55      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:网络流   二分查找   前向星   c语言   

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;
}


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

标签:网络流   二分查找   前向星   c语言   

原文地址:http://blog.csdn.net/u013486414/article/details/43053005

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!