Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5294
Test Instructions: give you n a tomb, m path, a person in the Tomb of No. 1th (beginning), another person in the tomb of N (end), the beginning of the person only through the shortest path to catch up with the end of the person, and the end of the person can cut off any path.
The first question--the end of the person to make the starting point that the person can not catch up the number of paths to be cut, the minimum number of paths to output
The second question--the beginning that man can catch up with the end of the person's case, the end that person can cut the maximum number of paths, output the maximum number of paths
Ideas: to make the starting point that the person can not catch up, as long as the shortest path does not exist on the good, so long as the shortest path to the flow of each road is 1, and the maximum flow of the ball out 1-n is, can cut the least number of paths;
If the beginning of the person can catch up, so long as his shortest path exists, and in these shortest path to find the least number of paths, then the maximum number of paths is the least M-path number of the path number.
Code:
#include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <iostream > #include <queue> #include <algorithm> #include <vector>using namespace std; #define LL __int64# Define INF 0x3f3f3f3fconst int maxn=20005;struct qnode{int v;int c;qnode (int _v=0,int _c=0): V (_v), C (_c) {}bool operator &L t; (const qnode &r) Const{return c>r.c;}}; BOOL Vis[2005];int dist[2005];int mp[2005][2005];void Dijkstra (int n,int start)//Find Shortest Path size {memset (vis,false,sizeof (Vis )); for (int i=1;i<=n;i++) Dist[i]=inf;priority_queue<qnode>que;while (!que.empty ()) Que.pop ();d ist[start]= 0;que.push (Qnode (start,0,0)); Qnode Tmp;while (!que.empty ()) {tmp=que.top (); Que.pop (); int u=tmp.v;if (Vis[u]) continue;vis[u]=true;for (int i=1;i<=n;i++) {if (!vis[i]&&mp[u][i]<inf&&dist[i]> Dist[u]+mp[u][i]) {dist[i]=dist[u]+mp[u][i]; Que.push (Qnode (i,dist[i)); }}}}int mp2[2005][2005];void Dijkstra1 (int n,int start)//Find the shortest possible route {memset (vis,false,sizeof (VIS)); for (int i=1;i<=n;i++) Dist[i]=inf; Priority_queue<qnode>que;while (!que.empty ()) Que.pop ();d Ist[start]=0;que.push (Qnode (start,0,0)); Qnode tmp; while (!que.empty ()) {tmp=que.top (); Que.pop (); int u=tmp.v;int gg=tmp.flag+1;if (vis[u]) continue;vis[u]=true;for (int i=1;i<=n;i++) {if (!vis[i]&&mp2[u][i]<inf&&dist[i]>dist[u]+mp2[u][i]) {Dist[i]=dist[u]+mp2[u][i]; Que.push (Qnode (i,dist[i)); }}}}struct edge{int from,to,cap,flow; Edge () {} edge (int f,int t,int c,int FL): From (f), to (t), Cap (c), Flow (fl) {}};struct dinic{int n,m,s,t; Vector<edge> edges; Vector<int> G[MAXN]; int CUR[MAXN]; int D[MAXN]; BOOL VIS[MAXN]; void init (int n,int s,int t) {this->n=n, this->s=s, this->t=t; Edges.clear (); for (int i=0;i<n;i++) g[i].clear (); } void AddEdge (int from,int to,int cap) {Edges.push_back (Edge (from,to,cap,0)); Edges.push_back (Edge (to,from,0,0)); m = Edges.size (); G[from].push_back (m-2); G[to].push_back (m-1); } bool BFS () {queue<int> Q; Q.push (s); memset (vis,0,sizeof (VIS)); d[s]=0; Vis[s]=true; while (! Q.empty ()) {int X=q.front (); Q.pop (); for (int i=0;i<g[x].size (); ++i) {edge& e=edges[g[x][i]]; if (!vis[e.to] && e.cap>e.flow) {d[e.to]=1+d[x]; Vis[e.to]=true; Q.push (e.to); }}} return vis[t]; } int DFS (int x,int a) {if (x==t | | a==0) return A; int flow=0,f; For (int& i=cur[x];i<g[x].size (); ++i) {edge& e=edges[g[x][i]]; if (d[e.to]==d[x]+1 && (F=dfs (e.to, Min (a,e.cap-e.flow))) >0) {E.flow +=f; Edges[g[x][i]^1].flow-=f; Flow +=f; A-=f; if (a==0) break; }} return flow; } int Max_flow () {int ans=0; while (BFS ()) {memset (cur,0,sizeof (cur)); Ans + = DFS (S,inf); } return ans; }}dc;int Ggg[2005][2005];int Main () {int n,m,i,j,a,b,c; while (~SCANF ("%d%d", &n,&m)) {dc.init (n,1,n); memset (ggg,0,sizeof (GGG)); for (i=1;i<=n;i++) for (j=1;j<=n;j++) Mp[i][j]=mp2[i][j]=inf; for (i=0;i<m;i++) {scanf ("%d%d%d", &a,&b,&c); if (mp[a][b]>c) {mp[a][b]=c; Mp[b][a]=c; Ggg[a][b]=1; Ggg[b][a]=1; } else if (mp[a][b]==c) {ggg[a][b]++; ggg[b][a]++; }} Dijkstra (n,1); for (i=1;i<=n;i++) {for (j=1;j<=n;j++) {if (Mp[i][j]+dist[i]==dist[j]) {for (int k=0;k<ggg[i][j];k++) DC. Addedge (i,j,1); Mp2[i][j]=1; }}} Dijkstra1 (n,1); printf ("%d%d\n", Dc.max_flow (), m-dist[n]); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5294 Tricks Device (2015 Multi-school first game 7th) Max Stream + shortest circuit