题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2760

题意:

给出一张有向带边权图,若不联通为-1。问不重叠的最短路有多少条(不重叠是指这条路上没有边重叠)。

思路:

首先由于源点和终点任意,设s为源点,t为终点。

所以用floyd求一遍最短路。

将所有可能是最短路的边建图。

设mp[i][j]为边的权值。dist[i][j]为i到j的最短路。

如果dist[s][i]+mp[i][j]+dist[j][t] == dist[s][t],则i->j这条边就是最短路上的边。

设源点为s,汇点为t,连接图中所有可能是最短路上的边。容量为1。表示这条边只能经过一次。

然后求最大流。

要注意的是给出的图中可能点到该点本身的边权值不一定为0。

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 int N;
  4 #define maxn 110
  5 const int inf = 0x3f3f3f3f;
  6 struct Edge
  7 {
  8     int from, to, cap, flow;
  9     Edge(int f, int t, int c, int fl)
 10     {
 11         from = f; to = t; cap = c; flow = fl;
 12     }
 13 };
 14 vector <Edge> edges;
 15 int s, t, n, m;
 16 vector <int> G[maxn];
 17 int vis[maxn], cur[maxn], d[maxn];
 18 void AddEdge(int from, int to, int cap)
 19 {
 20     edges.push_back(Edge(from, to, cap, 0));
 21     edges.push_back(Edge(to, from, 0, 0));
 22     m = edges.size();
 23     G[from].push_back(m-2);
 24     G[to].push_back(m-1);
 25 }
 26 bool bfs()
 27 {
 28     memset(vis, 0, sizeof(vis));
 29     d[s] = 0;
 30     vis[s] = 1;
 31     queue <int> q;
 32     q.push(s);
 33     while(!q.empty())
 34     {
 35         int u = q.front(); q.pop();
 36         for(int i = 0; i < G[u].size(); i++)
 37         {
 38             Edge &e = edges[G[u][i]];
 39             if(!vis[e.to] && e.cap > e.flow)
 40             {
 41                 vis[e.to] = 1;
 42                 d[e.to] = d[u]+1;
 43                 q.push(e.to);
 44             }
 45         }
 46     }
 47     return vis[t];
 48 }
 49 int dfs(int x, int a)
 50 {
 51     if(x == t || a == 0) return a;
 52     int flow = 0, f;
 53     for(int &i = cur[x]; i < G[x].size(); i++)
 54     {
 55         Edge &e = edges[G[x][i]];
 56         if(d[x]+1 == d[e.to] && (f = dfs(e.to, min(e.cap - e.flow, a))) > 0)
 57         {
 58             e.flow += f;
 59             edges[G[x][i]^1].flow -= f;
 60             flow += f;
 61             a -= f;
 62             if(a == 0) break;
 63         }
 64     }
 65     return flow;
 66 }
 67 int MaxFlow()
 68 {
 69     int flow = 0;
 70     while(bfs())
 71     {
 72         memset(cur, 0, sizeof(cur));
 73         flow += dfs(s, inf);
 74     }
 75     return flow;
 76 }
 77 int mp[maxn][maxn];
 78 int dist[maxn][maxn];
 79 int main()
 80 {
 81     while(~scanf("%d", &N))
 82     {
 83         edges.clear();
 84         for(int i = 1; i <= N; i++) G[i].clear();
 85         for(int i = 1; i <= N; i++)
 86         {
 87             for(int j = 1; j <= N; j++)
 88             {
 89                 scanf("%d", &mp[i][j]);
 90                 dist[i][j] = mp[i][j];
 91                 if(i == j) dist[i][j] = mp[i][j] = 0;
 92             }
 93         }
 94
 95         scanf("%d%d", &s, &t);
 96         s++; t++;
 97
 98         if(s == t)
 99         {
100             printf("inf\n"); continue;
101         }
102
103         for(int i = 1; i <= N; i++)
104         for(int j = 1; j <= N; j++)
105         for(int k = 1; k <= N; k++)
106         {
107             if(dist[j][i] == -1 || dist[i][k] == -1) continue;
108             if(dist[j][k] == -1)
109             {
110                 dist[j][k] = dist[j][i] + dist[i][k];
111                 continue;
112             }
113             if(dist[j][k] >= dist[j][i] + dist[i][k])
114             {
115                 dist[j][k] = dist[j][i] + dist[i][k];
116             }
117         }
118         for(int i = 1; i <= N; i++)
119         {
120             if(dist[s][i] == -1) continue;
121             for(int j = 1; j <= N; j++)
122             {
123                 if(i == j) continue;
124                 if(dist[j][t] == -1) continue;
125                 if(dist[s][t] == -1) continue;
126                 if(dist[s][i] + mp[i][j] + dist[j][t] == dist[s][t])
127                 {
128                     AddEdge(i, j, 1);
129                 }
130             }
131         }
132         int flow = MaxFlow();
133         printf("%d\n", flow);
134
135
136     }
137     return 0;
138 }

转载于:https://www.cnblogs.com/titicia/p/4855365.html

ZOJ 2760 How Many Shortest Path 最大流+floyd求最短路相关推荐

  1. zoj 2760 How Many Shortest Path 最大流

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

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

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

  3. AOJ GRL_1_A: Single Source Shortest Path (Dijktra算法求单源最短路径,邻接表)

    题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_A Single Source Shortest Path In ...

  4. AOJ GRL_1_A: Single Source Shortest Path (Dijktra算法求单源最短路径,邻接表)

    题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_A Single Source Shortest Path In ...

  5. ZOJ 1760 How Many Shortest Path

    ZOJ_1760 只要保留S-T所有可能的最短路上的边,然后做最大流即可,题目数据存在f[i][i]!=0的情况,因此如果用floyd预处理的话要注意初始化f[i][i]=0. #include< ...

  6. zoj How Many Shortest Path

    How Many Shortest Path 题目: 给出一张图,求解最短路有几条.处理特别BT.还有就是要特别处理map[i][i] = 0,数据有不等于0的情况! 竟然脑残到了些错floyd! ! ...

  7. P - The Shortest Path in Nya Graph HDU - 4725

    P - The Shortest Path in Nya Graph HDU - 4725 最短路 不是 每两个点之间按层数设置边权 + 额外边权 TLE 是 相邻两层之间设置边权 + 额外边权 需注 ...

  8. [CF843D]Dynamic Shortest Path

    [CF843D]Dynamic Shortest Path 题目大意: 给定一个带权有向图,包含\(n(n\le10^5)\)个点和\(m(m\le10^5)\)条边.共\(q(q\le2000)\) ...

  9. OSPF(Open Shortest Path First开放式最短路径优先)

    **协议** OSPF(Open Shortest Path First开放式最短路径优先)是一个内部网关协议(Interior Gateway Protocol,简称IGP),用于在单一自治系统(a ...

最新文章

  1. JAVA实现ftp服务端_用 java 实现FTP SERVER(附源码)
  2. electron打包可选择安装位置,可自动更新
  3. Python使用matplotlib可视化发散棒棒糖图、发散棒棒糖图可以同时处理负值和正值、并按照大小排序区分数据、为发散棒棒糖图的特定数据点添加标签、自定义背景填充色、自定义数据点颜色
  4. mysql的一个bug Block Nested Loop
  5. ubuntu中的日志文件位置,用于错误查找
  6. 第四十四期:1.3万亿条数据查询如何做到毫秒级响应?
  7. js过渡效果_干货 | Vue事件、过渡和制作index页面
  8. 练习div出现的小问题
  9. automake生成静态库文件_基于CocoaPods的组件化原理及私有库实践
  10. Carbon Copy Cloner for Mac(磁盘克隆/同步/备份工具)直装版
  11. 基于科大讯飞实现语音识别功能
  12. 微信WAP H5支付功能实现
  13. 番外5. Python OpenCV 中滑动条详细说明与常见问题解决方案
  14. 大学英语综合教程三 Unit 4 课文内容英译中 中英翻译
  15. Android 模块化总结
  16. 微软Surface Go 体验:可以当平板使用的便携笔记本电脑
  17. find grep联合搜索
  18. 使用openCV画出一幅图像的直方图
  19. 你应该了解的 MySQL 细节
  20. 【C语言】文件操作必知必会

热门文章

  1. docker启动tomcat容器并添置项目首页
  2. 【图】二分图最大权匹配
  3. springMVC设置静态资源过滤器,过滤js、css、images等静态资源
  4. IP设置的批处理文件
  5. 2020-05-06 ethtool源代码学习步骤
  6. 2019-12-17 TCP报头结构
  7. 2. 抓ARP包, 抓PPPoE包
  8. 读书笔记《了解用户系列六 | 群体用户心理》文/产品100(简书作者)
  9. JavaScript——(function(){})()立即执行函数解析
  10. html——inline、block与block-inline区别