[Two points + has the maximum flow of the upper and lower bounds] zoj3496:assignment__ Graph theory-

Source: Internet
Author: User
Tags stdin
the

Company A wants to transport as many goods as possible from the city S to City T. So they ask company B to do the transportation. There are n cities the problem and There are directed from roads I to City J with cities capacity.

After a long negotiation, a and B reach a consensus that a A and a-decide to transport the goods (of course under the CO Ndition that most goods are transported and then the quantity transported in each road cannot exceed the capacity) and the N B can assign non-negative integer unit transportation costs to each road such this total unit transportation costs I n All roads sum to P. So the money A has to pay are the sum of costs of all roads while the cost of a certain road is the quantity transported (a Ssigned by A) multiplied by the transportation cost of the unit (assigned by B). Notice that since the goods are not divisible, the quantity to transported for each road to be a should integer.

Of course A wants to minimize the cost and B want to maximize the cost. So, are to work out what the total cost would be?

To make the problem more fun, your should also work out of the total cost this A wants to maximize the cost while B wants to M Inimize it and other conditions keep the same.
2≤n≤500, 0≤m≤10000, 0≤s, T < N, 0≤p≤100000

The title of the topic is more around, it is the flood problem.
In fact, B Company set the side right must be greedy only in the flow of the largest (small) plus p, and a company is to make the maximum flow (small) side of the flow of the smallest (large).
So the two-point answer, then the brush has the upper and lower bounds of the maximum flow verification can be.

#include <cstdio> #include <queue> #include <cstring> #include <algorithm> using namespace std;
    Inline Char GC () {static Char buf[100000],*p1=buf,*p2=buf; Return p1==p2&& (p2= (p1=buf) +fread (Buf,1,100000,stdin), P1==P2)?
eof:*p1++;
    inline int getint () {char ch=gc (); int res=0,ff=1; while (!) ('
    0 ' <=ch&&ch<= ' 9 ')) {if (ch== '-') ff=-1; CH=GC ();}
    while (' 0 ' <=ch&&ch<= ' 9 ') res= (res<<3) + (res<<1) +ch-' 0 ', CH=GC ();
return RES*FF;
const int maxn=605, maxe=40005;
int n,m,s,t,ss,tt,p,_test,ans,maxflow;
    struct edge{int from,to,flow,cap; Edge (int t1=0,int t2=0,int t3=0,int t4=0) {from=t1; to=t2; flow=t3; cap=t4;}}

Es[maxe];
struct data{int x,y,z;} A[maxe];
int fir[maxn],nxt[maxe],tot=1;
    void Add (int x,int y,int z) {Es[++tot]=edge (x,y,0,z); nxt[tot]=fir[x]; fir[x]=tot; Es[++tot]=edge (y,x,0,0); Nxt[tot]=fir[y];
Fir[y]=tot;
} queue< int > que;
int N,D[MAXN],POS[MAXN],TMP[MAXN]; BOOL Bfs (int S,int T) {memset (d,63,sizeof (d)); int inf=d[0]; Que.push (S); 
    d[s]=0;
        while (!que.empty ()) {int X=que.front (); Que.pop (); for (int j=fir[x];j;j=nxt[j]) if (d[es[j].to]==inf&&es[j].cap>es[j].flow) {d[es[j].to]=d[x]+1; que.pu
        SH (es[j].to);
} return D[t]!=inf; int Find_dfs (int x,int flow,int T) {if x==t| |
    flow==0) return flow;
    int res=0,t; for (int &j=pos[x];j;j=nxt[j]) {if (d[x]+1==d[es[j].to]&& T=find_dfs (Es[j].to,min (FLOW,ES[J].CAP-ES[J) . Flow), T) >0) {es[j].flow+=t; 
            es[j^1].flow-=t; res+=t; flow-=t;
        if (flow==0) break;
} return res;
    int dinic (int s,int T) {int res=0; 
        while (Bfs (s,t)) {for (int i=1;i<=n;i++) pos[i]=fir[i];
    Res+=find_dfs (s,1e+9,t);
return res;
    } void Getmaxflow () {memset (fir,0,sizeof fir); tot=1;
    for (int i=1;i<=m;i++) Add (A[I].X,A[I].Y,A[I].Z); N=n;
Maxflow=dinic (s,t); } void Del(int x)
{for (int j=fir[x];j;j=nxt[j]) es[j].flow=es[j].cap=es[j^1].flow=es[j^1].cap=0;}
    BOOL Check (int _l,int _r) {memset (fir,0,sizeof fir); tot=1; N=n; Ss=++n;
    Tt=++n;
    memset (tmp,0,sizeof tmp);
        for (int i=1;i<=m;i++) {int fl=_l, fr=min (_R,A[I].Z);
        if (FL&GT;FR) continue; Add (A[I].X,A[I].Y,FR-FL); TMP[A[I].X]-=FL;
    TMP[A[I].Y]+=FL;
    int blc=0;
                for (int i=1;i<=n;i++) {if (tmp[i]>0) Add (Ss,i,tmp[i]), blc+=tmp[i]; 
    else Add (i,tt,-tmp[i]);
    Add (t,s,1e+9);
    if (Dinic (SS,TT) &LT;BLC) return false;
    int res=es[tot-1].flow; Es[tot].flow=es[tot].cap=es[tot-1].flow=es[tot-1].cap=0; Del (SS);
    Del (TT);
Return Res+dinic (s,t) ==maxflow;
    int main () {freopen ("zoj3496.in", "R", stdin);
    Freopen ("Zoj3496.out", "w", stdout);
    _test=getint (); while (_test--) {n=getint (); M=getint (); S=getint () +1; T=getint () +1;
        P=getint (); for (int i=1;i<=m;i++) a[i].x=getint () +1, A[i].y=getInt () +1, A[i].z=getint ();
        Getmaxflow (); int l=0,r=1000000;
        ans=0;
            while (l<=r) {int mid= (L+R) >>1;
                        if (check (0,mid)) r=mid-1, Ans=mid;
        else l=mid+1;
        printf ("%lld", (Long Long) ans *p); l=0,r=1000000;
        ans=0;
            while (l<=r) {int mid= (L+R) >>1;
                           if (check (mid,1e+9)) l=mid+1, Ans=mid;
        else r=mid-1;
    printf ("%lld\n", (Long Long) ans *p);
return 0; }

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.