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

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.

题目描述:求一个有向图起点到终点的边不相交的最短路径的条数。

算法分析:floyd+最大流。针对网络流算法而建的模型中,s-t对应于实际中每一种方案,所以此题中的s-t就对应于题目中的一条源点到汇点的最短路径,最大流就是最短路径条数。

接下来就是怎么建模的问题:既然s-t对应于一条最短路径,那么s-t路径上的每一条边都是路径中的最短边。所以首先用floyd求出点到点的最短路径,然后枚举每条边判断是否是最短路径上的边,若是,则加入到新建的图中,权值为1。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=+; int n,from,to;
int dist[maxn][maxn],an[maxn][maxn];
int d[maxn],graph[maxn][maxn]; int bfs()
{
memset(d,,sizeof(d));
d[from]=;
queue<int> Q;
Q.push(from);
while (!Q.empty())
{
int u=Q.front() ;Q.pop() ;
for (int v= ;v<n ;v++)
{
if (!d[v] && graph[u][v]>)
{
d[v]=d[u]+;
Q.push(v);
if (v==to) return ;
}
}
}
return ;
} int dfs(int u,int flow)
{
if (u==to || flow==) return flow;
int cap=flow;
for (int v= ;v<n ;v++)
{
if (d[v]==d[u]+ && graph[u][v]>)
{
int x=dfs(v,min(cap,graph[u][v]));
cap -= x;
graph[u][v] -= x;
graph[v][u] += x;
if (cap==) return flow;
}
}
return flow-cap;
} int dinic()
{
int sum=;
while (bfs()) sum += dfs(from,inf);
return sum;
} int main()
{
while (scanf("%d",&n)!=EOF)
{
for (int i= ;i<n ;i++)
{
for (int j= ;j<n ;j++)
{
scanf("%d",&an[i][j]);
dist[i][j]=an[i][j];
}
dist[i][i]=an[i][i]=;
}
scanf("%d%d",&from,&to);
if (from==to) {printf("inf\n");continue; }
for (int k= ;k<n ;k++)
{
for (int i= ;i<n ;i++) if (i!=k)
{
for (int j= ;j<n ;j++) if (j!=k && j!=i)
{
if (dist[i][k]!=- && dist[k][j]!=- &&
(dist[i][j]==- || dist[i][j]>dist[i][k]+dist[k][j]))
dist[i][j]=dist[i][k]+dist[k][j];
}
}
}
//cout<<"dist[from][to]= "<<dist[from][to]<<endl;
if (dist[from][to]==-) {printf("0\n");continue; }
memset(graph,,sizeof(graph));
for (int i= ;i<n ;i++)
{
for (int j= ;j<n ;j++)
{
if (i!=j && dist[from][to]!=- && dist[from][i]!=- && dist[j][to]!=- && an[i][j]!=- &&
dist[from][to]==dist[from][i]+an[i][j]+dist[j][to])
graph[i][j]=;
}
}
printf("%d\n",dinic());
}
return ;
}

zoj 2760 How Many Shortest Path 最大流的更多相关文章

  1. ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]

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

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

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

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

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

  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. zoj How Many Shortest Path

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

  8. zoj 2760(网络流+floyed)

    How Many Shortest Path Time Limit: 10 Seconds      Memory Limit: 32768 KB Given a weighted directed ...

  9. hdu-----(2807)The Shortest Path(矩阵+Floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. Asp.net页面跳转Session丢失问题

    原本去年在做项目时,写好的一记篇博客分享给大家. Asp.net页面跳转Session丢失问题   编写人:CC阿爸 2014-4-2 l  近来在做泛微OA与公司自行开发的系统集成登录的问题.在使用 ...

  2. Knockout.Js官网学习(监控属性Observables)

    前言 1.创建一个ViewModel <script type="text/javascript"> //1.创建一个ViewModel var myViewModel ...

  3. 学习c的第6天2

    #include <stdio.h> #include <math.h> int main() { float num; printf("请输入该生当月的消费额:\n ...

  4. 分析MapReduce执行过程

    分析MapReduce执行过程 MapReduce运行的时候,会通过Mapper运行的任务读取HDFS中的数据文件,然后调用自己的方法,处理数据,最后输出. Reducer任务会接收Mapper任务输 ...

  5. php敏感词过滤

    在项目开发中发现有个同事在做敏感词过滤的时候用循环在判断,其实是不用这样做的,用php的数组函数和字符串函数即可实现 function filterNGWords($string) { $badwor ...

  6. 怎样用foreach去修改数组之中的数据

    $table_exchange=array(1,2,3,4,5,6,7,8); foreach ($table_exchange as $b=>$c){ $table_exchange[$b]= ...

  7. Mysql常用数据类型详细说明及实例说明(学习笔记一)

    1.Mysql 在windows下 Net start mysql[启动] Net stop mysql[停止] Quit[退出mysql命令行] \c[取消输入的命令] Select version ...

  8. 【推介】GitHub

    隆重推介:GitHub(https://github.com/) 作为开源代码库以及版本控制系统,Github拥有140多万开发者用户. 随着越来越多的应用程序转移到了云上,Github已经成为了管理 ...

  9. Keil(MDK-ARM)使用教程(三)_在线调试

    Ⅰ.概述 该文章总结Keil(MDK-ARM)在线调试相关的内容,详情请往下看. 该文章是基于新建好软件工程来讲述,关于Keil的下载.安装和新建工程我已将在前面做了详细的总结,不懂的可以参看我博客里 ...

  10. C#条件编译,发布多平台和多种选择性的项目

    http://www.cnblogs.com/chengulv/p/4579528.html 界面操作参考 这样正对不同环境就可以编译出不同的exe或者dll,做到一个项目的灵活多变.条件编译还可以满 ...