ZOJ2760_How Many Shortest Path
给一个图,求从某个点到另一个点的最短路有多少条?所有的路都不共边。
首先从终点开始Spfa标记最短距离,然后建图。
建图的时候,如果满足两点之间的最短路只差为两点之间的边长,那么在网络流的模型中连接一条边。
最终也只需要跑最大流即可。
注意此题没有要求不能经过同一个点,所有不需要拆点,由于我们在网络流的模型中间加边的时候边容量为1,也就保证了每条边只遍历一边了。
注意,有可能两个不同点之间的距离也为0,真是深坑啊,无法直视。
召唤代码君:
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 111
#define maxm 44444
typedef long long ll;
using namespace std; ll inf=~0U>>;
ll to[maxm],next[maxm],c[maxm],first[maxn],edge;
ll d[maxn],tag[maxn],TAG=;
ll Q[maxm],bot,top;
ll ans,n,s,t;
ll dis[maxn][maxn],f[maxn];
bool can[maxn]; void _input()
{
for (ll i=; i<=n; i++)
{
f[i]=inf,first[i]=-;
for (ll j=; j<=n; j++) scanf("%lld",&dis[i][j]);
}
scanf("%lld%lld",&s,&t);
s++,t++;
Q[bot=top=]=t,f[t]=;
while (bot<=top)
{
ll cur=Q[bot++];
for (ll i=; i<=n; i++)
if (dis[i][cur]>= && f[cur]+dis[i][cur]<f[i])
f[i]=f[cur]+dis[i][cur],Q[++top]=i;
}
} void addedge(ll U,ll V)
{
edge++;
to[edge]=V,c[edge]=,next[edge]=first[U],first[U]=edge;
edge++;
to[edge]=U,c[edge]=,next[edge]=first[V],first[V]=edge;
} void build_graph()
{
edge=-;
for (int i=; i<=n; i++)
for (int j=; j<=n; j++)
if (i!=j && dis[i][j]>= && f[i]==f[j]+dis[i][j])
addedge(i,j);
} bool bfs()
{
Q[bot=top=]=t,d[t]=,tag[t]=++TAG,can[t]=false;
while (bot<=top)
{
ll cur=Q[bot++];
for (ll i=first[cur]; i!=-; i=next[i])
if (c[i^]> && tag[to[i]]!=TAG)
{
tag[to[i]]=TAG,can[to[i]]=false;
d[to[i]]=d[cur]+,Q[++top]=to[i];
if (to[i]==s) return true;
}
}
return false;
} ll dfs(ll cur,ll num)
{
if (cur==t) return num;
ll tmp=num,k;
for (ll i=first[cur]; i!=-; i=next[i])
if (c[i]> && tag[to[i]]==TAG && d[to[i]]==d[cur]- && !can[to[i]])
{
k=dfs(to[i],min(num,c[i]));
if (k) num-=k,c[i]-=k,c[i^]+=k;
if (num==) break;
}
if (num) can[cur]=true;
return tmp-num;
} int main()
{
inf*=inf;
while (scanf("%lld",&n)!=EOF)
{
_input();
if (s==t)
{
puts("inf");
continue;
}
build_graph();
for (ans=; bfs(); ) ans+=dfs(s,inf);
printf("%lld\n",ans);
}
return ;
}
ZOJ2760_How Many Shortest Path的更多相关文章
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- Shortest Path(思维,dfs)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- Shortest Path
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- 【ZOJ2760】How Many Shortest Path
How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...
- [Swift]LeetCode847. 访问所有节点的最短路径 | Shortest Path Visiting All Nodes
An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...
随机推荐
- jquery validate 使用示例
var el = { $jsFrom: $('.js-form'), }; // 检测用户名是否存在 jQuery.validator.addMethod("isexist", f ...
- Object C学习笔记5-ARC forbids explicit message* 编译错误
在学习Object C的过程中XCode 编译器经常出现 "ARC forbids explicit message send of release" 的错误提示. 以上问题主要出 ...
- nginx 配置 ssl 双向证书
CA 根证书制作 # 创建 CA 私钥 openssl genrsa -out ca.key 2048 #制作 CA 根证书(公钥) openssl req -new -x509 -days 3650 ...
- request.getParameter()和request.getAttribute()的区别
request.getParameter("val_1");这是获取请求的参数,比如你在url上看到的?id=12&name=abc就是参数,如果是post请求,就看不到. ...
- python应用:生成简单二维码
概述 \(\quad\)第一篇python的应用就打算写一写用python生成简单的二维码啦.因为二维码在日常生活中越来越常用了,部分博客也用二维码来用作打赏的工具.但是要提醒大家的是,千万不要乱扫街 ...
- 【转】Linux系统下的ssh使用
Linux系统下的ssh使用(依据个人经验总结) 对于linux运维工作者而言,使用ssh远程远程服务器是再熟悉不过的了!对于ssh的一些严格设置也关系到服务器的安全维护,今天在此,就本人工作中使 ...
- c语言数字图像处理(七):频率域滤波
代码运行了两个小时才出的结果,懒得测试了,这一部分先鸽了,等对DFT算法进行优化后再更
- MergeSort 归并排序(java)
MergeSort 归并排序 排序思想:1,分解待排序的n个元素为两个子列,各为n/2个元素 2,若子列没有排好序,重复1步骤,每个子列继续分解为两个子列,直至被分解的子列个数为1 3,子列元素个数为 ...
- 前端开发利器 livereload -- 从此告别浏览器F5键
各位从事前端开发的童鞋们,大家每天coding && coding,然后F5 && F5,今天推荐一个静态文件在浏览器中自动更新的扩展 livereload,不同手动刷 ...
- NO.3_1:自学python之路------番外:第三方库安装、numpy
引言 Python因为pip的存在,使得第三方库的发布和获取都比较方便.并且Python对跨平台的支持,使得其相较于C++,Java更加方便使用.在本文中,将会介绍在Windows中安装第三方库的方法 ...