HDU 5294 Tricks Device Shortest circuit diagram + min cut

Source: Internet
Author: User

Links: http://acm.hdu.edu.cn/showproblem.php?pid=5294

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


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

There is a shortest path from 1 to n points.

Ask you how many roads you destroy at least will block all the shortest paths.

Ask again how many edges you destroy, and still have the shortest-circuiting of the same length as the original diagram.


Practice:

Run the shortest way first.

Then through dist[i]-dist[j] = = Map[j][i]

If compliant, Map[j][i] is an edge in the shortest path.

Then the shortest side of the building map, run the maximum flow, the flow is how many sides of the same weight edge, run out is the smallest cut, that is, the minimum cost of blocking all the shortest. The cost is each destruction a road for 1. So the value out is the amount of damage to the edge.


And then the same as the maximum flow of the same building, run the shortest way, Edge 1, run out of the shortest road dist[n], is the smallest number of edges across the number of short-circuiting.


#include <iostream> #include <cstring> #include <cstdio> #include <vector> #include <queue > #include <string.h>using namespace std;const int maxn = 2200;//maximum value of const int MAXM = 800000;//number of sides const int I NF2 = 2000000000;struct edge1{int to,next,cap,flow;}    edge[maxm];//Note is maxmint tol;int head[maxn];int gap[maxn],dep[maxn],cur[maxn];void init () {tol = 0; memset (head,-1,sizeof (Head));}    void Add (int u,int v,int w,int rw = 0)//network stream to have reverse arc {edge[tol].to = v; edge[tol].cap = w; edge[tol].flow = 0; Edge[tol].next = Head[u];    Head[u] = tol++; edge[tol].to = u; Edge[tol].cap = RW;    Edge[tol].flow = 0; Edge[tol].next = Head[v]; HEAD[V] = tol++;}    int q[maxn];void BFS (int start,int end) {memset (dep,-1,sizeof (DEP));    memset (Gap,0,sizeof (GAP));    Gap[0] = 1;    int front = 0, rear = 0;    Dep[end] = 0;    q[rear++] = end;        while (front! = rear) {int u = q[front++];       for (int i = head[u]; I! =-1; i = Edge[i].next) {     int v = edge[i].            to;            if (dep[v]! =-1) continue;            q[rear++] = v;            DEP[V] = Dep[u] + 1;        gap[dep[v]]++;    }}}int s[maxn];int SAP (int start,int end, int N)//There are several points {BFS (start,end);      memcpy (cur,head,sizeof (head));    int top = 0;    int u = start;    int ans = 0;    int i;            while (Dep[start] < N) {if (U = = end) {int Min = INF2;            int inser;                    for (i = 0;i < top;i++) {if (Min > Edge[s[i]].cap-edge[s[i]].flow) {                    Min = Edge[s[i]].cap-edge[s[i]].flow;                Inser = i;                }} for (i = 0;i < top;i++) {Edge[s[i]]. Flow + = Min;            Edge[s[i]^1].flow = Min;            } ans + = Min;            top = Inser;            u = edge[s[top]^1].to;        Continue        } bool flag = FALSE;        int V; for (i = Cur[u]; I! =-1; i = Edge[i].            Next) {v = edge[i]. to;                if (edge[i].cap-edge[i].flow && dep[v]+1 = = Dep[u]) {flag = true;                Cur[u] = i;            Break            }} if (flag) {s[top++] = Cur[u];            U = V;        Continue        } int Min = N;  for (i = head[u]; I! = 1; i = edge[i].next) {if (Edge[i].cap-edge[i].flow && dep[edge[i].to]                < min) {min = dep[edge[i].to];            Cur[u] = i;        }} gap[dep[u]]--;        if (!gap[dep[u]]) return ans;        Dep[u] = Min + 1;        gap[dep[u]]++;    if (u! = start) u = edge[s[--top]^1].to; } return ans; #define TYPEC Inttypec inf=0x3f3f3f3f;//prevent back overflow, this cannot be too large bool Vis[maxn];int pre[maxn];void Dijkstra (Typec COST[][MAXN],     Typec lowcost[],int N,int Beg) {for (int i=0;i<n;i++) {lowcost[i]=inf;   Vis[i]=false;    Pre[i]=-1;    } lowcost[beg]=0;        for (int j=0;j<n;j++) {int k=-1;        int min=inf;                for (int i=0;i<n;i++) if (!vis[i]&&lowcost[i]<min) {min=lowcost[i];            K=i;            } if (k==-1) break;            Vis[k]=true;                     for (int i=0;i<n;i++) if (!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i]) {                    Lowcost[i]=lowcost[k]+cost[k][i];                Pre[i]=k; }}}int cost2[maxn][maxn];int dist2[maxn];int cost[maxn][maxn];int num[maxn][maxn];int has[MAXN];int dist[MAXN];int ma    In () {int n,m;        while (scanf ("%d%d", &n,&m)!=eof) {init ();        memset (cost,inf,sizeof cost);        memset (num,0,sizeof num);        for (int i=0;i<n;i++) cost[i][i]=0;            for (int i=1;i<=m;i++)//bidirectional has a heavy edge {int u,v,w; scanf ("%d%d%d", &u,&v,&AMP;W);            u--;            v--;                if (cost[u][v]>w) {cost[u][v]=w;                Cost[v][u]=w;                Num[u][v]=1;            Num[v][u]=1;                } else if (cost[u][v]==w) {num[u][v]++;            num[v][u]++;         }} Dijkstra (cost,dist,n,0);//Wuxiangda Bulian memset (cost2,inf,sizeof cost2);        for (int i=0;i<n;i++) cost2[i][i]=0; for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {if (i!=j) if                (Dist[i]-dist[j]==cost[i][j])                    {cost2[j][i]=1;                 Add (J,i,num[i][j]);        }}} Dijkstra (cost2,dist2,n,0);     printf ("%d%d\n", SAP (0,N-1,N), m-dist2[n-1]); } return 0; }





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

HDU 5294 Tricks Device Shortest circuit diagram + min cut

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.