Given a weighted directed graph, we define the shortest path as the path who has the smallest length among allthe path connecting the source vertex to the target vertex. And if two path is said to be non-overlapping, it means that thetwo path has no common edge. So, given a weighted directed graph, a source vertex and a target vertex, we areinterested in how many non-overlapping shortest path could we find out at most.
Input
Input consists of multiple test cases. The first line of each test case, there is an integer number N (1<=N<=100),which is the number of the vertices. Then follows an N * N matrix, represents the directed graph. Each element of thematrix is either non-negative integer, denotes the length of the edge, or -1, which means there is no edge. At the last,the test case ends with two integer numbers S and T (0<=S, T<=N-1), that is, the starting and ending points.Process to the end of the file.
Output
For each test case, output one line, the number of the the non-overlapping shortest path that we can find at most,or "inf" (without quote), if the starting point meets with the ending.
Sample Input
4 0 1 1 -1 -1 0 1 1 -1 -1 0 1 -1 -1 -1 0 0 3 5 0 1 1 -1 -1 -1 0 1 1 -1 -1 -1 0 1 -1 -1 -1 -1 0 1 -1 -1 -1 -1 0 0 4
Sample Output
2 1Author: SHEN, Guanghao
传送门:zoj 2760 How Many Shortest Path
题目大意:求不相交最短路径数
题目分析:可以Floyd的貌似,管他呢,敲一个Dijkstra + Heap堆优化 + ISAP搞搞得了。就是先预处理出最短路,然后对于所有在最短路上的边(u,v)建边(u,v,1)。
源点就是起点,汇点就是终点。然后跑一遍最大流即可。
PS:早期写的一道题,代码写的太shi,画风不好还请轻喷 O _ O ||
再次无责任PS:其实跑一次 Dij 就够了,完全没必要跑两次,不一定非要在全局最短路上的边,只要是当前最短路上的就行了,反正不是全局最短路的边肯定不会流到汇点的。
代码如下:
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int O = 100000; const int N = 105; const int inf = 0x3f3f3f3f; typedef long long LL; int src, des, n; struct D{ typedef struct edge{ int v, n, d; }edge; typedef struct H{ int d, num; }H; edge E[O]; int Adj[N], tot; H heap[O]; int top; int d[N][2], done[N]; int w[N][N], dis; void addedge(int u, int v,int w){ E[tot].v = v; E[tot].d = w; E[tot].n = Adj[u]; Adj[u] = tot++; } void swap(H *a, H *b){ H tmp = *a; *a = *b; *b = tmp; } void push(int d, int num){ H h = {d, num}; heap[top] = h; int o = top++; int p = (o - 1) / 2; while(o && heap[o].d < heap[p].d){ swap(&heap[o], &heap[p]); o = p; p = (o - 1) / 2; } } int pop(){ int ans = heap[0].num; heap[0] = heap[--top]; int o = 0; int tmp = o; int l = o * 2 + 1; int r = o * 2 + 2; while(1){ if(l <= top && heap[l].d < heap[tmp].d) tmp = l; if(r <= top && heap[r].d < heap[tmp].d) tmp = r; if(tmp != o){ swap(&heap[tmp], &heap[o]); o = tmp; tmp = o; l = o * 2 + 1; r = o * 2 + 2; } else break; } return ans; } int is_empty(){ return top == 0; } void init(){ memset(Adj, -1, sizeof(Adj)); memset(done, 0, sizeof(done)); tot = top = 0; } void Dijkstra(int s, int t, int o){ d[s][o] = 0; push(d[s][o], s); while(!is_empty()){ int u = pop(); if(done[u]) continue; done[u] = 1; for(int i = Adj[u]; ~i; i = E[i].n){ int v = E[i].v; if(d[v][o] > d[u][o] + E[i].d){ d[v][o] = d[u][o] + E[i].d; push(d[v][o], v); } } } if(dis > d[t][o]) dis = d[t][o]; } int read(int s, int t, int n){ for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ scanf("%d", &w[i][j]);//如果wa记得检查这里 } } scanf("%d%d", &src, &des); if(src == des) return 0; dis = inf; memset(d, inf, sizeof(d)); init(); for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(w[i][j] >= 0) addedge(i, j, w[i][j]); } } Dijkstra(src, des, 0); init(); for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(w[i][j] >= 0) addedge(j, i, w[i][j]); } } Dijkstra(des, src, 1); return 1; } }D; struct I{ typedef struct edge{ int v, c, n; }edge; edge E[O]; int Adj[N], tot; int Q[O], head, tail; int d[N], num[N], pre[N], cur[N]; void init(){ memset(Adj, -1, sizeof(Adj)); tot = 0; } void addedge(int u, int v, int w){ E[tot].v = v; E[tot].c = w; E[tot].n = Adj[u]; Adj[u] = tot++; E[tot].v = u; E[tot].c = 0; E[tot].n = Adj[v]; Adj[v] = tot++; } void rev_BFS(int s, int t){ memset(num, 0, sizeof(num)); memset(d, -1, sizeof(d)); head = tail = 0; d[t] = 0; num[0] = 1; Q[tail++] = t; while(head != tail){ int u = Q[head++]; for(int i = Adj[u]; ~i; i = E[i].n){ int v = E[i].v; if(d[v] >= 0) continue; d[v] = d[u] + 1; num[d[v]]++; Q[tail++] = v; } } } int ISAP(int s, int t, int nv){ memcpy(cur, Adj, sizeof(cur)); rev_BFS(s, t); int flow = 0, u, i; u = pre[s] = s; while(d[s] < nv){ if(u == t){ int f = inf, neck; for(i = s; i != t; i = E[cur[i]].v){ if(f > E[cur[i]].c){ neck = i; f = E[cur[i]].c; } } for(i = s; i != t; i = E[cur[i]].v){ E[cur[i]].c -= f; E[cur[i] ^ 1].c += f; } flow += f; u = neck; } for(i = cur[u]; ~i; i = E[i].n){ if(E[i].c > 0 && d[u] == d[E[i].v] + 1) break; } if(~i){ cur[u] = i; pre[E[i].v] = u; u = E[i].v; } else{ if(0 == (--num[d[u]])) break; int mind = nv; for(i = Adj[u]; ~i; i = E[i].n){ if(E[i].c > 0 && mind > d[E[i].v]){ cur[u] = i; mind = d[E[i].v]; } } d[u] = mind + 1; num[d[u]]++; u = pre[u]; } } return flow; } }I; struct S{ void solve(int n){ if(D.read(src, des, n) == 0){ printf("inf\n"); return; } I.init(); for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(D.w[i][j] != -1 && D.d[i][0] + D.d[j][1] + D.w[i][j] == D.dis){ I.addedge(i, j, 1); } } } printf("%d\n", I.ISAP(src, des, n)); } }S; int main(){ while(~scanf("%d", &n)) S.solve(n); return 0; }