Given a weighted directed graph, we define the shortest path as the path who has the smallest length among all the path connecting the source vertex to the target vertex. And if two path is said to be non-overlapping, it means that the two path has no common edge. So, given a weighted directed graph, a source vertex and a target vertex, we are interested 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 the matrix 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
21
我想 静静。。。
Each element of the matrix is either non-negative integer, denotes the length of the edge, or -1, which means there is no edge。
为什么矩阵中 i 到 i的边的长度可以不为0,不为0就算了。为什么在不为0的情况下 必须按0处理? 无语死了。。。 TLE6次才过。
题意:给你N个点(0->N-1),给出一个关系矩阵表示两点间边的长度L, L可能为非负数,也可能为-1。其中-1表示两点间没有边。 又给了你起点和终点,让你求从起点到终点的 边不重复的最短路径的最多条数。(不同的选择 会导致 边不重复的最短路径的条数有所不同)。
思路:预处理最短路。然后建新图,把最短路上的边加到新图上,边权为1。再跑一遍起点到终点的最大流即可。
AC代码:10ms
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define MAXN 200 #define MAXM 80000+10 #define INF 1000000 using namespace std; struct Edge { int from, to, cap, flow, next; }; Edge edge[MAXM]; int head[MAXN], edgenum, cur[MAXN]; int dist[MAXN]; bool vis[MAXN]; int N; struct DD { int from, to, val, next; }; DD Dist[MAXM]; int H[MAXN], top; bool flag;//判断是否存在最短路 int sx, ex;//起点终点 void initDD() { top = 0; memset(H, -1, sizeof(H)); } void addDD(int u, int v, int w) { DD E = {u, v, w, H[u]}; Dist[top] = E; H[u] = top++; } void getDD() { int a; for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { scanf("%d", &a); if(a != -1) { if(i == j && a != 0) a = 0;//坑死!!! addDD(i, j, a); } } } } void SPFA()//预处理最短路 { queue<int> Q; memset(dist, INF, sizeof(dist)); memset(vis, false, sizeof(vis)); dist[sx] = 0; vis[sx] = true; Q.push(sx); while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = false; for(int i = H[u]; i != -1; i = Dist[i].next) { DD E = Dist[i]; if(dist[E.to] > dist[u] + E.val) { dist[E.to] = dist[u] + E.val; if(!vis[E.to]) { vis[E.to] = true; Q.push(E.to); } } } } if(dist[ex] == INF)//到不了终点 flag = false; } void initEdge() { edgenum = 0; memset(head, -1, sizeof(head)); } void addEdge(int u, int v, int w) { Edge E1 = {u, v, w, 0, head[u]}; edge[edgenum] = E1; head[u] = edgenum++; Edge E2 = {v, u, 0, 0, head[v]}; edge[edgenum] = E2; head[v] = edgenum++; } void getEdge() { memset(mark, false, sizeof(mark)); for(int i = 0; i < N; i++) { for(int j = H[i]; j != -1; j = Dist[j].next) { DD E = Dist[j]; if(dist[E.to] == dist[i] + E.val) addEdge(i, E.to, 1);//最短路的边加进 新图 } } } bool BFS(int start, int end)//找增广路 { queue<int> Q; memset(dist, -1, sizeof(dist)); memset(vis, false, sizeof(vis)); dist[start] = 0; vis[start] = true; Q.push(start); while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int i = head[u]; i != -1; i = edge[i].next) { Edge E = edge[i]; if(!vis[E.to] && E.cap > E.flow) { vis[E.to] = true; dist[E.to] = dist[u] + 1; if(E.to == end) return true; Q.push(E.to); } } } return false; } int DFS(int x, int a, int end) { if(x == end || a == 0) return a; int flow = 0, f; for(int &i = cur[x]; i != -1; i = edge[i].next) { Edge &E = edge[i]; if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(E.cap - E.flow, a), end)) > 0) { E.flow += f; edge[i^1].flow -= f; flow += f; a -= f; if(a == 0) break; } } return flow; } int Maxflow(int start, int end) { int flow = 0; while(BFS(start, end)) { memcpy(cur, head, sizeof(head)); flow += DFS(start, INF, end); } return flow; } int main() { while(scanf("%d", &N) != EOF) { initDD(); getDD(); scanf("%d%d", &sx, &ex); if(sx == ex) { printf("inf\n"); continue; } flag = true; SPFA(); if(!flag)//不存在最短路 { printf("0\n"); continue; } initEdge(); getEdge(); printf("%d\n", Maxflow(sx, ex)); } return 0; }