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 ...
随机推荐
- Python 学习 第二篇:数据类型(字符串)
字符串是一个字符的.有序的.不可变的序列,用于存储基于文本的信息.字符串所包含的字符存在从左至右的位置顺序,不可以在原处(in-place)修改.Python没有C语言的字符和字符串之分,只有字符串. ...
- 基于Cocos2d-x-1.0.1的飞机大战游戏迁移到Cocos2d-x-3.0版本,并移植到Android平台成功运行
一.版本迁移中的问题 1.游戏元素Sprite.Label.Action等等的创建函数名都改为create. 2.函数的回调callfunc_selectorcallfuncN_selectorcal ...
- .Net Core Linux centos7行—jenkins linux 构建.net core web app
1.安装jdk.jenkins 是一个java web程序.所以必然需要jdk. yum install java 或者 yum install java-1.8.0-openjdk 2.下载jenk ...
- POJ--3279(开关问题2个不同时间写的代码)
Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19730 Accepted: 7118 Descrip ...
- SQLMAP学习笔记1 access注入
SQLMAP学习笔记1 access注入 Sqlmap是开源的自动化SQL注入工具,由Python写成,具有如下特点: 完全支持MySQL.Oracle.PostgreSQL.Microsoft S ...
- Markdown 版本演进
本文作为 Markdown 系列的第二篇,对上一篇使用 Markdown 写技术博客,我踩过的 6个坑博客提到的版本变迁进行简要的提纲说明. 如果不想读文章,请直接看思维导图,使用 Atom + ma ...
- Hyperledger Fabric -- gossip 协议
Hyperledger gossip 本文记述了Hyperledger Fabric 中 一种网络数据同步协议--gossip,它的主要作用是致力于账本数据的安全传输,保证不同节点之间状态的同步和 ...
- 【python 2.7】输入任意字母数字,输出其对应的莫尔斯码并播放声音
#python 2.7 #!/usr/bin/env python # -*- coding:utf-8 -*- import os import winsound,sys,time __author ...
- iOS 开发中,关于xxx.xcodeproj 文件冲突的解决方案 (以后谁不会了,直接将连接给他)
iOS 开发中,关于xxx.xcodeproj 文件冲突的解决方案 (一有冲突要手把手教一遍,太麻烦了,现在总结下,以后谁不会了,连接直接发他). 关于xxx.xcodeproj 文件冲突的话,是比较 ...
- Notes of Daily Scrum Meeting(11.19)
Notes of Daily Scrum Meeting(11.19) 现在工程项目进入尾声了,我们的项目中还有一些问题需要解决,调试修改起来进度比较慢,所以昨天就没有贴出项目 进度,今天的团队工作总 ...