POJ 3228 Gold Transportation two points + max flow

Source: Internet
Author: User
Tags getflow

http://poj.org/problem?id=3228

Test instructions: There are n cities, each city has a certain amount of gold, we need to put all the gold in the city's warehouses, there are some roads connected to these cities, each road has its own cost, the demand is that all the gold is transported to the warehouse, the road to go, where the maximum cost of the minimum is how much.

Thinking, two-point answer, the cost is less than the answer of those roads to build a diagram, and then establish a source point and all cities connected, traffic is the number of gold per city, and then set up a meeting point, connecting all cities, traffic is the maximum amount of gold in each city. Run the maximum flow again, if the flow equals all the gold, then you can, otherwise you can't.


#include <cstdio>
#include <cstring>
#include <iostream>
using namespace Std;


const int INF=1E9;
struct side{
int from,to,val;
}SIDE1[20010];
struct side2{
int to,val,next;
}SIDE2[40010];
int n,m;
int a[210],b[210];
int cnt,dis[210],gap[210];
int node[210],top;
int sum;


void addside (int a,int b,int c) {
Side2[top]= (Side2) {b,c,node[a]};
node[a]=top++;
Side2[top]= (Side2) {a,0,node[b]};
node[b]=top++;
}


int getflow (int u,int f) {
if (u==n+1)
return F;
int ans=0;
for (int i=node[u];i!=-1;i=side2[i].next) {
int to=side2[i].to,c=side2[i].val;
if (dis[u]>dis[to]&&c) {
int X=getflow (To,min (f-ans,c));
Side2[i].val-=x;
Side2[i^1].val+=x;
Ans+=x;
if (ans==f) return ans;
}
}
gap[dis[u]]--;
if (gap[dis[u]]==0) dis[0]=cnt+2;
dis[u]++;
gap[dis[u]]++;
return ans;
}


bool OK (int x) {
cnt=n+2;
memset (dis,0,sizeof (dis));
memset (Gap,0,sizeof (GAP));
memset (node,-1,sizeof (node));
gap[0]=cnt;
Top=0;
for (int i=1;i<=n;i++) {
Addside (0,i,a[i]);
Addside (I,n+1,b[i]);
}
for (int i=0;i<m;i++) if (side1[i].val<=x) {
Addside (Side1[i].from,side1[i].to,inf);
Addside (Side1[i].to,side1[i].from,inf);
}
int ans=0;
while (dis[0]<cnt) Ans+=getflow (0,inf);
if (ans==sum)
return 1;
else return 0;
}




int main ()
{
while (Cin>>n) {
sum=0;
if (n==0) break;
for (int i=1;i<=n;i++) {
scanf ("%d", &a[i]);
Sum+=a[i];
}
for (int i=1;i<=n;i++)
scanf ("%d", &b[i]);
cin>>m;
int x, y, Z;
int l=1e9,r=0,mid;
for (int i=0;i<m;i++) {
scanf ("%d%d%d", &x,&y,&z);
Side1[i]= (Side) {x, y, z};
L=min (L,Z);
R=max (R,Z);
}
r++;
int maxx=r;
while (l!=r) {
Mid= (L+R)/2;
if (OK (mid))
R=mid;
else l=mid+1;
}
if (L==maxx)
cout<< "No Solution" <<endl;
else cout<<l<<endl;
}
return 0;
}


POJ 3228 Gold Transportation two points + max flow

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.