zoj 2760 How Many Shortest Path【最大流】
不重叠最短路计数。
先弗洛伊德求一遍两两距离(其实spfa或者迪杰斯特拉会更快但是没必要懒得写),然后设dis为st最短距离,把满足a[s][u]+b[u][v]+a[v][t]==dis的边(u,v)连流量为1的边,表示只能走一次。注意这里a数组是弗洛伊德之后的,b是边的原长,然后跑一边最大流即可。
注意两点
- 特判掉s不能到达t的情况,直接输出0
- 弗洛伊德之前把所有数组中形如[i][i]的全部置为0,输入可能有trick
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=205,inf=1e8;
int n,a[N][N],h[N],cnt,s,t,le[N],b[N][N];
bool v[N];
struct qwe
{
int ne,to,va;
}e[N*N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v,int w)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
e[cnt].va=w;
h[u]=cnt;
}
void ins(int u,int v,int w)
{//cout<<u<<" "<<v<<endl;
add(u,v,w);
add(v,u,0);
}
bool bfs()
{
memset(le,0,sizeof(le));
queue<int>q;
le[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=h[u];i;i=e[i].ne)
if(!le[e[i].to]&&e[i].va>0)
{
le[e[i].to]=le[u]+1;
q.push(e[i].to);
}
}
return le[t];
}
int dfs(int u,int f)
{
if(u==t||!f)
return f;
int us=0;
for(int i=h[u];i&&us<f;i=e[i].ne)
if(le[e[i].to]==le[u]+1&&e[i].va>0)
{
int t=dfs(e[i].to,min(e[i].va,f-us));
e[i].va-=t;
e[i^1].va+=t;
us+=t;
}
return us;
}
int dinic()
{
int re=0;
while(bfs())
re+=dfs(s,inf);
return re;
}
int main()
{
while(~scanf("%d",&n))
{
memset(h,0,sizeof(h));
cnt=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
int x=read();
a[i][j]=(x==-1)?inf:x;
b[i][j]=a[i][j];
}
for(int i=1;i<=n;i++)
a[i][i]=b[i][i]=0;//!!!!!!!!!!!!!!!!!
s=read()+1;t=read()+1;
if(s==t)
{
puts("inf");
continue;
}
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(a[i][j]>a[i][k]+a[k][j])
a[i][j]=a[i][k]+a[k][j];
int dis=a[s][t];// cout<<dis<<endl;
if(dis==inf)//!!!!!!!!!!!!!!!!
{
puts("0");
continue;
}
for(int u=1;u<=n;u++)
for(int v=1;v<=n;v++)
if(u!=v&&a[s][u]+b[u][v]+a[v][t]==dis)
ins(u,v,1);
printf("%d\n",dinic());
}
return 0;
}
zoj 2760 How Many Shortest Path【最大流】的更多相关文章
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- 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 (不相交的最短路径个数)
[题意]给定一个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 ...
随机推荐
- HDU1533 最小费用最大流
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- JVM内存区域(运行时数据区)划分
前言: 我们每天都在编写Java代码,编译,执行.很多人已经知道Java源代码文件(.java后缀)会被Java编译器编译为字节码文件(.class后缀),然后由JVM中的类加载器加载各个类的字节码文 ...
- Eclipse-Java代码规范和质量检查插件-Checkstyle
CheckStyle是SourceForge下的一个项目,提供了一个帮助JAVA开发人员遵守某些编码规范的工具.它能够自动化代码规范检查过程,从而使得开发人员从这项重要但枯燥的任务中解脱出来.它可以根 ...
- chapter1:using neural nets to recognize handwritten digits
two important types of artificial neuron :the perceptron and the sigmoid neuron Perceptrons 感知机的输入个数 ...
- Centos7 Samba 独立账户
创建了一个组:smbgrp 和用户srijan通过认证来访问Samba服务器. groupadd smbgrp useradd srijan -G smbgrp smbpasswd -a srijan ...
- [TypeScript] Transform Existing Types Using Mapped Types in TypeScript
Mapped types are a powerful and unique feature of TypeScript's type system. They allow you to create ...
- Dom对象的经常用法
Dom对象的经常用法: (1)getElementById() 查询给定ID属性值的元素,返回该元素的元素节点 1. 查询给定ID属性值的元素,返回该元素的元素节点.也称为元素对象. ...
- http://vdceye.com/ 全新页面上线
vdceye manager home page
- ADO.NET(OleDb)读取Excel表格时的一个BUG
如果我们有例如以下一个Excel表格: 如今要使用C#程序读取其内容: using System; using System.Data.OleDb; namespace Skyiv.Be ...
- 【git体验】git原理及基础
原理:分布式版本号控制系统像 Git,Mercurial,Bazaar 以及 Darcs 等,client并不仅仅提取最新版本号 的文件快照,而是把原始的代码仓库完整地镜像下来. 这么一来.不论什么一 ...