The shortest path, at the same time, find out how many edges are at least
The minimum cut is then reconstructed using the side of the shortest path.
Tricks DeviceTime
limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 1584 Accepted Submission (s): 388
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
Authorfzuacm
Source2015 multi-university Training Contest 1
/* ***********************************************author:ckbosscreated time:2015 July 24 Friday 10:07 09 seconds file Name : hdoj5294.cpp************************************************ * * #include <iostream> #include <cstdio># Include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib > #include <vector> #include <queue> #include <set> #include <map>using namespace Std;typedef pair<int,int> pii;const int inf=0x3f3f3f3f;const int Maxn=2200;int n,m;/*************edge********************/ struct Edge{int to,next,cost,cap,flow;} Edge[maxn*60],edge2[maxn*60];int adj[maxn],size;int adj2[maxn],size2;void add_edge (int u,int v,int c) {edge[Size].to= V;edge[size].next=adj[u];edge[size].cost=c; adj[u]=size++;} /********spfa************/int dist[maxn];bool inq[maxn];vector<int> pre[maxn];int spfa (Edge* edge,int* Adj) { memset (dist,63,sizeof (Dist)); memset (inq,false,sizeof (InQ)); dist[1]=0; QueUe<int> Q; Inq[1]=true;q.push (1); while (!q.empty ()) {int U=q.front (); Q.pop (); for (int i=adj[u];~i;i=edge[i].next) {int v=edge[i].to; if (dist[v]>dist[u]+edge[i].cost) {pre[v].clear (); Pre[v].push_back (U); Dist[v]=dist[u]+edge[i].cost; if (!inq[v]) {inq[v]=true; Q.push (v); }}else if (dist[v]==dist[u]+edge[i].cost) {pre[v].push_back (U);} } Inq[u]=false; } return dist[n];} /********************rebuild************************/void add_edge2 (int u,int v,int w,int rw=0) {edge2[Size2].cost=1 ; edge2[size2].to=v; Edge2[size2].cap=w; edge2[size2].next=adj2[u];edge2[size2].flow=0; Adj2[u]=size2++;edge2[size2].cost=1;edge2[size2].to=u; Edge2[size2].cap=w; edge2[size2].next=adj2[v];edge2[size2].flow=0; adj2[v]=size2++;} BOOL Used[maxn];int edges;void rebuild () {memset (used,false,sizeof (used)); QUEUE<INT> Q;q.push (n); Used[n]=true;edges=0;while (!q.empty ()) {int V=q.front (); Q.pop (); for (int i=0,sz=pre[v].size (); i<sz;i++) {int u= pre[v][i];///u--->v//cout<<u<< "--->" <<v<<endl;edges++; Add_edge2 (u,v,1); if (used[u]==false) {used[u]=true; Q.push (U);}}} /************************max_flow*******************************/int GAP[MAXN],DEP[MAXN],PRE[MAXN],CUR[MAXN]; int sap (int start,int end,int n,edge* edge=edge2) {memset (gap,0,sizeof (GAP)); memset (DEP); Dep,0,sizeof (memcpy, Adj2,sizeof (ADJ2)); int u=start;pre[u]=-1; Gap[0]=n;int Ans=0;while (dep[start]<n) {if (u==end) {int min=inf;for (int i=pre[u];~i;i=pre[edge[i^1].to]) {if (Min >edge[i].cap-edge[i].flow) Min=edge[i].cap-edge[i].flow;} for (int i=pre[u];~i;i=pre[edge[i^1].to]) {edge[i].flow+=min;edge[i^1].flow-=min;} U=start;ans+=min;continue;} BOOL Flag=false;int v;for (int i=cur[u];~i;i=edge[i].next) {v=edge[i].to;if (edge[i].cap-edge[i].flow&&dep[v ]+1==dep[u]) {flag=true;cur[u]=pre[v]=i;break;}} if (flag) {u=v; contiNue;} int min=n;for (int i=adj2[u];~i;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[pre[u]^1].to;} return ans;} void Init () {memset (adj,-1,sizeof (ADJ)); Size=0;memset (adj2,-1,sizeof (ADJ2)); size2=0;for (int i=1;i<=n;i++) pre[i].clear ();} int main () {//freopen ("In.txt", "R", stdin),//freopen ("OUT.txt", "w", stdout), while (scanf ("%d%d", &n,&m)!=eof {init (); for (int i=0,u,v,c;i<m;i++) {scanf ("%d%d%d", &u,&v,&c); Add_edge (U,V,C); Add_edge (v,u,c);} SPFA (EDGE,ADJ); rebuild (); int Max_flow=sap (1,n,n); int MIN_SHORT_PATH=SPFA (EDGE2,ADJ2);p rintf ("%d%d\n", Max_flow, M-min_short_path);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdoj 5294 Tricks Device Shortest route (record path) + min cut