zoj 2760(边不相交最短路的条数)
来源:互联网 发布:末世之超级淘宝txt 编辑:程序博客网 时间:2024/05/23 13:24
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
40 1 1 -1-1 0 1 1-1 -1 0 1-1 -1 -1 00 350 1 1 -1 -1-1 0 1 1 -1-1 -1 0 1 -1-1 -1 -1 0 1-1 -1 -1 -1 00 4
Sample Output
21
Author: SHEN, Guanghao
Source: ZOJ Monthly, September 2006
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2760
分析:这题要求边不相交的最短路条数,我们可以想到把最短路上的边都加到网络里,且容量为1,这样边就不会相交了,最大流就是答案,问题即转化为求哪些边是最短路上的,一开始我用dijstra做最短路,然后枚举边e[i][j]满足dis[i]+e[i][j]==dis[j]的边为最短路上的,但是wa了,后来看了一些人的做法都是floyd求出最短路d[i][j],然后枚举边e[i][j]满足d[s][i]+e[i][j]+d[j][t]==d[s][t]的边即最短路上的边(之前那种为什么错呢。。。),这样图就建完了,不过要注意不要用d[s][t]==0来判断是否输出inf,我因此wa了几次,还有d[i][i]都要赋值为0,数据很坑人阿~~~
代码:
#include<cstdio>using namespace std;const int mm=222222;const int mn=222;const int oo=1000000000;int node,src,dest,edge;int ver[mm],flow[mm],next[mm];int head[mn],work[mn],dis[mn],q[mn];int map[mn][mn],d[mn][mn];inline int min(int a,int b){ return a<b?a:b;}inline void prepare(int _node,int _src,int _dest){ node=_node,src=_src,dest=_dest; for(int i=0; i<node; ++i)head[i]=-1; edge=0;}inline void addedge(int u,int v,int c){ ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++; ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;}bool Dinic_bfs(){ int i,u,v,l,r=0; for(i=0; i<node; ++i)dis[i]=-1; dis[q[r++]=src]=0; for(l=0; l<r; ++l) for(i=head[u=q[l]]; i>=0; i=next[i]) if(flow[i]&&dis[v=ver[i]]<0) { dis[q[r++]=v]=dis[u]+1; if(v==dest)return 1; } return 0;}int Dinic_dfs(int u,int exp){ if(u==dest)return exp; for(int &i=work[u],v,tmp; i>=0; i=next[i]) if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0) { flow[i]-=tmp; flow[i^1]+=tmp; return tmp; } return 0;}int Dinic_flow(){ int i,ret=0,delta; while(Dinic_bfs()) { for(i=0; i<node; ++i)work[i]=head[i]; while(delta=Dinic_dfs(src,oo))ret+=delta; } return ret;}int main(){ int i,j,k,n,s,t; while(scanf("%d",&n)!=-1) { for(i=0; i<n; ++i) for(j=0; j<n; ++j) { scanf("%d",&map[i][j]); if(i==j)map[i][j]=0; if(map[i][j]<0)map[i][j]=oo; d[i][j]=map[i][j]; } scanf("%d%d",&s,&t); if(s!=t) { for(k=0; k<n; ++k) for(i=0; i<n; ++i) if(d[i][k]<oo)for(j=0; j<n; ++j) if(d[k][j]<oo)d[i][j]=min(d[i][j],d[i][k]+d[k][j]); prepare(n,s,t); for(i=0; i<n; ++i) if(d[s][i]<oo)for(j=0; j<n; ++j) if(d[j][t]<oo&&map[i][j]<oo&&d[s][i]+map[i][j]+d[j][t]==d[s][t])addedge(i,j,1); printf("%d\n",Dinic_flow()); } else printf("inf\n"); } return 0;}
- zoj 2760(边不相交最短路的条数)
- ZOJ 2760 How Many Shortest Path 边不相交最短路条数
- zoj How Many Shortest Path 网络流 不相交最短路条数
- zoj 2760 网络流——无相同边的最短路的条数
- hud4807 Lunch Time 【费用流 求前k条不相交的最短路】
- ZOJ 2760--How Many Shortest Path【dinic最大流 && 最短路构图 && 求最短路径的条数】
- 最短路条数
- 求最短路条数之类的~~~
- soj 4526 不重叠最短路条数
- zoj 2760 How Many Shortest Path 【最短路 + 最大流】 【求边不重复最短路径条数】
- Uva 10806 求2条不相交最短路 费用流
- CDOJ_1147 (最短路条数)
- 最短路条数模板
- 最短路条数计数
- POJ_3463_Sightseeing(最短路/次短路条数)
- 最短路和次短路条数
- poj3463Sightseeing(最短路条数+次短路条数)
- ZOJ 2760 最短路径条数问题
- Lazarus 下如何让非可视化组件(类)截获事件
- VC音频函数的介绍-附实例
- PCI的配置空间、I/O空间、内存空间的具体区别
- linux shell命令笔记
- hdu 1408 盐水的故 (注意精度)
- zoj 2760(边不相交最短路的条数)
- 开发游戏音频程序——播放MIDI文件
- C++0X中值得关注的几大变化
- JAVA新浪微博运用API开源开发
- Android AsyncTask
- C#学习笔记23——C#操作XML
- c++实现文件传输解析
- C++ 前自增和后自增
- 一个传值的问题”*&”与”*”