POJ2455 Secret Milking Machine "two points, maximum flow"

Source: Internet
Author: User

The main idea: N points P-bar edge, so that there is a path from 1 to N of the T bar, the maximum value of the Benquan on the path to the minimum number

Idea: Do a lot of two points + the maximum flow of the problem, the idea is very good out of two the largest side of the right to build a map, run Dinic

The problem is .... This problem is a good question of the card constant!!!!!

T after 8 hair really can't stand, aimed at the eyes of the online program, Uniform adjacency matrix .... On the superiority of adjacency matrix

But not superstitious I finally repeated the optimization constants after the adjacency table a

Program for Tle

#include <stdio.h>

#include <iostream>

#include <string.h>

#include <algorithm>

#include <queue>

#define MAXN 200090

#define ESP 0.001

#define INF 0x3f3f3f3f

using namespace Std;

int head[300],next[maxn],point[maxn],now=0;

int flow[maxn],dist[300];

int tt,p,h=0,n;

struct T

{

int X;int Y;int v;

}A[MAXN];

void Add (int x,int Y,int v)

{

NEXT[++NOW]=HEAD[X];

Head[x]=now;

Point[now]=y;

Flow[now]=v;

Next[++now]=head[y];

Head[y]=now;

Point[now]=x;

flow[now]=0;

}

int BFS (int s,int t,int x)

{

queue<int>q;

Q.push (s);

memset (dist,-1,sizeof (Dist));

dist[s]=0;

while (!q.empty ())

{

int U=q.front ();

Q.pop ();

for (int i=head[u];i;i=next[i])

{

int k=point[i];

if (flow[i]!=0&&dist[k]==-1)

{

dist[k]=dist[u]+1;

Q.push (k);

}

}

}

return dist[t]!=-1;

}

int dfs (int s,int d,int t,int x)

{

if (s==t) return D;

int res=0;

for (int i=head[s];i&&res<d;i=next[i])

{

int u=point[i];

if (flow[i]&&dist[u]==dist[s]+1)

{

int Dd=dfs (U,min (flow[i],d-res), t,x);

if (DD)

{

FLOW[I]-=DD;

flow[((i-1) ^1) +1]+=dd;

RES+=DD;

}

}

}

if (res==0) dist[s]=-1;

return res;

}

int judge (int x,int s,int t)

{

int ans=0;

memset (head,0,sizeof (head));

now=0;

for (int i=1;i<=p;i++) if (a[i].v<=x)

{

Add (a[i].x,a[i].y,1);

Add (a[i].y,a[i].x,1);

}

Add (S,1,tt); add (N,t,inf);

while (BFS (s,t,x))

{Ans+=dfs (s,inf,t,x);}

if (ANS>=TT) return 1;else return 0;

}

int read ()

{

int X=0,f=1;char Ch=getchar ();

while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}

while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();}

return x*f;

}

int main ()

{

int x,y,v;

int l=0x3f3f3f3f,r=0,mid;

scanf ("%d%d%d", &N,&P,&TT);

int s=n+10,t=n+12;

for (int i=1;i<=p;i++)

{

X=read (); Y=read (); V=read ();

A[i].x=x;a[i].y=y;a[i].v=v;

R=max (R,V);

L=min (L,V);

}

while (mid= (l+r) >>1,l<r)

{

if (judge (mid,s,t) ==1) R=mid;else l=mid+1;

}

printf ("%d\n", R);

return 0;

}

The AC program

#include <stdio.h>

#include <iostream>

#include <string.h>

#include <algorithm>

#include <queue>

#define MAXN 200090

#define ESP 0.001

#define INF 0x3f3f3f3f

using namespace Std;

int head[300],next[maxn],point[maxn],now=0;

int flow[maxn],dist[300];

int tt,p,h=0,n;

struct T

{

int X;int Y;int v;

}A[MAXN];

void Add (int x,int Y,int v)

{

NEXT[++NOW]=HEAD[X];

Head[x]=now;

Point[now]=y;

Flow[now]=v;

Next[++now]=head[y];

Head[y]=now;

Point[now]=x;

flow[now]=0;

}

int BFS (int s,int t,int x)

{

queue<int>q;

Q.push (s);

memset (dist,-1,sizeof (Dist));

dist[s]=0;

while (!q.empty ())

{

int U=q.front ();

Q.pop ();

for (int i=head[u];i;i=next[i])

{

int k=point[i];

if (flow[i]!=0&&dist[k]==-1)

{

dist[k]=dist[u]+1;

Q.push (k);

}

}

}

return dist[t]!=-1;

}

int dfs (int s,int d,int t,int x)

{

if (s==t) return D;

int res=0;

for (int i=head[s];i&&res<d;i=next[i])

{

int u=point[i];

if (flow[i]&&dist[u]==dist[s]+1)

{

int Dd=dfs (U,min (flow[i],d-res), t,x);

if (DD)

{

FLOW[I]-=DD;

flow[((i-1) ^1) +1]+=dd;

RES+=DD;

}

}

}

if (res==0) dist[s]=-1;

return res;

}

int judge (int x,int s,int t)

{

int ans=0;

memset (head,0,sizeof (head));

now=0;

for (int i=1;i<=p;i++) if (a[i].v<=x)

{

Add (a[i].x,a[i].y,1);

Add (a[i].y,a[i].x,1);

}

Add (S,1,tt); add (N,t,inf);

while (BFS (s,t,x))

{Ans+=dfs (s,inf,t,x);}

if (ANS>=TT) return 1;else return 0;

}

int read ()

{

int X=0,f=1;char Ch=getchar ();

while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}

while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();}

return x*f;

}

int main ()

{

int x,y,v;

int l=0x3f3f3f3f,r=0,mid;

scanf ("%d%d%d", &N,&P,&TT);

int s=n+10,t=n+12;

for (int i=1;i<=p;i++)

{

X=read (); Y=read (); V=read ();

A[i].x=x;a[i].y=y;a[i].v=v;

R=max (R,V);

L=min (L,V);

}

while (mid= (l+r) >>1,l<r)

{

if (judge (mid,s,t) ==1) R=mid;else l=mid+1;

}

printf ("%d\n", R);

return 0;

}

Finish is also drunk, not a don't sleep tut

POJ2455 Secret Milking Machine "two points, maximum 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.