G-island transport-hdu 4280 (maximum flow)

Source: Internet
Author: User

Test instructions: There are N Islands, M route, each road connected to two islands, and each road has a maximum number of people, now want to know from the westernmost island to the most east of the island can have the maximum number of persons in the past (the most western and eastern Islands only one).

Analysis: Can be more obvious to see is a maximum flow problem, and the source point sinks are also given, you can use the most basic dinic solution, but note some optimization, will be tle, the following is dinic AC code =========================================================================================================== =#pragmaComment (linker, "/stack:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
usingnamespaceStd

ConstintMAXN =100005;
ConstintOO = 1e9+7;

structedge{intV, flow, next;} edge[maxn<<1];
intHEAD[MAXN], CNT;
intLAYER[MAXN];

voidInIt (intN
{
CNT =0;
memset (Head,-1,sizeof(Head));
}
voidAddedge (intUintVintFlow
{
EDGE[CNT].V = v;
Edge[cnt].flow = flow;
Edge[cnt].next = Head[u];
Head[u] = cnt++;
}
BOOLBFsintStartintEND)
{
queue<int>Q; Q.push (start);
memset (layer,0,sizeof(layer));
Layer[start] =1;

while(Q.size ())
{
intU = Q.front (); Q.pop ();

if(U = = End)returntrue;

for(intJ=head[u]; j!=-1; J=edge[j].next)
{
intv = edge[j].v;

if(Layer[v] = =false&& Edge[j].flow)
{
LAYER[V] = Layer[u] +1;
Q.push (v);
}
}
}

returnfalse;
}
intDfsintUintMaxflow,intEND)
{
if(U = = End)returnMaxflow;

intUflow =0;

for(intJ=head[u]; j!=-1; J=edge[j].next)
{
intv = edge[j].v;

if(layer[v]-1= = Layer[u] && edge[j].flow)
{
intflow = min (Maxflow-uflow, edge[j].flow);
Flow = DFS (V, Flow, End);

Edge[j].flow-= flow;
edge[j^1].flow + = flow;

Uflow + = flow;

if(Uflow = = Maxflow)
Break;
}
}

if(Uflow = =0)///optimized to prevent re-search, no optimization will time out
Layer[u] =0;

returnUflow;
}
intDinic (intStartintEnd,intN
{
intMaxflow =0;

while(BFS (start, End) = =true)
Maxflow + = DFS (start, oo, End);

returnMaxflow;
}
intMain ()
{
intT

scanf"%d", &t);

while(t--)
{
intI, N, M, X, Y, East=-oo, West=oo, start, End;

scanf"%d%d", &n, &m);

InIt (N);

for(i=1; i<=n; i++)
{
scanf"%d%d", &x, &y);

if(West > x)
{
West = x;
start = i;
}
if(East < X)
{
east = x;
End = i;
}
}

while(m--)
{
intU, V, flow;

scanf"%d%d%d", &u, &v, &flow);
Addedge (U, V, flow);
Addedge (V, U, flow);
}

printf"%d\n", Dinic (Start, End, N));
}

return0;
}View Code

G-island transport-hdu 4280 (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.