不重叠最短路计数。

先弗洛伊德求一遍两两距离(其实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【最大流】的更多相关文章

  1. zoj 2760 How Many Shortest Path 最大流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...

  2. ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]

    人老了就比较懒,故意挑了到看起来很和蔼的题目做,然后套个spfa和dinic的模板WA了5发,人老了,可能不适合这种刺激的竞技运动了…… 题目链接:http://acm.zju.edu.cn/onli ...

  3. ZOJ 2760 How Many Shortest Path(最短路径+最大流)

    Description Given a weighted directed graph, we define the shortest path as the path who has the sma ...

  4. ZOJ 2760 How Many Shortest Path(Dijistra + ISAP 最大流)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 题意:给定一个带权有向图 G=(V, E)和源点 s.汇点 t ...

  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. .net core webapi jwt 更为清爽的认证 ,续期很简单(2)

    .net core webapi jwt 更为清爽的认证  后续:续期以及设置Token过期 续期: 续期的操作是在中间件中进行的,续期本身包括了前一个Token的过期加发放新的Token,所以在说续 ...

  2. babel 用法及其 .babelrc 的配置详解,想做前端架构,拒绝一知半解...

    Babel 官方介绍:将 ECMAScript 2015 及其版本以后的 javascript 代码转为旧版本浏览器或者是环境中向后兼容版本的  javascript 代码. 简而言之,就是把不兼容的 ...

  3. 查看linux接口进出口流量的命令;linux 网络监控;流量监控

    1.nload,左右键切换网卡 2.sudo iftop 3.sudo iptraf 按连接/端口查看流量 4.sudo nethogs: 按进程查看流量占用 5.ss: 连接查看工具 6.dstat ...

  4. Visual Studio Visual assistant注释也做拼写检查怎么办

    1 打开Visual Assistant   2 在Advanced中找到Underlines,取消勾选"Underline spelling errors in comments and ...

  5. 【Java 虚拟机探索之路系列】:JIT编译器

    作者:郭嘉 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.com/AllenWell 为 ...

  6. WebApi-路由机制 Visual Studio 2015中的常用调试技巧分享

    WebApi-路由机制   一.WebApi路由机制是什么? 路由机制通俗点来说:其实就是WebApi框架将用户在浏览器中输入的Url地址和路由表中的路由进行匹配,并根据最终匹配的路由去寻找并匹配相应 ...

  7. iOS 开发小常识 开发笔记

    一   自定义push方法 /*  参数说明 *  controllerName : push的目标页 例:@“testcontroll”    ---注意不带.h *  isNibPage     ...

  8. 一个很小的C++写的MVC的例子

    #include<iostream> #include<vector> //get namespace related stuff using std::cin; using ...

  9. Tomcat9无法启动

    闲来无事,重新学习一下Java, 去Tomcat官网下载Tomcat,各种版本,7-8-9,果断下载最新的9,解压后,无需安装,到bin文件夹下启动, 结果总是一闪而过,百度: 1.查看8080是否占 ...

  10. 第一个get请求的爬虫程序

    一:urllib库: urllib是Python自带的一个用于爬虫的库,器主要作用就是可以通过代码模拟浏览器发送请求.其被用到子模块在Python3中的urllib.request和urllib.pa ...