题目链接: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. 如何调节datagridview中字体

    设置ColumnHeaderDefaultCellStyle的Font属性 或者 编程 datagridview.Columns[index].DefaultCellStyle.Font.Size=“ ...

  2. C# 多线程 简单使用方法以及常用参数

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. 异步导出excel

    最近看园里有几篇写有关导出导入excel的博客,我正好最近在项目中也有涉及想来一起分享一下,正好整理一下自己的思路. 一.异步的方式是通过iframe来实现,代码如下: if ($('#downloa ...

  4. Css 书写规范【转】

    1. 不同浏览器元素的默认属性有所不同,使用Reset可重置浏览器元素的一些默认属性,以达到浏览器的兼容. /** 清除内外边距 **/ body, h1, h2, h3, h4, h5, h6, h ...

  5. Python核心编程--学习笔记--2--Python起步(上)

    本章是对Python的主要特性做一个快速介绍. 1 介绍 交互执行时,解释器有两种提示符: 主提示符(>>>):解释器在等待输入下一个语句: 次提示符(...):解释器在等待输入当前 ...

  6. 在openSUSE13.2上gem install rails -v 4.1成功,但是之后不存在rails命令解决

    解决方案为,不要用sudo gem install就好了,卧槽

  7. equals方法,hashcode()方法

    Object类的equals 方法 用来检测两个对象是否相等,即两个对象的内容是否相等,区分大小写.   (一)说到equals方法,不得不提一下==号. ==用于比较引用和比较原生数据类型时具有不同 ...

  8. Hive深入浅出

    1.  Hive是什么 1) Hive是什么? 这里引用 Hive wiki 上的介绍: Hive is a data warehouse infrastructure built on top of ...

  9. Linux软链接与硬链接

    1.Linux链接概念Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接. [硬连接]硬连接指通过索引节点 ...

  10. oracle 日志文件管理

    OS: [root@b28-122 ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 DB: SQL> select * f ...