题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3416

题意:

给出含n个点、m条有向边的图,每条边只能走一次,给出起点和终点,求起点到终点的最短路径有多少条。

思路:

题目要求是最短路径,当然需要求出最短路,用Dijkstra就可以了,然后我们需要构造网络流的图。将能组成最短路的边加入图中,容量设为1,注意能组成最短路的边是满足dis[u] + edge[i].dist == dis[v] 的边,其中u是边的起点,v是边的终点,dis[]保存的是最短路。最后跑最大流即可。

代码如下:

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue> using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=;
const int maxm=;
struct node
{
int d,u;
friend bool operator<(node a,node b)
{
return a.d>b.d;
}
node(int dist,int point):d(dist),u(point){}
}; struct Edge1
{
int to,next;
int dist;
}edge1[maxm];
int head1[maxn],tot;
int pre[maxn],dis[maxn];
bool vis[maxm]; void init1()
{
memset(head1,-,sizeof(head1));
tot=;
} void addedge1(int u,int v,int d)
{
edge1[tot].to=v;
edge1[tot].dist=d;
edge1[tot].next=head1[u];
head1[u]=tot++;
} void Dijkstra(int s)
{
priority_queue<node> q;
memset(dis,0x3f,sizeof(dis));
memset(pre,-,sizeof(pre));
dis[s]=;
while(!q.empty())
q.pop();
node a(,s);
q.push(a);
while(!q.empty())
{
node x=q.top();
q.pop();
if(dis[x.u]<x.d)
continue;
for(int i=head1[x.u];i!=-;i=edge1[i].next)
{
int v=edge1[i].to;
if(dis[v]>dis[x.u]+edge1[i].dist)
{
dis[v]=dis[x.u]+edge1[i].dist;
pre[v]=x.u;
q.push(node(dis[v],v));
}
}
}
}
const int MAXN = ;
const int MAXM = ;
struct Temp
{
int u,v;
}temp[MAXM];
int cnt;
void dfs(int u)
{
for(int i=head1[u];i!=-;i=edge1[i].next)
{
int v=edge1[i].to;
if(dis[u]+edge1[i].dist==dis[v]&&(!vis[i]))
{
temp[cnt].u=u;
temp[cnt++].v=v;
vis[i]=true;
dfs(v);
}
}
} struct Edge2
{
int to, next, cap, flow;
}edge[MAXM];
int tol;
int head[MAXN];
void init()
{
tol = ;
memset(head, -, sizeof(head));
}
void addedge(int u, int v, int w, int rw=)
{
edge[tol].to = v; edge[tol].cap = w; edge[tol].flow = ;
edge[tol].next = head[u]; head[u] = tol++;
edge[tol].to = u; edge[tol].cap = rw; edge[tol].flow = ;
edge[tol].next = head[v]; head[v] = tol++;
}
int Q[MAXN];
int dep[MAXN], cur[MAXN], sta[MAXN];
bool bfs(int s, int t, int n)
{
int front = , tail = ;
memset(dep, -, sizeof(dep[])*(n+));
dep[s] = ;
Q[tail++] = s;
while(front < tail)
{
int u = Q[front++];
for(int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap > edge[i].flow && dep[v] == -) {
dep[v] = dep[u] + ;
if(v == t) return true;
Q[tail++] = v;
}
}
}
return false;
}
int dinic(int s, int t, int n) {
int maxflow = ;
while(bfs(s, t, n)) {
for(int i = ; i < n; i++) cur[i] = head[i];
int u = s, tail = ;
while(cur[s] != -)
{
if(u == t)
{
int tp = INF;
for(int i = tail-; i >= ; i--)
tp = min(tp, edge[sta[i]].cap-edge[sta[i]].flow);
maxflow+=tp;
for(int i = tail-; i >= ; i--) {
edge[sta[i]].flow+=tp;
edge[sta[i]^].flow-=tp;
if(edge[sta[i]].cap-edge[sta[i]].flow==)
tail = i;
}
u = edge[sta[tail]^].to;
}
else
if(cur[u] != - && edge[cur[u]].cap > edge[cur[u]].flow && dep[u] + == dep[edge[cur[u]].to])
{
sta[tail++] = cur[u];
u = edge[cur[u]].to;
}
else
{
while(u != s && cur[u] == -)
u = edge[sta[--tail]^].to;
cur[u] = edge[cur[u]].next;
}
}
}
return maxflow;
}
int n,m,a,b; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init1();
scanf("%d%d",&n,&m);
for(int i=;i<=m;++i)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
if(u!=v)
addedge1(u,v,c);
}
scanf("%d%d",&a,&b);
Dijkstra(a);
memset(vis,false,sizeof(vis));
cnt=;
dfs(a);
init();
for(int i=;i<cnt;++i)
addedge(temp[i].u-,temp[i].v-,);
int ans=dinic(a-,b-,n);
cout<<ans<<endl;
}
return ;
}

hdu3416 Marriage Match IV(最短路+网络流)的更多相关文章

  1. Marriage Match IV(最短路+网络流)

    Marriage Match IV http://acm.hdu.edu.cn/showproblem.php?pid=3416 Time Limit: 2000/1000 MS (Java/Othe ...

  2. HDU-3416 Marriage Match IV 最短路+最大流 找各最短路的所有边

    题目链接:https://cn.vjudge.net/problem/HDU-3416 题意 给一个图,求AB间最短路的条数(每一条最短路没有重边.可有重复节点) 思路 首先把全部最短路的边找出来,再 ...

  3. hdu3416 Marriage Match IV 最短路+ 最大流

    此题的大意:给定一幅有向图,求起点到终点(都是固定的)的不同的最短路有多少条.不同的最短路是说不能有相同的边,顶点可以重复.并且图含有平行边. 看了题以后,就想到暴力,但是暴力往往是不可取的.(暴力的 ...

  4. HDU3416 Marriage Match IV —— 最短路径 + 最大流

    题目链接:https://vjudge.net/problem/HDU-3416 Marriage Match IV Time Limit: 2000/1000 MS (Java/Others)    ...

  5. hdu3416 Marriage Match IV【最短路+最大流】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297581.html   ---by 墨染之樱花 题目链接:http://acm.hdu.ed ...

  6. HDU 3416 Marriage Match IV (最短路建图+最大流)

    (点击此处查看原题) 题目分析 题意:给出一个有n个结点,m条单向边的有向图,问从源点s到汇点t的不重合的最短路有多少条,所谓不重复,意思是任意两条最短路径都不共用一条边,而且任意两点之间的边只会用一 ...

  7. HDU 3416 Marriage Match IV (最短路径,网络流,最大流)

    HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...

  8. HDU 3416 Marriage Match IV (求最短路的条数,最大流)

    Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...

  9. hdu 3416 Marriage Match IV (最短路+最大流)

    hdu 3416 Marriage Match IV Description Do not sincere non-interference. Like that show, now starvae ...

随机推荐

  1. Angular02 将数据添加到组件中

    准备:已经搭建好angular-cli环境.知道如何创建组件 一.将一个数据添加到组件中 1 创建一个新的组件 user-item 2 将组件添加到静态模板中 3 为组件添加属性,并利用构造器赋值 4 ...

  2. (转)PL SQL Developer 使用总结

    如果OS为windows 7 64位系统,Oracle版本为 Oracle 11g 64 安装PL SQL Developer 请参考    http://myskynet.blog.51cto.co ...

  3. 在CentOS7上部署OpenStack 步骤详解

    OpenStack作为一个由NASA(美国国家航空航天局)和Rackspace合作研发并发起的,开放源代码项目的云计算管理平台项目.具体知识我会在后面文章中做出介绍,本章主要按步骤给大家演示在Cent ...

  4. 整合微信小程序的Web API接口层的架构设计

    在我前面有很多篇随笔介绍了Web API 接口层的架构设计,以及对微信公众号.企业号.小程序等模块的分类划分.例如在<C#开发微信门户及应用(43)--微信各个项目模块的定义和相互关系>介 ...

  5. 假面舞会[NOI2008]

    题目描述 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择一 个自己喜欢的面具.每个面具都有一个编号,主办方会把此编号告 ...

  6. Java中基本数据类型和包装类

    参考:深入剖析Java中的装箱和拆箱; Java中基本数据类型和包装类互转中 缓冲机制的使用; java学习笔记:装箱和拆箱,包装器和缓冲池 Java 各 类型数据在内存中分配情况详解 一 java内 ...

  7. ubuntu下安装opencv库+Python2.7环境安装及开发摄像头拍照应用

    好久没有更新了,今天更一篇最近遇到的问题,及解决办法,后面博客得继续写起来 安装 #使用Python下的lib库直接进行安装 apt-get install python-opencv 测试 #使用如 ...

  8. LAP+mysql-主从+redis

    Redis是一个开源的,内存中的数据结构存储系统,他可以用作数据库,缓存和消息中间介.支持多种类型数据库结构,如字符串(strings),散列(hashes),列表(lists),集合(sets),有 ...

  9. SpringBoot填坑系列---XML方式配置数据库

    本次只是简单的运用SpringBoot搭建框架,对其原理并不做深入的探究 1.POM文件 <?xml version="1.0" encoding="UTF-8&q ...

  10. MySQL、Oracle数据库之操作系统版本选择

    玩了快五年的Oracle,期间接触的操作系统大都是linux和aix,其中linux大部分为5.8的红帽子以及centos,oracle可以在上边运行稳定且需要安装其他与oracle相关的rpm包都是 ...