How Many Shortest Path


Time Limit: 10 Seconds      Memory Limit: 32768 KB

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

4
0 1 1 -1
-1 0 1 1
-1 -1 0 1
-1 -1 -1 0
0 3
5
0 1 1 -1 -1
-1 0 1 1 -1
-1 -1 0 1 -1
-1 -1 -1 0 1
-1 -1 -1 -1 0
0 4

Sample Output

2
1

题意:给定源点和汇点的情况下边不相交最短路的条数
题解:先用floyed算法将图中的点全部两点之间的距离全部算一遍,求出最短路,然后枚举原来图中的边,如果在最短路上,则连一条容量为1的边,算出最大流即可。
//zoj 2760
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <string.h>
#include <math.h>
#include <iostream>
#include <math.h>
using namespace std;
const int N = ;
const int INF = ;
struct Edge{
int v,next;
int w;
}edge[*N*N];
int head[N];
int level[N];
int graph[N][N],dis[N][N];
int tot;
void init(){
memset(head,-,sizeof(head));
tot=;
}
void addEdge(int u,int v,int w,int &k){
edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++;
edge[k].v = u,edge[k].w=,edge[k].next=head[v],head[v]=k++;
}
int BFS(int src,int des){
queue<int >q;
memset(level,,sizeof(level));
level[src]=;
q.push(src);
while(!q.empty()){
int u = q.front();
q.pop();
if(u==des) return ;
for(int k = head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
int w = edge[k].w;
if(level[v]==&&w!=){
level[v]=level[u]+;
q.push(v);
}
}
}
return -;
}
int dfs(int u,int des,int increaseRoad){
if(u==des) return increaseRoad;
int ret=;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
int w = edge[k].w;
if(level[v]==level[u]+&&w!=){
int MIN = min(increaseRoad-ret,w);
w = dfs(v,des,MIN);
if(w>){
edge[k].w -=w;
edge[k^].w+=w;
ret+=w;
if(ret==increaseRoad) return ret;
}else level[v] = -;
}
}
return ret;
}
int Dinic(int src,int des){
int ans = ;
while(BFS(src,des)!=-) ans+=dfs(src,des,INF);
return ans;
}
void floyd(int n){
for(int k=;k<n;k++){
for(int i=;i<n;i++){
for(int j=;j<n;j++){
dis[i][j] = min(dis[i][k]+dis[k][j],dis[i][j]);
}
}
}
}
void build(int s,int t,int N){
int i,j;
for(i=;i<N;i++){
if(dis[s][i]==INF)continue;
for(j=;j<N;j++){
if(i==j||dis[j][t]==INF||dis[i][j]==INF)continue;
if(dis[s][t]==dis[s][i]+graph[i][j]+dis[j][t]){
addEdge(i,j,,tot);
}
}
}
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
init();
for(int i=;i<n;i++){
for(int j=;j<n;j++){
int w;
scanf("%d",&graph[i][j]);
if(graph[i][j]==-) graph[i][j]=INF;
if(i==j) graph[i][j] = ;
dis[i][j] = graph[i][j];
}
}
int src,des;
scanf("%d%d",&src,&des);
if(src==des) {
printf("inf\n");continue;
}
floyd(n);
build(src,des,n);
int ans = Dinic(src,des);
printf("%d\n",ans);
}
}

zoj 2760(网络流+floyed)的更多相关文章

  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. ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]

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

  4. ZOJ 2760 How Many Shortest Path(最短路径+最大流)

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

  5. ZOJ 2532 网络流最小割

    求最小割的问题. 题意:已知网络中有n个源点,m的中转站(也就是节点),一个汇点(编号为0).给出网络,求一些边(增大这个边就可以增大汇点流量的边). 思路:一开始代码只找了有流=0就加入输出数组的情 ...

  6. zoj 2676 网络流+01分数规划

    思路: 这题的结论得要看amber的论文,结论就是将求f(x)/b(x)最小转化为求min(f(x)-b(x)*λ),其中x为S集的解空间,f(x)为解的边权和,b(x)为解的边数, λ=f(x)/b ...

  7. ZOJ 2760 How Many Shortest Path

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

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

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

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

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

随机推荐

  1. centOS下lnamp安装

    首先安装apache,mysql ,最后安装php 1>apache安装 安装:yum install -y httpd 运行:/bin/systemctl start httpd.servic ...

  2. python入门:求1-2+3-4+5...99的所有数的和(自写)

    #!/usr/bin/env pyhton # -*- coding:utf-8 -*- #求1-2+3-4+5...99的所有数的和(自写) """ 给x赋值为0,给y ...

  3. python-numpy-pandas

    目录 numpy 模块 创建矩阵方法: 获取矩阵的行列数 切割矩阵 矩阵元素替换 矩阵的合并 通过函数创建矩阵 矩阵的运算 pandas模块 series (一维列表) DataFrame DataF ...

  4. 有关nmap的5个常用的扫描指令

    [以下IP可替换成需要被测试的IP网段] 1.ping扫描:扫描192.168.0.0/24网段上有哪些主机是存活的: nmap -sP 192.168.0.0/24   2.端口扫描:扫描192.1 ...

  5. Linux学习-开放源码的软件安装与升级简介

    什么是开放源码.编译程序与可执行文件 我们说过,在 Linux 系统上面,一个文件能不能被执行看的是有没有可执行的那个权限 (具有 x permission),不过,Linux 系统上真 正认识的可执 ...

  6. HDU 3667 费用流 拆边 Transportation

    题意: 有N个城市,M条有向道路,要从1号城市运送K个货物到N号城市. 每条有向道路<u, v>运送费用和运送量的平方成正比,系数为ai 而且每条路最多运送Ci个货物,求最小费用. 分析: ...

  7. 实现socket并发的几种方法

    # 使用多进程实现socket聊天并发-server #服务端 import socket from multiprocessing import Process def server(conn,ad ...

  8. 反转单词顺序 VS 左旋转字符串

    题目一:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变.为简单起见,标垫符号和普通字母一样处理.例如输入字符串“I am a student.”,则输出“student. a am I ...

  9. webdriver高级应用- 操作富文本框

    富文本框的技术实现和普通的文本框的定位存在较大的区别,富文本框的常见技术用到了Frame标签,并且在Frame里面实现了一个完整的HTML网页结构,所以使用普通的定位模式将无法直接定位到富文本框对象. ...

  10. “万恶”的break

    写这篇随笔主要是希望自己长点记性,break的作用是跳出当次循环,每次用break都有点忘记break后面的条件提前. 正常是这样: exit_flag=Falsefor i in range(10) ...