zoj 2760 How Many Shortest Path 最大流
题目链接: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 最大流的更多相关文章
- ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]
人老了就比较懒,故意挑了到看起来很和蔼的题目做,然后套个spfa和dinic的模板WA了5发,人老了,可能不适合这种刺激的竞技运动了…… 题目链接:http://acm.zju.edu.cn/onli ...
- ZOJ 2760 How Many Shortest Path(最短路径+最大流)
Description Given a weighted directed graph, we define the shortest path as the path who has the sma ...
- ZOJ 2760 How Many Shortest Path(Dijistra + ISAP 最大流)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 题意:给定一个带权有向图 G=(V, E)和源点 s.汇点 t ...
- zoj 2760 How Many Shortest Path【最大流】
不重叠最短路计数. 先弗洛伊德求一遍两两距离(其实spfa或者迪杰斯特拉会更快但是没必要懒得写),然后设dis为st最短距离,把满足a[s][u]+b[u][v]+a[v][t]==dis的边(u,v ...
- ZOJ 2760 How Many Shortest Path (不相交的最短路径个数)
[题意]给定一个N(N<=100)个节点的有向图,求不相交的最短路径个数(两条路径没有公共边). [思路]先用Floyd求出最短路,把最短路上的边加到网络流中,这样就保证了从s->t的一个 ...
- ZOJ 2760 How Many Shortest Path
题目大意:给定一个带权有向图G=(V, E)和源点s.汇点t,问s-t边不相交最短路最多有几条.(1 <= N <= 100) 题解:从源点汇点各跑一次Dij,然后对于每一条边(u,v)如 ...
- zoj How Many Shortest Path
How Many Shortest Path 题目: 给出一张图,求解最短路有几条.处理特别BT.还有就是要特别处理map[i][i] = 0,数据有不等于0的情况! 竟然脑残到了些错floyd! ! ...
- zoj 2760(网络流+floyed)
How Many Shortest Path Time Limit: 10 Seconds Memory Limit: 32768 KB Given a weighted directed ...
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
随机推荐
- php反射应用实例代码
php反射应用示例. 代码如下:<?php function custom(){ } class custom{ public function index(){ } } prin ...
- ubuntu 12.04 LTS(64位)安装apache2
在网上找了很多文章,说法不一,不知道应该用哪种方法,后来想就用其中最简单的吧,装不好再卸了重装, 然后就...装上了,用的是tar包源码安装,下面是步骤 1.去官网下载apache2.2.25源码包, ...
- 自定义获取html元素对象的7种方法。
- 内存分配函数malloc、realloc、calloc、_alloca
1.内存分配函数_alloca.malloc.realloc.calloc: _alloca 函数原型void * __cdecl _alloca(size_t); 头文件:malloc.h _all ...
- java-testng-selenium优化
由于项目中webui测试的需要,是用testng+selenium的方式,其中遇到过几个问题,记录下,方便以后查看 1.重复运行多次case 因为是selenium,所以有的时候需要运行多次,方法是写 ...
- About Inside the Azure Storage Outage of November 18th
Channel 9的官方解释 http://channel9.msdn.com/posts/Inside-the-Azure-Storage-Outage-of-November-18th 曾经在自己 ...
- Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- ElasticSearch版本升级备忘录(1.5.2至2.3.1)
discovery机制默认为单播,需配置discovery.zen.ping.unicast.hosts:,如果各节点使用默认端口,则只配IP即可(["172.17.4.47", ...
- c++基础(三):多态
virtual:虚函数.C++通过虚函数实现多态."无论发送消息的对象属于什么类,它们均发送具有同一形式的消息,对消息的处理方式可能随接手消息的对象而变"的处理方式被称为多态性.& ...
- iOS进阶学习-CoreData
一.CoreData数据库框架的优势 1.CoreData数据持久化框架是Cocoa API的一部分,首次在iOS5版本的系统中出现,它允许按照实体-属性-值模型组织数据,并以XML.二进制文件或者S ...