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

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

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.

Sample Input

  1. 4
  2. 0 1 1 -1
  3. -1 0 1 1
  4. -1 -1 0 1
  5. -1 -1 -1 0
  6. 0 3
  7. 5
  8. 0 1 1 -1 -1
  9. -1 0 1 1 -1
  10. -1 -1 0 1 -1
  11. -1 -1 -1 0 1
  12. -1 -1 -1 -1 0
  13. 0 4

Sample Output

  1. 2
  2. 1

呃,题意和思路什么的直接看宝典吧:

当然啦,没必要一定像他那样ds[i]+edge[i][j]+dt[j]==ds[t]这样,我们要抓住精髓,看清本质,只要保证剌进来的边属于最短路上的边就行,

所以,只要做一次spfa,然后满足d[i]+edge[i][j]==d[j]就行。

某只A题A的神志不清的博主的心灵独白                                                                   begin

然后联想到前面那题POJ1637的构图,不难发现,在integer的情况下把edge.cap设为1,可以代表一种这条边到底走不走的意义,然后全图都设为1的话,最大流大概就是……

从source到target最多有几条不相交的简单路径可走……

嗯,如果这个图上的边全是属于最短路的边的话,那么这个最大流就是本题的答案了……诶等下,再联想到dinic的BFS过程是找层次图,而层次图从某种意义上来讲就是最短路图?

所以我们可以用一个计算层次的BFS来代替SPFA?我好想发现了什么??让我来再码一发(然而spfa本来不就是个BFS么……

然后,我立马就发现……dinic里的BFS是建立在边长默认为1的情况下的,所以我立马就放弃了我TM真是个傻子……

某只A题A的神志不清的博主的心灵独白                                                                   end

呃,先忽略我的心灵独白,看跟现在市面上比较相像的代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<queue>
  4. #define MAXN 103
  5. #define INF 0x3f3f3f3f
  6. using namespace std;
  7. int n,s,t;
  8. int d[MAXN],map[MAXN][MAXN];
  9. bool vis[MAXN];
  10. void spfa(int st)
  11. {
  12. for(int i=;i<n;i++){
  13. i==st ? d[i]= : d[i]=INF;
  14. vis[i]=;
  15. }
  16. queue<int> q;
  17. q.push(st);
  18. vis[st]=;
  19. while(!q.empty())
  20. {
  21. int u=q.front();q.pop();vis[u]=;
  22. for(int v=;v<n;v++)
  23. {
  24. if(u==v || map[u][v]==-) continue;
  25. int tmp=d[v];
  26. if(d[v]>d[u]+map[u][v]) d[v]=d[u]+map[u][v];
  27. if(d[v]<tmp && !vis[v]) q.push(v),vis[v]=;
  28. }
  29. }
  30. }
  31.  
  32. struct Edge{
  33. int u,v,c,f;
  34. };
  35. struct Dinic
  36. {
  37. vector<Edge> E;
  38. vector<int> G[MAXN];
  39. bool vis[MAXN]; //BFS使用
  40. int lev[MAXN];//记录层次
  41. int cur[MAXN]; //当前弧下标
  42. void init(int n)
  43. {
  44. E.clear();
  45. for(int i=;i<n;i++) G[i].clear();
  46. }
  47. void addedge(int from,int to,int cap)
  48. {
  49. E.push_back((Edge){from,to,cap,});
  50. E.push_back((Edge){to,from,,});
  51. int m=E.size();
  52. G[from].push_back(m-);
  53. G[to].push_back(m-);
  54. }
  55. bool bfs()
  56. {
  57. memset(vis,,sizeof(vis));
  58. queue<int> q;
  59. q.push(s);
  60. lev[s]=;
  61. vis[s]=;
  62. while(!q.empty())
  63. {
  64. int now=q.front(); q.pop();
  65. for(int i=;i<G[now].size();i++)
  66. {
  67. Edge edge=E[G[now][i]];
  68. int nex=edge.v;
  69. if(!vis[nex] && edge.c>edge.f)//属于残存网络的边
  70. {
  71. lev[nex]=lev[now]+;
  72. q.push(nex);
  73. vis[nex]=;
  74. }
  75. }
  76. }
  77. return vis[t];
  78. }
  79. int dfs(int now,int aug)//now表示当前结点,aug表示目前为止的最小残量
  80. {
  81. if(now==t || aug==) return aug;//aug等于0时及时退出,此时相当于断路了
  82. int flow=,f;
  83. for(int& i=cur[now];i<G[now].size();i++)//从上次考虑的弧开始,注意要使用引用,同时修改cur[now]
  84. {
  85. Edge& edge=E[G[now][i]];
  86. int nex=edge.v;
  87. if(lev[now]+ == lev[nex] && (f=dfs(nex,min(aug,edge.c-edge.f)))>)
  88. {
  89. edge.f+=f;
  90. E[G[now][i]^].f-=f;
  91. flow+=f;
  92. aug-=f;
  93. if(!aug) break;//aug等于0及时退出,当aug!=0,说明当前节点还存在另一个增广路分支
  94. }
  95. }
  96. return flow;
  97. }
  98. int maxflow()//主过程
  99. {
  100. int flow=;
  101. while(bfs())//不停地用bfs构造分层网络,然后用dfs沿着阻塞流增广
  102. {
  103. memset(cur,,sizeof(cur));
  104. flow+=dfs(s,INF);
  105. }
  106. return flow;
  107. }
  108. }dinic;
  109.  
  110. int main()
  111. {
  112. while(scanf("%d",&n)!=EOF)
  113. {
  114. for(int i=;i<n;i++) for(int j=;j<n;j++) scanf("%d",&map[i][j]);
  115. scanf("%d%d",&s,&t);
  116. if(s==t)
  117. {
  118. printf("inf\n");
  119. continue;
  120. }
  121. spfa(s);
  122. dinic.init(n);
  123. for(int i=;i<n;i++)
  124. for(int j=;j<n;j++)
  125. if(i!=j && map[i][j]!=- && d[i]!=INF && d[j]!=INF && d[i]+map[i][j]==d[j]) dinic.addedge(i,j,);
  126. printf("%d\n",dinic.maxflow());
  127. }
  128. }

(dinic模板的中文注释懒得去掉了,反正看起来不多,到时候忘记了过程还可以看看注释回忆回忆……)

ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]的更多相关文章

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

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

  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(最短路径+最大流)

    Description Given a weighted directed graph, we define the shortest path as the path who has the sma ...

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

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

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

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

  6. ZOJ 2760 How Many Shortest Path

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

  7. HDU - 3631 Shortest Path(Floyd最短路)

    Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...

  8. CF843D Dynamic Shortest Path spfa+剪枝

    考试的T3,拿暴力+剪枝卡过去了. 没想到 CF 上也能过 ~ code: #include <bits/stdc++.h> #define N 100004 #define LL lon ...

  9. ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds      Me ...

随机推荐

  1. Python基础教程学习笔记:第一章 基础知识

    Python基础教程 第二版 学习笔记 1.python的每一个语句的后面可以添加分号也可以不添加分号:在一行有多条语句的时候,必须使用分号加以区分 2.查看Python版本号,在Dos窗口中输入“p ...

  2. POJ 1459 &amp;&amp; ZOJ 1734--Power Network【最大流dinic】

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 25108   Accepted: 13077 D ...

  3. 5 -- Hibernate的基本用法 --2 1 Hibernate 下载和安装

    1. 下载Hibernate压缩包 2. 解压:文件结构 ⊙ documentation : 该路径下存放了Hibernate的相关文档,包括Hibernate的参考文档和API文档等. ⊙ lib ...

  4. linux下查看当前目录属于哪个分区?

    下班之前写哈今天用的一个新命令. df -h /opt/test

  5. 如何关闭Struts2的webconsole.html

    出于安全目的,在禁用了devMode之后,仍然不希望其他人员看到webconsole.html页面,则可以直接删除webconsole.html 的源文件, 它的位置存在于: 我们手工删除 strut ...

  6. 给sharepoint某列表项单独赋予权限

    /// <summary> /// 列表项事件 /// </summary> public class EventReceiver2 : SPItemEventReceiver ...

  7. Disruptor LMAX学习

    http://lmax-exchange.github.io/disruptor/ http://bruce008.iteye.com/blog/1408075 http://code.google. ...

  8. Win10 快捷键

    Win + D # 最小化桌面 Win + L # 锁屏 Win + E # 打开"我的电脑" Win + I # 打开设置 Win + P # 启动投屏 Win + G # 屏幕 ...

  9. Splash Lua 脚本

    Splash 可以通过 Lua 脚本执行一系列渲染操作,这样我们就可以用 Splash 来模拟浏览器的操作了,Splash Lua 基础语法如下: function main(splash, args ...

  10. 关于OSG+QT+VS版本的问题

    CMake3.10.0 Qt5.11.0安装包只有VS2017_64版本,没有VS2017的32位版本,有VS2015的32位版本 Qt5.11.0+osg3.4在CMake的时候,会出现 CMake ...