HDU 3416 marriage match IV (spfa + maximum Stream)

Source: Internet
Author: User

The general meaning of the question is: give you some directed edge for you to find the given point S, the number of the shortest circuit between T.

The spfa on both sides are from S to T, and from t to S, and then the vertex on the shortest road is obtained to build an edge with a capacity of 1, and then the maximum traffic from S to T is obtained, is the number of the most short circuits.

PS: the code writing posture is not beautiful enough.

Marriage match IV Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2051 accepted submission (s): 608


Problem descriptiondo not sincere non-interference.
Like that show, now starvae also take part in a show, but it take place between City A and B. starvae is in City A and girls are in City B. every time starvae can get to City B and make a data with a girl he likes. but there are two problems with it, one is starvae must get to B within least time, it's said that he must take a shortest path. other is no road can be taken more than once. while the city starvae passed away can been taken more than once.


So, under a good RP, starvae may have chances to get to City B. but he don't know how many chances at most he can make a data with the girl he likes. cocould you help starvae?
Inputthe first line is an integer t indicating the case number. (1 <= T <= 65)
For each case, there are two integer N and m in the first line (2 <= n <= 1000, 0 <= m <= 100000 ), n is the number of the City and m is the number of the roads.

Then follows M line, each line have three integers A, B, C, (1 <= A, B <= N, 0 <C <= 1000) it means there is a road from A to B and it's distance is C, while there may have no road from B to. there may have a road from A to A, but you can ignore it. if there are two roads from A to B, they are different.

At last is a line with two integer a and B (1 <= A, B <= n,! = B), means the number of city a and City B.
There may be some blank line between each case.
Outputoutput a line with a integer, means the chances starvae can get at most.
Sample Input
37 81 2 11 3 12 4 13 4 14 5 14 6 15 7 16 7 11 76 71 2 12 3 11 3 33 4 13 5 14 6 15 6 11 62 21 2 11 2 21 2
 
Sample output
211
# Include <algorithm> # include <iostream> # include <stdlib. h> # include <string. h> # include <iomanip> # include <stdio. h> # include <string> # include <queue> # include <cmath> # include <stack> # include <map> # include <set> # define EPS 1e-12 ///# define M 1000100 # define ll _ int64 // # define ll long // # define INF 0x7ffffff # define INF 0x3ffffff # define PI 3.1415926535898 # define zero (X) (FABS (x) <EPS )? 0: X) using namespace STD; const int maxn = 2010; int CNT; int n, m; int cur [maxn], head [maxn]; int dis [maxn], gap [maxn]; int Aug [maxn], pre [maxn]; struct node {int V, W; int next;} f [510000]; int head1 [maxn]; int head2 [maxn]; int cnt1; int cnt2; struct node1 {int U, V, W; int next ;}; node1 PP [500010], FF [500010]; void Init () {CNT = 0; cnt1 = 0; cnt2 = 0; memset (head1,-1, sizeof (head1); memset (Head,-1, sizeof (H EAD); memset (head2,-1, sizeof (head2);} void Add1 (int u, int V, int W) {PP [cnt1]. U = u; PP [cnt1]. V = V; PP [cnt1]. W = W; PP [cnt1]. next = head1 [u]; head1 [u] = cnt1 ++;} void Add2 (int u, int V, int W) {FF [cnt2]. U = u; FF [cnt2]. V = V; FF [cnt2]. W = W; FF [cnt2]. next = head2 [u]; head2 [u] = cnt2 ++;} void add (int u, int V, int W) {f [CNT]. V = V; F [CNT]. W = W; F [CNT]. next = head [u]; head [u] = CNT ++; F [CNT]. V = U; F [CNT]. W = 0; F [CNT]. next = head [v]; head [v] = CNT ++;} int SAP (int s, int e, int N) {int max_flow = 0, V, U = s; int ID, mindis; Aug [s] = inf; Pre [s] =-1; memset (DIS, 0, sizeof (DIS); memset (gap, 0, sizeof (GAP); Gap [0] = N; For (INT I = 0; I <= N; ++ I) cur [I] = head [I]; // initialize the current arc as the first arc while (DIS [s] <n) {bool flag = false; if (u = e) {max_flow + = Aug [E]; for (V = pre [E]; V! =-1; V = pre [v]) // path tracing updates residual network {id = cur [v]; F [ID]. w-= Aug [E]; F [ID ^ 1]. W + = Aug [E]; Aug [v]-= Aug [E]; // modify the augmented quantity. If (F [ID] will be used in the future. W = 0) u = V; // do not roll back to the source point, only roll back to the arc end of the arc with the capacity of 0} For (ID = cur [u]; ID! =-1; id = f [ID]. next) // start from the current arc to find the allowable arc {v = f [ID]. v; If (F [ID]. w> 0 & dis [u] = dis [v] + 1) // find the allowable arc {flag = true; Pre [v] = u; cur [u] = ID; Aug [v] = min (Aug [u], F [ID]. w); U = V; break;} If (flag = false) {If (-- gap [dis [u] = 0) break; /// gap optimization. When a fault occurs in the hierarchy tree, the end algorithm mindis = N; cur [u] = head [u]; for (ID = head [u]; ID! =-1; id = f [ID]. next) {v = f [ID]. v; If (F [ID]. w> 0 & dis [v] <mindis) {mindis = dis [v]; cur [u] = ID; /// modify the current arc while modifying the label} dis [u] = mindis + 1; Gap [dis [u] ++; If (u! = S) u = pre [u]; // trace back to continue searching for allowed arcs} return max_flow;} int D1 [maxn]; int vis [maxn]; queue <int> FP; void spfa1 (INT s) {for (INT I = 0; I <= N; I ++) D1 [I] = inf; while (! FP. empty () FP. pop (); memset (VIS, 0, sizeof (VIS); vis [s] = 1; FP. push (s); D1 [s] = 0; while (! FP. empty () {int x = FP. front (); vis [x] = 0; FP. pop (); For (INT I = head1 [X]; I! =-1; I = PP [I]. next) {int v = PP [I]. v; If (d1 [v]> d1 [x] + PP [I]. w) {D1 [v] = D1 [x] + PP [I]. w; If (! Vis [v]) {FP. push (V); vis [v] = 1 ;}}}} int D2 [maxn]; void spfa2 (INT s) {for (INT I = 0; I <= N; I ++) D2 [I] = inf; while (! FP. empty () FP. pop (); memset (VIS, 0, sizeof (VIS); vis [s] = 1; FP. push (s); d2 [s] = 0; while (! FP. empty () {int x = FP. front (); FP. pop (); vis [x] = 0; For (INT I = head2 [X]; I! =-1; I = FF [I]. next) {int v = FF [I]. v; If (D2 [v]> D2 [x] + FF [I]. w) {d2 [v] = d2 [x] + FF [I]. w; If (! Vis [v]) {FP. push (V); vis [v] = 1 ;}}} int main () {int K; CIN> K; while (k --) {Init (); scanf ("% d", & N, & M); int X, Y; int U, V, W; For (INT I = 0; I <m; I ++) {scanf ("% d", & U, & V, & W); Add1 (U, V, W); Add2 (V, u, W) ;}scanf ("% d", & X, & Y); spfa1 (x); spfa2 (y); For (INT I = 0; I <cnt1; I ++) if (d1 [PP [I]. u] + D2 [PP [I]. v] + PP [I]. W = D1 [y]) add (PP [I]. u, PP [I]. v, 1); int ans = SAP (X, Y, n); cout <ans <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.