ZOJ 2760 How Many Shortest Path 最短路+最大流
来源:互联网 发布:剑三捏脸数据百度云 编辑:程序博客网 时间:2024/06/13 07:44
题目链接:https://cn.vjudge.net/contest/176665#problem/H
题意:
给定起点s,终点t,统计一个图中不重复边的最短路的条数。
题解:
显然,只有原本就在最短路径上的边才可能会统计中起作用。如果,咱们知道了所有在最短路径上的边,然后,看这些变能凑成条最短路不就可以了么?从起点,到终点的最大路径数,最大->最大流,因为不存在错误的答案,只要能走通,就+1,所以,最大流是正确的。
任意算法得到从起点s开始的最短路径,因为题目数据范围小,所以Floyd也可以。然后建图,建图时不必要全局最优,也就是dist[s][i]+edge[i][j]+dist[j][t]==dist[s][t]
,只需保证dist[s][i]+edge[i][j]==dist[s][j]
,因为,如果是无用边,那么dist[s][j]+dist[j][t]!=dist[s][t]
,也就是任意从j出发的边,都不会是最短路径上的边,这条无用的路径,最晚在t之前断裂,却不会连接到t。只要保证局部最优,则添加进图中,然后在新建图中跑最大流,最大流即答案。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int N = 105;const int M = N*N*8;const int INF = 0x3f3f3f3f;struct edge { int to, next; int c, f;}graph[M];int totlen;int head[N];int e[N][N];int n;int s, t;int dist[N][N];int min(int a, int b) { if(a < 0) return b; if(b < 0) return a; if(a < b) return a; else return b;}void Floyd() { memcpy(dist, e, sizeof dist); for(int k = 0; k < n; k++) { for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(dist[i][k] < 0 || dist[k][j] < 0) continue; dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]); } } }}void init() { totlen = 0; memset(head, -1, sizeof head);}void addEdge(int u, int v, int w) { graph[totlen] = {v, head[u], w, 0}; head[u] = totlen++; graph[totlen] = {u, head[v], 0, 0}; head[v] = totlen++;}// Dinic int que[N];int cur[N];int level[N];bool dinic_bfs(int s, int t) { int front = 0, tail = 0; memset(level, 0, sizeof level); level[s] = 1; que[tail++] = s; while(front != tail) { int u = que[front++]; if(front == N) front = 0; for(int i = head[u]; i != -1; i = graph[i].next) { int v = graph[i].to; if(!level[v] && graph[i].c-graph[i].f > 0) { level[v] = level[u]+1; que[tail++] = v; if(tail == N) tail = 0; } } } return level[t];}// cpflow: can pass flow 到达u点最大能通过的流量int dinic_dfs(int u, int t, int cpflow) { if(u == t) return cpflow; // 到达汇点 // u 点到其他点 最多能增广的流量, 最多不能超过cpflow,由前面的边限制 int addflow = 0; for(int& i = cur[u]; i != -1; i = graph[i].next) { int v = graph[i].to; if(level[v] == level[u]+1 && graph[i].c-graph[i].f > 0) { addflow = dinic_dfs(v, t, min(graph[i].c-graph[i].f, cpflow)); if(addflow > 0) { graph[i].f += addflow; // 正向通过的流量加 graph[i^1].f -= addflow; // 反向的流量就得减 return addflow; // 增广成功,直接返回。 } } } return addflow;}int dinic(int s, int t) { int maxflow = 0; int addflow = 0; while(dinic_bfs(s, t)) { memcpy(cur, head, sizeof head); // 提高效率 while((addflow = dinic_dfs(s, t, INF)) > 0) maxflow += addflow; } return maxflow;}int main () { while(scanf("%d", &n) != EOF) { init(); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { scanf("%d", &e[i][j]); if(i == j) e[i][j] = 0; } } scanf("%d%d", &s, &t); if(s == t) { printf("inf\n"); continue; } Floyd(); for (int i = 0; i < n; i++) { if(dist[s][i] < 0) continue; for(int j = 0; j < n; j++) if(e[i][j] >= 0 && dist[s][j] >= 0) { if(dist[s][i]+e[i][j] == dist[s][j]) { addEdge(i, j, 1); } } } int ans = dinic(s, t); printf("%d\n", ans); } return 0;}
阅读全文
0 0
- ZOJ 2760 How Many Shortest Path 最短路+最大流
- ZOJ 2760 How Many Shortest Path 最短路+最大流
- ZOJ 2760 - How Many Shortest Path(最短路的个数) Floyd+最大流
- zoj 2760 How Many Shortest Path 【最短路 + 最大流】 【求边不重复最短路径条数】
- ZOJ 1760 How Many Shortest Path(最短路+网络流之最大流)
- zoj 2760 How Many Shortest Path(Floyd+枚举判边是否在最短路上+最大流)
- ZOJ 2760--How Many Shortest Path【dinic最大流 && 最短路构图 && 求最短路径的条数】
- ZOJ 2760 - How Many Shortest Path(网络流’最大流)
- ZOJ 2760 How Many Shortest Path(floyd+最大流)
- ZOJ 2760 How Many Shortest Path 最大流+Floyed
- ZOJ 2760 How Many Shortest Path 网络最大流
- ZOJ 2760 How Many Shortest Path(floyd + 最大流)
- ZOJ 2760 How Many Shortest Path 边不相交最短路条数
- zoj How Many Shortest Path 网络流 不相交最短路条数
- ZOJ 2760 How Many Shortest Path(Dijistra + ISAP 最大流)
- ZOJ 2760 How Many Shortest Path (最小费用最大流做法)
- zoj 2760 How Many Shortest Path floyd+最大流+处理的技巧
- zoj 2760 How Many Shortest Path //MAXFLOW
- HDU5972 Regular Number (ShiftAnd算法+bitset)
- javascript中+号的运用
- HDU-4586 Play the Dice(概率期望)
- PTA 7-1 表达式转换——表达式树
- Numpy-复制与视图
- ZOJ 2760 How Many Shortest Path 最短路+最大流
- CSDN 搜索自己博客文章
- Bootstrap学习
- 文摘:跑了1135 家制造企业,我终于明白它们是怎么把自己玩死的了
- Leetcode:Median of Two Sorted Arrays
- hdu2577(基础dp)
- JavaScript DOM操作
- Java程序---学生管理系统的登录和注册
- 初探AssetBundle