POJ 3228-gold Transportation (Network flow _ Max Stream + binary search)

Source: Internet
Author: User

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

Description

Recently, a number of gold mines has been discovered in zorroming state. To protect this treasure, we must transport this gold to the storehouses as quickly as possible. Suppose the zorroming state consists of N towns and there is M bidirectional roads among these towns. The gold mines is only discovered in parts of the towns, while the storehouses is also owned by parts of the towns. The storage of the gold mine and storehouse is finite. The truck drivers in the zorroming state is famous for their bad temper that they would do like to drive all the time an D They need a bar and an inn available in the 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 was safely transported to the storehouses.

Input

The input contains several test cases. For each case, the first line is an integer N (1<=n<=200). The second line was N integers associated with the storage of the gold mine in every towns. The third line was also N integers associated with the storage of the storehouses in every towns. Next is an integer M (0<=m<= (n-1) *N/2). Then M lines follow. Each of the three integers x y and D (1<=x,y<=n,0<d<=10000), means that there are a road between x and Y for di Stance of D. N=0 means end of the input.

Output

For each case, output the minimum of the maximum adjacent distance on the condition so all the gold have been transported To the storehouses or "No solution".

Sample Input

43 2 0 00 0 3 361 2 41 3 101 4 122 3 62 4 83 4 50

Sample Output

6

Test instructions: There are n towns, some towns have gold mines, and some towns have vaults. The second line has n number, representing the deposit of the gold deposit in the I town, the third row has n number, represents the capacity of the vault in the I town. Next there is m bidirectional path, each line input three number u,v,w, represents the distance between U and V W. The shortest distance in the longest contiguous distance is required.

Idea: This problem is because it requires the adjacent distance, so there is no need to split the point. Build a super source point and a super meeting point, connect the gold mine with the source point, and connect the vault to the sink point. Then the two points the maximum distance, less than two points of the distance of the edge, and finally determine whether full stream.

#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&GT;=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]&LT;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) BR        Eak            } 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 (Network flow _ Max Stream + binary search)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.