Description

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.

题目大意:一有向有权图,给源点、汇点,问从源点到汇点有多少条不重叠(没有重边)的最短路径

思路:一次floyd,把dis[s][i] + edge[i][j] + dis[j][t] == dis[s][t]的边(最短路径上的边)都加入网络流的图,容量为1。最大流为答案(容量为1,那么这些从源点出发的流都不会有重叠边)。

PS:据说矩阵的对角线上的点不都是0,我把AC代码上的mat[i][i] = st[i][i] = 0注释掉了,果然WA了,这是闹哪样……当然要是你不使用类似于[i][i]这种边就不会有这种烦恼……

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; const int MAXN = 110;
const int MAXE = MAXN * MAXN * 2;
const int INF = 0x7f7f7f7f; struct Dinic {
int n, m, st, ed, ecnt;
int head[MAXN];
int cur[MAXN], d[MAXN];
int to[MAXE], next[MAXE], flow[MAXE], cap[MAXE]; void init(int ss, int tt, int nn) {
st = ss; ed = tt; n = nn;
ecnt = 2;
memset(head, 0, sizeof(head));
} void add_edge(int u, int v, int c) {
to[ecnt] = v; cap[ecnt] = c; flow[ecnt] = 0; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; cap[ecnt] = 0; flow[ecnt] = 0; next[ecnt] = head[v]; head[v] = ecnt++;
} bool bfs() {
memset(d, 0, sizeof(d));
queue<int> que; que.push(st);
d[st] = 1;
while(!que.empty()) {
int u = que.front(); que.pop();
for(int p = head[u]; p; p = next[p]) {
int v = to[p];
if(!d[v] && cap[p] > flow[p]) {
d[v] = d[u] + 1;
que.push(v);
if(v == ed) return true;
}
}
}
return d[ed];
} int dfs(int u, int a) {
if(u == ed || a == 0) return a;
int outflow = 0, f;
for(int &p = cur[u]; p; p = next[p]) {
int v = to[p];
if(d[u] + 1 == d[v] && (f = dfs(v, min(a, cap[p] - flow[p]))) > 0) {
flow[p] += f;
flow[p ^ 1] -= f;
outflow += f;
a -= f;
if(a == 0) break;
}
}
return outflow;
} int Maxflow() {
int ans = 0;
while(bfs()) {
for(int i = 0; i <= n; ++i) cur[i] = head[i];
ans += dfs(st, INF);
}
return ans;
}
} G; int mat[MAXN][MAXN];
int st[MAXN][MAXN]; #define REP(i, t) for(int i = 1; i <= t; ++i) void floyd(int n) {
REP(k, n) REP(i, n) REP(j, n) {
if(st[i][k] == -1 || st[k][j] == -1) continue;
if(st[i][j] == -1 || st[i][j] > st[i][k] + st[k][j]) st[i][j] = st[i][k] + st[k][j];
}
//REP(i, n) REP(j, n) printf("%d\n", st[i][j]);
} int main() {
int n, s, t;
while(scanf("%d", &n) != EOF) {
REP(i, n) REP(j, n) {
scanf("%d", &mat[i][j]);
st[i][j] = mat[i][j];
}
REP(i, n) st[i][i] = mat[i][i] = 0;
scanf("%d%d", &s, &t);
++s, ++t;
if(s == t) {
printf("inf\n");
continue;
}
floyd(n);
G.init(s, t, n);
REP(i, n) REP(j, n)
if(i != j && mat[i][j] != -1 && st[s][i] != -1 && st[j][t] != -1
&& st[s][t] == st[s][i] + mat[i][j] + st[j][t]) G.add_edge(i, j, 1);
printf("%d\n", G.Maxflow());
}
}

  

ZOJ 2760 How Many Shortest Path(最短路径+最大流)的更多相关文章

  1. zoj 2760 How Many Shortest Path【最大流】

    不重叠最短路计数. 先弗洛伊德求一遍两两距离(其实spfa或者迪杰斯特拉会更快但是没必要懒得写),然后设dis为st最短距离,把满足a[s][u]+b[u][v]+a[v][t]==dis的边(u,v ...

  2. zoj 2760 How Many Shortest Path 最大流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...

  3. ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]

    人老了就比较懒,故意挑了到看起来很和蔼的题目做,然后套个spfa和dinic的模板WA了5发,人老了,可能不适合这种刺激的竞技运动了…… 题目链接:http://acm.zju.edu.cn/onli ...

  4. ZOJ 2760 How Many Shortest Path (不相交的最短路径个数)

    [题意]给定一个N(N<=100)个节点的有向图,求不相交的最短路径个数(两条路径没有公共边). [思路]先用Floyd求出最短路,把最短路上的边加到网络流中,这样就保证了从s->t的一个 ...

  5. ZOJ 2760 How Many Shortest Path

    题目大意:给定一个带权有向图G=(V, E)和源点s.汇点t,问s-t边不相交最短路最多有几条.(1 <= N <= 100) 题解:从源点汇点各跑一次Dij,然后对于每一条边(u,v)如 ...

  6. ZOJ 2760 How Many Shortest Path(Dijistra + ISAP 最大流)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 题意:给定一个带权有向图 G=(V, E)和源点 s.汇点 t ...

  7. SPOJ 15. The Shortest Path 最短路径题解

    本题就是给出一组cities.然后以下会询问,两个cities之间的最短路径. 属于反复询问的问题,临时我仅仅想到使用Dijsktra+heap实现了. 由于本题反复查询次数也不多,故此假设保存全部最 ...

  8. [ZOJ2760]How Many Shortest Path(floyd+最大流)

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 题意:给你一个一个n*n(n<=100)的有向图,问你从s到 ...

  9. [Swift]LeetCode847. 访问所有节点的最短路径 | Shortest Path Visiting All Nodes

    An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...

随机推荐

  1. 解决h5底部输入框在ios被软键盘顶飞 软键盘消失还下不来

    好吧,其实不是顶飞,准确点说应该是h5页面fiexed定位在底部的输入框在ios软键盘弹起的时候软键盘跟输入框有时会有一段悬空的距离,无法紧贴.在安卓机子上则没有这样的情况. 解决方法是通过h5的sc ...

  2. es6 Reflect对象详解

    Reflect是ES6为操作对象而提供的新API,而这个API设计的目的只要有: 将Object对象的一些属于语言内部的方法放到Reflect对象上,从Reflect上能拿到语言内部的方法.如:Obj ...

  3. php bug 调试助手 debug_print_backtrace()

    debug_print_backtrace() 是一个很低调的函数,很少有人注意过它. 不过当我对着一个对象调用另一个对象再调用其它的对象和文件中的一个函数出错时,它也许正在一边笑呢 如果我们想知道某 ...

  4. QWebView 与Js 交互

    我本愚钝,在网上搜了一下没找到可以运行的栗子,遂在这记录一下吧. 环境:win10 64位系统  qt 4.8.7 (mingw32) qtcreator(4.5.0) 1. 建立一个 Widgets ...

  5. 如何在golang中打印grpc详细日志

    最近捣鼓fabric,在一个tls证书问题上纠结挺久,连接orderer服务时候,grpc日志总是冷冰冰的显示这个信息 Orderer Client Status Code: (2) CONNECTI ...

  6. 快排(golang实现) 递归方法

    递归方法,逻辑简洁清晰.这个算法还是很重要的,需要重点记忆理解,面试经常考手写.据说是与傅里叶变换等并称“20世纪十大算法”.https://blog.csdn.net/v_JULY_v/articl ...

  7. tensorflow 教程 文本分类 IMDB电影评论

    昨天配置了tensorflow的gpu版本,今天开始简单的使用一下 主要是看了一下tensorflow的tutorial 里面的 IMDB 电影评论二分类这个教程 教程里面主要包括了一下几个内容:下载 ...

  8. 蓝牙入门知识-CC2541知识

    蓝牙是为了能够通信,想要通信就必须遵守一定的规则, Profile 就可以理解为相互约定的规则,因为每个协议栈demo 都会有一个Profile 与之对应, 我们这里的SimpleBLExxx 对应的 ...

  9. 数据库路由中间件MyCat - 背景篇(2)

    此文已由作者张镐薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. MyCat的前世今生 如前文所说,Amoeba.Cobar.MyCat等属于同宗一脉.若Amoeba能继续下 ...

  10. 机器学习的5种“兵法"

    大数据文摘作品,欢迎个人转发朋友圈,自媒体.媒体.机构转载务必申请授权,后台留言“机构名称+转载”,申请过授权的不必再次申请,只要按约定转载即可. 作者:Jason Brownlee 译者:Clair ...