Marriage Match IV HDU-3416

Source: Internet
Author: User
Tags min
Test Instructions

Give you n points, M edge, the number of the shortest path of s to t that requires only one pass per side

In my WA and tle also re, Yyb told me to run two times SPFA, also said I wrote again SPFA is wrong, however

A slap in the face ...
And he's

Run slower than me, 2333
Now let's talk about the method
First time SPFA (or Dijkstra) from S run over to all points of the shortest, re-build the map for each pair of U, v if dis[u] + w[u][v] = = Dis[v] Then add this side, capacity of 1 (plus reverse edge), the last run maximum flow can, the maximum flow I use is dinic, Then pay attention to the hand-beating queue, the system will tle

Changshu huge Ugly code

# include <bits/stdc++.h> # define RG Register # define IL Inline # define LL Long Long # define MEM (A, B) memset (A, b, sizeof (a)) # define Min (A, B) ((a) > (b))? (b): (a) # define MAX (A, B) (((a) < (b))?

(b): (a) using namespace Std; IL int Get () {RG char c = '! ';
    RG int x = 0, z = 1; for (C > ' 9 ' | | C < ' 0 '; c = GetChar ()) z = c = = '-'?
    -1:1;
    for (; C <= ' 9 ' && C >= ' 0 '; c = GetChar ()) x = x * + C-' 0 ';
return x * z;
} const int MAXN = 1001, Maxm = 200001, INF = 2147483647;
int n, M, FT[MAXN], cnt, ans, DIS[MAXN], VIS[MAXN], _FT[MAXN], LEVEL[MAXN], Q[MAXM];

struct edge{int to, F, NT;} EDGE[MAXM], _EDGE[MAXM];

IL void Add (RG int u, RG int V, RG int f) {edge[cnt] = (edge) {V, F, ft[u]}; ft[u] = cnt++;}

IL void Add2 (RG int u, RG int V, RG int f) {_edge[cnt] = (edge) {V, F, _ft[u]}; _ft[u] = cnt++;}
    IL void SPFA (RG int S, RG int T) {RG int head = 0, tail = 0; Q[0] = S; Vis[s] = 1;
    Dis[s] = 0; WhiLe (head <= tail) {RG int u = q[head++]; Vis[u] = 0;
            for (RG int e = Ft[u]; E! =-1; e = Edge[e].nt) {RG int v = edge[e].to, F = edge[e].f + Dis[u];
                if (Dis[v] > F) {dis[v] = f;
            if (!vis[v]) vis[v] = 1, q[++tail] = v;
    }}}} IL bool Bfs (RG int S, RG int T) {mem (level, 0);
    RG int head = 0, tail = 0; Q[0] = S;
    Level[s] = 1;
        while (head <= tail) {RG int u = q[head++];
        if (U = = T) return 1;
            for (RG int e = _ft[u]; E! =-1; e = _edge[e].nt) {RG int v = _edge[e].to, f = _edge[e].f;
                if (f &&!level[v]) {Level[v] = Level[u] + 1;
            Q[++tail] = v;
}}} return 0;
    } IL int Dfs (RG int u, RG int T, RG int maxf) {if (U = = T) return MAXF;
    RG int res = 0;
        for (RG int e = _ft[u]; E! =-1; e = _edge[e].nt) {RG int v = _edge[e].to, f = _edge[e].f; if (leVel[u] + 1 = = Level[v] && f) {f = Dfs (V, T, Min (f, maxf-res)); _EDGE[E].F-= f;
            _edge[e ^ 1].f + = f;
            Res + = f;
        if (res = = MAXF) break;
}} return res;
    } int main () {RG int T = Get ();
        while (t--) {n = get (); m = Get (); Mem (ft,-1); Mem (DIS, 63); Mem (_ft,-1);
        Ans = cnt = 0;
            for (RG int i = 1; I <= m; i++) {RG int u = Get (), V = Get (), F = Get ();
            if (U = = v) continue;
        ADD (U, V, f); } RG int S = Get (), T = Get ();
        CNT = 0;
        SPFA (S, T); for (RG int i = 1, i <= N; i++) for (RG int e = ft[i]; E! =-1; e = Edge[e].nt) if (Dis[i] + E
        DGE[E].F = = Dis[edge[e].to]) Add2 (i, edge[e].to, 1), ADD2 (edge[e].to, I, 0);
        while (Bfs (s, T)) ans + = Dfs (S, T, INF);
    printf ("%d\n", ans);
} 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.