HDU 5294--tricks Device "minimum cut && shortest circuit processing, new diagram"

Source: Internet
Author: User

Tricks DeviceTime limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 2208 Accepted Submission (s): 584


Problem descriptioninnocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu's at the entrance of the tomb while Dumb Zhang's at the end of it. The tomb is made up of many chambers, and the total number is N. And there is M channels connecting the chambers. Innocent Wu wants to catch up Dumb-to-find out the answers of some questions, however, it ' s Dumb Zhang ' s intention t o Keep Innocent Wu in the dark, to do which he have to stop Innocent Wu from getting him. Only via the original shortest ways from the entrance to the end of the tomb costs the minimum time, and that's the only C Hance Innocent Wu can catch Dumb Zhang.
Unfortunately, Dumb Zhang masters The Art of becoming invisible (qi men dun jia) and tricks devices of this tomb, he can cut off the Connections between chambers by using them. Dumb Zhang wanders how many channels at least he had to cut to stop Innocent Wu. And Innocent Wu wants to know after what many channels at most Dumb Zhang cut off Innocent Wu still have the chance to catch Dumb Zhang.

Inputthere is multiple test cases. Please process till EOF.
For each case,the first line must includes the integers, N (<=2000), M (<=60000). N is the total number of the chambers, and M is the total number of the channels.
In the following M lines, every line must includes three numbers, and use AI, Bi, Li as channel i connecting Chamber AI and Bi (1<=ai,bi<=n), it costs Li (0<li<=100) minute to pass channel I.
The entrance of the tomb is at the chamber one, and the end of tomb is at the chamber N.

Outputoutput numbers to stand for the answers of Dumb Zhang and Innocent Wu ' s questions.
Sample Input
8 91 2 22 3 22 4 13 5 34 5 45 8 11 6 26 7 57 8 1

Sample Output
2 6

Test instructions

N points, M-bars, constructs the right-to-none graph.

The deletion of the minimum number of edges can make the graph not the shortest path, and the deletion of the most edges so that the graph still has the most paths.

Ideas:

(1) The shortest path is processed by a short circuit, if dis[v] = Dist[u] + w (u,v), then the road is in the shortest path. the shortest edge is added to the shortest path graph, the edge weight is 1, and the minimum cut is obtained in the new figure. Turn to the opposing question and ask for the maximum flow of 1--N.

(2) in the shortest running short circuit with the side array record the shortest short-circuit of the minimum number of sides , m-side[N] is the solution.


The shortest path to the most short-circuit processing is the crux of the problem, think for a long time did not want to understand, finally Yu brother to speak, or to SPFA understanding not in depth.

Shortest path graph code is processed at the most short circuit:

See figure void Getmap () {for    (int i = 1; I <= n; ++i)//enumeration start for    (int j = head1[i]; J! =-1; j = str[j].next) {        NOD E e = str[j];        if (dist[e.v] = = Dist[i] + E.W)//plus the shortest path side            Addedge (i, e.v, 1);}    }


AC Code

#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath > #define MAXN 2000+100#define maxm 200000+100#define INF 0x3f3f3f3fusing namespace Std;int m, N;int HEAD[MAXN], Cur[max N], Cnt;int HEAD1[MAXN], Cnt1;int DIST[MAXN], vis[maxn];int side[maxn];struct node{int u, V, cap, flow, next;}; node edge[maxm];struct node{int U, V, W, next;};    NODE str[maxm];void Initedge () {cnt = 0; Memset (Head,-1, sizeof (head));}    void Initstr () {cnt1 = 0; memset (Head1,-1, sizeof (HEAD1));    void Addedge (int u, int v, int w) {node E1 = {u, V, W, 0, Head[u]};    EDGE[CNT] = E1;    Head[u] = cnt++;    Node E2 = {V, u, 0, 0, Head[v]};    EDGE[CNT] = E2; HEAD[V] = cnt++;}    void Addstr (int u, int v, int w) {NODE E1 = {u, V, W, Head1[u]};    STR[CNT1] = E1;    Head1[u] = cnt1++;    NODE E2 = {V, u, W, Head1[v]};    STR[CNT1] = E2; HEAD1[V] = cnt1++;}    void Spfa () {//Find the shortest path + reach N At least the number of edges passed, the number of sides exists side inside queue<int>q; for (int i = 1; I <= N;        ++i) {Vis[i] = 0;        Dist[i] = INF;    Side[i] = 0;    } vis[1] = 1;    DIST[1] = 0;    Q.push (1);        while (!q.empty ()) {int u = q.front ();        Q.pop ();        Vis[u] = 0;            for (int i = head1[u]; i =-1; i = str[i].next) {int v = STR[I].V;            int w = STR[I].W;                if (Dist[v] > Dist[u] + W) {Dist[v] = Dist[u] + W;                SIDE[V] = Side[u] + 1;                    if (!vis[v]) {vis[v] = 1;                Q.push (v);        }} else if (dist[v] = = Dist[u] + W) side[v] = min (Side[v], Side[u] + 1);        }}}//See figure void Getmap () {for (int i = 1; I <= n; ++i)//enumeration start for (int j = head1[i]; J! =-1; j = str[j].next) {        NODE E = Str[j];    if (dist[e.v] = = Dist[i] + E.W)//plus the shortest way side addedge (i, e.v, 1);    }}bool BFS (int st, int ed) {queue<int>q;    memset (Vis, 0, sizeof (VIS)); memset (Dist,-1, sizeof (DIST));    VIS[ST] = 1;    DIST[ST] = 0;    Q.push (ST);        while (!q.empty ()) {int u =q.front ();        Q.pop ();            for (int i = head[u]; i =-1; i = Edge[i].next) {node E = Edge[i];                if (!VIS[E.V] && e.cap > E.flow) {vis[e.v] = 1;                DIST[E.V] = Dist[u] + 1;                if (e.v = = ed) return true;            Q.push (E.V); }}} return false;}    int DFS (int x, int ed, int a) {if (x = = Ed | | a = = 0) return A;    int flow = 0, F;        for (int &i = cur[x]; I! =-1; i = Edge[i].next) {node &e = Edge[i];            if (dist[e.v] = = Dist[x] + 1 && (f = DFS (e.v, ed, Min (A, e.cap-e.flow))) > 0) {e.flow + = f;            edge[i ^ 1].flow-= f;            A-= f;            Flow + + F;        if (a = = 0) break; }} return flow;}    int Maxflow (int st, int ed) {int flowsum = 0;        while (BFS (St, ed)) {memcpy (cur, head, sizeof (head)); FlowSum + = DFS (St, Ed, INF); } return flowsum;}        int main () {while (scanf ("%d%d", &n, &m)! = EOF) {initstr ();            for (int i = 1; I <= m; ++i) {int u, V, W;            scanf ("%d%d%d", &u, &v, &w);        Addstr (U, V, W);        } SPFA ();        Initedge ();        Getmap ();    printf ("%d%d\n", Maxflow (1, N), M-side[n]); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5294--tricks Device "minimum cut && shortest circuit processing, new diagram"

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.