给一个图,求从某个点到另一个点的最短路有多少条?所有的路都不共边。

首先从终点开始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的更多相关文章

  1. hdu-----(2807)The Shortest Path(矩阵+Floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. zoj 2760 How Many Shortest Path 最大流

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

  3. The Shortest Path in Nya Graph

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  4. hdu 3631 Shortest Path(Floyd)

    题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...

  5. Shortest Path(思维,dfs)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  6. Shortest Path

    Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  7. (中等) 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 ...

  8. 【ZOJ2760】How Many Shortest Path

    How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...

  9. [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 ...

随机推荐

  1. 开发中常遇到的linux系统配置操作整理

    一直以来,工作中使用xshell连接linux虚拟机.常常需要在虚拟机中搭建一个新的Linux系统,为了满足操作需要,必不可少的是一系列配置.之前对这些指令都是记录在云笔记,但是零零散散,每次用时,都 ...

  2. 5种处理js跨域问题方法汇总

    1.JSONP跨域GET请求 ajax请求,dataType为jsonp.这种形式需要请求在服务端调整为返回callback([json-object])的形式.如果服务端返回的是普通json对象.那 ...

  3. 怎样注册Docker Hub账号

    Docker Hub是Docker的远程镜像仓库,类似于GitHub;如果没有搭建本地私有仓库,Docker会默认去Docker Hub拉镜像. 访问Docker Hub官网https://hub.d ...

  4. kubenetes无法创建pod/创建RC时无法自动创建pod的问题

    一.问题概述 问题1: 虽然每次通过yaml创建rc都显示成功了,但是 kubectl get pod却没显示任何的pod. 问题2: 直接通过yaml创建pod提示apixxx 问题3: 通过.js ...

  5. Unity 实现一个简单的 TPS 相机

    效果如下: 代码如下: public class TPSCamera : MonoBehaviour { /// <summary> /// 目标对象 /// </summary&g ...

  6. 关于UC浏览器兼容scroll事件问题

    经过本人查阅无数资料,最终得出一个比较简单,具有一定兼容性的结果. $(window).scroll(function( ) { var scrollTop = document.documentEl ...

  7. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  8. 2.深入解析数据类型与变量——《Excel VBA 程序开发自学宝典》

    2.1 数据类型 数据类型 所占字节 Byte 1 Boolean 2 Integer 2 Long 4 Single 4 Double 8 Currency 8 Decimal 14 Date 8 ...

  9. [文章存档]Azure .net WebAPP的js/css文件过大导致访问慢的解决办法

    https://docs.azure.cn/zh-cn/articles/azure-operations-guide/app-service-web/aog-app-service-web-qa-j ...

  10. 高可用OpenStack(Queen版)集群-3.高可用配置(pacemaker&haproxy)

    参考文档: Install-guide:https://docs.openstack.org/install-guide/ OpenStack High Availability Guide:http ...