HDU 3987 Harry Potter and the Forbidden Forest minimum cut

Source: Internet
Author: User

This question Nima makes my eggs very painful .. The first time I encountered this problem, I went straight to the largest stream and read the question for half a day .. Then you can find out.The minimum number of edges in the smallest cut set,

No idea. Then read the online solution .. Know how the great God after writing (http://hi.baidu.com/mengyun1993/blog/item/c30d193c9a85932870cf6cda.html ),,

When I write it myself, there is no way to AC .. I wrote a dinic, timed out, and then wrote ISAP, wa. It didn't take a long time .. Write an Ek .. The direct results are all merged ..

It really hurts .. You can't do it anymore .. Post my various errors belowCodeWhich of the following is critical ..

 

# Include <stdio. h>
# Include <iostream>
# Include <queue>
Using namespace STD;
Const int n= 210;
Const int INF = 0x7fffffff;
Int n, m, map [N] [N], path [N], flow [N], start, end;
Queue <int> q;

 

Int BFS ()
{
Int I, T;
While (! Q. Empty ())
Q. Pop ();
Memset (path,-1, sizeof (PATH ));
Path [start] = 0, flow [start] = inf;
Q. Push (start );
While (! Q. Empty ())
{
T = Q. Front ();
Q. Pop ();
If (t = END) break;
For (I = 1; I <= m; I ++)
{

 

If (I! = Start & path [I] =-1 & map [T] [I])
{
Flow [I] = flow [T] <map [T] [I]? Flow [T]: Map [T] [I];
Q. Push (I );
Path [I] = T;
}
}
}
If (path [end] =-1) Return-1;
Return Flow [m]; // The traffic increment after one Traversal
}
Int edmonds_karp ()
{
Int max_flow = 0, step, now, pre;
While (step = BFS ())! =-1)
{// Exit when the path for adding is not found
Max_flow + = step;
Now = end;
While (now! = Start)
{
Pre = path [now];
Map [pre] [now]-= step; // update the actual capacity of the forward edge
Map [now] [pre] + = step; // Add a reverse edge
Now = pre;
}
}
Return max_flow;
}
Int main ()
{
Int CAS;
Int I, U, V, cost, D;
Int case = 0;
While (scanf ("% d", & CAS )! = EOF)
{
While (CAS --)
{
Scanf ("% d", & N, & M );
Memset (MAP, 0, sizeof (MAP ));
For (I = 0; I <m; I ++)
{
Scanf ("% d", & U, & V, & Cost, & D );
Map [u] [v] = Cost * 100001 + 1; // not just only one input
If (D = 1)
Map [v] [u] = Cost * 100001 + 1;
}
Start = 0, end = n-1;
_ Int64 ans = edmonds_karp ();
Printf ("case % d: % i64d \ n", ++ case, ANS % 100001 );
}
}
Return 0;
}
/*
# Include <cstdio>
# Include <iostream>
# Include <cstring>
# Include <queue>
# Include <algorithm>
# Define n 20001
# Define INF 99999999
Using namespace STD;

 

Int S, T, DIS [N], pre [N], gap [N], flow [N] [N];
Struct edge
{
Int V, W;
Edge * Next, * Rev;
Edge () {next = 0 ;}
Edge (int vv, int WW, edge * E) {v = vv; W = WW; next = E ;}
} * Adj [N], * path [N], * E;
Int min (int x, int y)
{
If (x> Y)
Return y;
Else
Return X;
}
Void insert (int u, int V, int W)
{
Edge * P = new edge (V, W, adj [u]);
Adj [u] = P;
Edge * q = new edge (u, 0, adj [v]);
Adj [v] = Q;
P-> REV = Q;
Q-> REV = P;
}
Void BFS ()
{
Memset (DIS, 0x7f, sizeof (DIS ));
Memset (GAP, 0, sizeof (GAP ));
Queue <int> q;
Dis [T] = 0;
Gap [0] = 1;
Q. Push (t );
While (Q. Size ())
{
Int x = Q. Front ();
Q. Pop ();
For (E = adj [X]; E = e-> next)
{
If (e-> rev-> W = 0 | dis [E-> V] <t)
Continue;
Dis [E-> V] = dis [x] + 1;
++ Gap [dis [E-> V];
Q. Push (e-> V );
}
}
}
Int ISAP ()
{
Memset (DIS, 0, sizeof (DIS ));
Memset (GAP, 0, sizeof (GAP ));
// BFS ();
Int ans = 0, u = s, D;
While (DIS [s] <= T)
{
If (u = T)
{
Int minflow =-1u> 1;
For (E = path [u]; u! = S; E = path [U = pre [u])
Minflow = min (minflow, e-> W );
For (E = path [U = T]; u! = S; E = path [U = pre [u])
{
E-> W-= minflow;
E-> rev-> W + = minflow;
Flow [pre [u] [u] + = minflow;
Flow [u] [pre [u]-= minflow;
}
Ans + = minflow;
}
For (E = adj [u]; E = e-> next)
If (e-> W> 0 & dis [u] = dis [E-> V] + 1)
Break;
If (E)
{
Pre [E-> V] = u;
Path [E-> V] = E;
U = e-> V;
}
Else
{
If (-- gap [dis [u] = 0)
Break;
For (D = T, E = adj [u]; E = e-> next)
If (e-> W> 0)
D = min (D, DIS [E-> V]);
Dis [u] = d + 1;
++ Gap [dis [u];
If (u! = S)
U = pre [u];
}
}
Return ans;
}
Int main ()
{
Int NN, mm;
Int CAS;
Int U, V, W, D;
Int case;
While (scanf ("% d", & CAS )! = EOF)
{
Case = 0;
While (CAS --)
{
Scanf ("% d", & NN, & mm );
Memset (adj, 0, sizeof (adj ));
While (Mm --)
{
Scanf ("% d", & U, & V, & W, & D );
Insert (U, V, W * 100001 + 1 );
If (D = 1)
Insert (v, U, w * 100001 + 1 );
}
S = 0;
T = nn-1;
_ Int64 ans = ISAP ();
Printf ("case % d: % i64d \ n", ++ case, ANS % 100001 );
}
}
Return 0;
}
/*
# Include <cstdio>
# Include <cstring>
# Include <iostream>
# Include <algorithm>
# Define n 5005
# Define M 10005 // This should be large enough ..
# Define INF 999999999
Using namespace STD;

Int n, m, S, T, num, adj [N], DIS [N], Q [N];
Struct edge
{
Int V, W, pre;
} E [m];
Int min (int x, int y)
{
If (x> Y)
Return y;
Else
Return X;
}
Void insert (int u, int V, int W) // The Insert above can be written as follows ..
{
E [num]. V = V;
E [num]. W = W;
E [num]. Pre = adj [u];
Adj [u] = num ++;
E [num]. V = u;
E [num]. W = 0;
E [num]. Pre = adj [v];
Adj [v] = num ++;

}
Int BFS ()
{
Int I, X, V, tail = 0, head = 0;
Memset (DIS, 0, sizeof (DIS ));
Dis [s] = 1;
Q [tail ++] = s;
While (Head <tail)
{
X = Q [head ++];
For (I = adj [X]; I! =-1; I = E [I]. PRE)
If (E [I]. W & dis [V = E [I]. V] = 0)
{
Dis [v] = dis [x] + 1;
If (V = T)
Return 1;
Q [tail ++] = V;
}
}
Return 0;
}
Int DFS (int s, int limit)
{
If (S = T)
Return limit;
Int I, V, TMP, cost = 0;
For (I = adj [s]; I! =-1; I = E [I]. PRE)
If (E [I]. W & dis [s] = dis [V = E [I]. V]-1)
{
TMP = DFS (v, min (Limit-cost, E [I]. W ));
If (TMP> 0)
{
E [I]. W-= TMP;
E [I ^ 1]. W + = TMP;
Cost + = TMP;
If (Limit = cost)
Break;
}
Else dis [v] =-1;
}
Return cost;
}
Int dinic ()
{
Int ans = 0;
While (BFS ())
Ans + = DFS (S, INF );
Return ans;
}
Int main ()
{
Int NN, mm;
Int CAS;
Int U, V, D;
Int case;
_ Int64 W;
While (scanf ("% d", & CAS )! = EOF)
{
Case = 0;
For (INT I = 0; I <CAS; I ++)
{
Scanf ("% d", & NN, & mm );
Num = 0;
Memset (adj,-1, sizeof (adj ));
For (Int J = 0; j <mm; j ++)
{
Scanf ("% d % i64d % d", & U, & V, & W, & D );
U ++; V ++;
Insert (U, V, W * 1000001 + 1 );
If (D = 1)
Insert (v, U, w * 1000001 + 1 );
}
S = 1;
T = nn;
_ Int64 ans = dinic ();
Printf ("case % d: % i64d \ n", Case ++, ANS % 1000001 );
}
}
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.