POJ 2135 fee flow basis

Source: Internet
Author: User
Tags acos

Question: give N vertices and M edges, and ask how many shortest paths are going back and forth from 1 to N, and one edge can only go through once.

You can directly repeat the billing flow. However, you need to pay attention to the initial values in the questions and make several WA contributions.


[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <algorithm>
# Include <string>
# Include <cmath>
# Include <cstring>
# Include <queue>
# Include <set>
# Include <vector>
# Include <stack>
# Include <map>
# Include <iomanip>
# Define PI acos (-1.0)
# Deprecision Max 100005
# Define inf 2000000000 // initially inf = 1 <28 but WA, so note that the initial value is large.
# Define LL (x) (x <1)
# Define RR (x) (x <1 | 1)
# Define REP (I, s, t) for (int I = (s); I <= (t); ++ I)
# Define ll long
# Define mem (a, B) memset (a, B, sizeof ())
# Define mp (a, B) make_pair (a, B)
Using namespace std;
Inline void readint (int & ret)
{
Char c;
Do
{
C = getchar ();
}
While (c <'0' | c> '9 ');
Ret = c-'0 ';
While (c = getchar ()> = '0' & c <= '9 ')
Ret = ret * 10 + (c-'0 ');
}
Int n, m;
Struct kdq
{
Int s, e, l, c, next;
} Ed [2, 1000005];
Int head [10005], num;
Void add (int s, int e, int l, int c)
{
Ed [num]. s = s;
Ed [num]. e = e;
Ed [num]. l = l;
Ed [num]. c = c;
Ed [num]. next = head [s];
Head [s] = num ++;
Ed [num]. s = e;
Ed [num]. e = s;
Ed [num]. l = 0;
Ed [num]. c =-c;
Ed [num]. next = head [e];
Head [e] = num ++;
}
Int S, T;
Void init ()
{
Mem (head,-1 );
Num = 0;
}
Int dis [Max];
Bool vis [Max];
Int qe [Max * 10];
Int pre [Max], path [Max];
Int spfa ()
{
REP (I, 0, T) dis [I] = inf, vis [I] = 0;
Dis [S] = 0;
Vis [S] = 1;
Int h = 0, t = 0;
Qe [h ++] = S;
While (h> t)
{
Int tt = qe [t ++];
Vis [tt] = 0;
For (int I = head [tt]; ~ I; I = ed [I]. next)
{
Int ee = ed [I]. e;
Int l = ed [I]. l;
Int c = ed [I]. c;
If (l> 0 & dis [ee]> dis [tt] + c)
{
Pre [ee] = tt;
Path [ee] = I;
Dis [ee] = dis [tt] + c;
If (! Vis [ee])
{
Vis [ee] = 1;
Qe [h ++] = ee;
}
}
}
}
Return dis [T]! = Inf;
}
Int MFMC ()
{
Int M = 0;
While (spfa ())
{
Int ff = inf;
For (int I = T; I! = S; I = pre [I])
Ff = min (ff, ed [path [I]. l );
For (int I = T; I! = S; I = pre [I])
Ed [path [I]. l-= ff, ed [path [I] ^ 1]. l + = ff;
M + = dis [T] * ff;
}
Return M;
}
Int main ()
{
# Ifndef ONLINE_JUDGE
Freopen ("acm.txt", "r", stdin );
# Endif
Init ();
Readint (n );
Readint (m );
S = 0, T = n + 1;
REP (I, 0, m-1)
{
Int a, B, c;
Readint ();
Readint (B );
Readint (c );
Add (a, B, 1, c );
Add (B, a, 1, c );
}
Add (S, 1, 2, 0 );
Add (n, T, 2, 0 );
Cout <MFMC () <endl;
Return 0;
}

# Include <iostream>
# Include <cstdio>
# Include <algorithm>
# Include <string>
# Include <cmath>
# Include <cstring>
# Include <queue>
# Include <set>
# Include <vector>
# Include <stack>
# Include <map>
# Include <iomanip>
# Define PI acos (-1.0)
# Deprecision Max 100005
# Define inf 2000000000 // initially inf = 1 <28 but WA, so note that the initial value is large.
# Define LL (x) (x <1)
# Define RR (x) (x <1 | 1)
# Define REP (I, s, t) for (int I = (s); I <= (t); ++ I)
# Define ll long
# Define mem (a, B) memset (a, B, sizeof ())
# Define mp (a, B) make_pair (a, B)
Using namespace std;
Inline void readint (int & ret)
{
Char c;
Do
{
C = getchar ();
}
While (c <'0' | c> '9 ');
Ret = c-'0 ';
While (c = getchar ()> = '0' & c <= '9 ')
Ret = ret * 10 + (c-'0 ');
}
Int n, m;
Struct kdq
{
Int s, e, l, c, next;
} Ed [2, 1000005];
Int head [10005], num;
Void add (int s, int e, int l, int c)
{
Ed [num]. s = s;
Ed [num]. e = e;
Ed [num]. l = l;
Ed [num]. c = c;
Ed [num]. next = head [s];
Head [s] = num ++;
Ed [num]. s = e;
Ed [num]. e = s;
Ed [num]. l = 0;
Ed [num]. c =-c;
Ed [num]. next = head [e];
Head [e] = num ++;
}
Int S, T;
Void init ()
{
Mem (head,-1 );
Num = 0;
}
Int dis [Max];
Bool vis [Max];
Int qe [Max * 10];
Int pre [Max], path [Max];
Int spfa ()
{
REP (I, 0, T) dis [I] = inf, vis [I] = 0;
Dis [S] = 0;
Vis [S] = 1;
Int h = 0, t = 0;
Qe [h ++] = S;
While (h> t)
{
Int tt = qe [t ++];
Vis [tt] = 0;
For (int I = head [tt]; ~ I; I = ed [I]. next)
{
Int ee = ed [I]. e;
Int l = ed [I]. l;
Int c = ed [I]. c;
If (l> 0 & dis [ee]> dis [tt] + c)
{
Pre [ee] = tt;
Path [ee] = I;
Dis [ee] = dis [tt] + c;
If (! Vis [ee])
{
Vis [ee] = 1;
Qe [h ++] = ee;
}
}
}
}
Return dis [T]! = Inf;
}
Int MFMC ()
{
Int M = 0;
While (spfa ())
{
Int ff = inf;
For (int I = T; I! = S; I = pre [I])
Ff = min (ff, ed [path [I]. l );
For (int I = T; I! = S; I = pre [I])
Ed [path [I]. l-= ff, ed [path [I] ^ 1]. l + = ff;
M + = dis [T] * ff;
}
Return M;
}
Int main ()
{
# Ifndef ONLINE_JUDGE
Freopen ("acm.txt", "r", stdin );
# Endif
Init ();
Readint (n );
Readint (m );
S = 0, T = n + 1;
REP (I, 0, m-1)
{
Int a, B, c;
Readint ();
Readint (B );
Readint (c );
Add (a, B, 1, c );
Add (B, a, 1, c );
}
Add (S, 1, 2, 0 );
Add (n, T, 2, 0 );
Cout <MFMC () <endl;
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.