题目链接:https://vjudge.net/contest/299467#problem/O

题目思路:网络流+最短路

这个是一个最短路+最大流,最短路容易,就是跑起点到每一个点的距离。

但是这个最大流的图难想,题解说的是,把每一个最短路的点放到网络流的图中,流量设置为1 ,然后跑一个最大流。

看了题解后,只有感觉题解特别正确,这个就是本来一个图里面有很多没有用的边,就是不能算作最短路里面的,

当我们把最短路的点放到了网络流中,然后通过跑最大流来求路径条数。

这个想清楚之后是真的好简单。

因为这个最大流这个可以不断变形,所以就有点难想啊。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
#include <string>
#include <map>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + ;
const int INF = 0x3f3f3f3f;
struct edge
{
int u, v, c, f;
edge(int u, int v, int c, int f) :u(u), v(v), c(c), f(f) {}
};
vector<edge>e;
vector<int>G[maxn];
int level[maxn];//BFS分层,表示每个点的层数
int iter[maxn];//当前弧优化
void init()
{
for (int i = ; i <= maxn; i++)G[i].clear();
e.clear();
}
void addedge(int u, int v, int c)
{
e.push_back(edge(u, v, c, ));
e.push_back(edge(v, u, , ));
int m = e.size();
G[u].push_back(m - );
G[v].push_back(m - );
//printf("%d %d %d\n", u, v, c);
}
void BFS(int s)//预处理出level数组
//直接BFS到每个点
{
memset(level, -, sizeof(level));
queue<int>q;
level[s] = ;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int v = ; v < G[u].size(); v++)
{
edge& now = e[G[u][v]];
if (now.c > now.f && level[now.v] < )
{
level[now.v] = level[u] + ;
q.push(now.v);
}
}
}
}
int dfs(int u, int t, int f)//DFS寻找增广路
{
if (u == t)return f;//已经到达源点,返回流量f
for (int &v = iter[u]; v < G[u].size(); v++)
//这里用iter数组表示每个点目前的弧,这是为了防止在一次寻找增广路的时候,对一些边多次遍历
//在每次找增广路的时候,数组要清空
{
edge &now = e[G[u][v]];
if (now.c - now.f > && level[u] < level[now.v])
//now.c - now.f > 0表示这条路还未满
//level[u] < level[now.v]表示这条路是最短路,一定到达下一层,这就是Dinic算法的思想
{
int d = dfs(now.v, t, min(f, now.c - now.f));
if (d > )
{
now.f += d;//正向边流量加d
e[G[u][v] ^ ].f -= d;
//反向边减d,此处在存储边的时候两条反向边可以通过^操作直接找到
return d;
}
}
}
return ;
}
int Maxflow(int s, int t)
{
int flow = ;
for (;;)
{
BFS(s);
if (level[t] < )return flow;//残余网络中到达不了t,增广路不存在
memset(iter, , sizeof(iter));//清空当前弧数组
int f;//记录增广路的可增加的流量
while ((f = dfs(s, t, INF)) > )
{
flow += f;
}
}
return flow;
} struct node
{
int from,to, dist;
node(int from=,int to=,int dist=):from(from),to(to),dist(dist){}
}exa[maxn]; struct heapnode
{
int u, d;
heapnode(int u=,int d=):u(u),d(d){}
bool operator<(const heapnode&a)const
{
return a.d < d;
}
}; vector<node>vec[maxn];
int d[maxn], n, m;
bool vis[maxn];
void dij(int s)
{
priority_queue<heapnode>que;
que.push(heapnode(s, ));
memset(vis, , sizeof(vis));
memset(d, inf, sizeof(d));
d[s] = ;
while(!que.empty())
{
heapnode x = que.top(); que.pop();
int u = x.u;
if (vis[u]) continue;
vis[u] = ;
for(int i=;i<vec[u].size();i++)
{
node now = vec[u][i];
if(d[now.to]>d[u]+now.dist)
{
d[now.to] = d[u] + now.dist;
que.push(heapnode(now.to, d[now.to]));
}
}
}
} int main()
{
int w;
scanf("%d", &w);
while(w--)
{
init();
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++) vec[i].clear();
for(int i=;i<=m;i++)
{
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
vec[u].push_back(node(u, v, c));
exa[i] = node(u, v, c);
}
int s, t;
scanf("%d%d", &s, &t);
dij(s);
for(int i=;i<=m;i++)
{
node now = exa[i];
if (d[now.to] == d[now.from] + now.dist) addedge(now.from, now.to, );
}
int ans = Maxflow(s, t);
printf("%d\n", ans);
}
return ;
}

网络流 O - Marriage Match IV的更多相关文章

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

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

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

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

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

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

  4. HDU3605:Marriage Match IV

    Marriage Match IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

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

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

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

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

  7. Q - Marriage Match IV (非重复最短路 + Spfa + 网络最大流Isap)

    Q - Marriage Match IV Do not sincere non-interference. Like that show, now starvae also take part in ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3416 题意: 给出含n个点.m条有向边的图,每条边只能走一次,给出起点和终点,求起点到终点的最短路径有 ...

  9. HDU 3416 Marriage Match IV(最短路,网络流)

    题面 Do not sincere non-interference. Like that show, now starvae also take part in a show, but it tak ...

随机推荐

  1. csdn的垃圾体验

    微信扫码登录网页csdn,每次扫码都是csdn有关的不同的公众号,必须关注才可以登录,为了推广公众号真是简直了 无法修改id 注销也需要扫码,这次是必须下载csdn的app才能注销,我真是服了,我都要 ...

  2. Python分析数据难吗?某科技大学教授说,很难但有方法就简单

    用python分析数据难吗?某科技大学的教授这样说,很难,但要讲方法,主要是因为并不是掌握了基础,就能用python来做数据分析的. 所谓python的基础,也就是刚入门的python学习者,学习的基 ...

  3. Julia基础语法复数和分数

     1.复数   2.分数

  4. stand up meeting 12/11/2015

    part 组员 今日工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云 完成单词释义热度排序:允许用户自主添加释义:完成了button位置的修正(finally)和弹窗的美化:     6 tr ...

  5. BUUOJ [极客大挑战 2019]Secret File

    [极客大挑战 2019]Secret File 0X01考点 php的file伪协议读取文件 ?file=php://filter/convert.base64-encode/resource= 0X ...

  6. MyBatis model、xml、mapper 自动生成,附源码

    Mybatis 代码自动生成 model.xml.mapper 代码结构图 代码地址 https://github.com/shootercheng/codegen 需要修改的地方见 readme

  7. Ubuntu当状态栏网络图标隐藏的解决方法汇总

    最有效之一: 直接在终端运行以下命令,以root身份: nm-applet --sm-disable 不建议修改配置文件内容

  8. linux下批量删除文件

    1. 在linux批量删除多级目录下同一格式的文件,可采用find + exec命令组合: 如在删除old目录下的,所有子目录中,后缀为.l的文件方法为: find old -type f -name ...

  9. golang/beego 微信模版消息

    // GO的微信SDK我用的是这个:https://github.com/silenceper/wechat // 发送模版消息 // UserNickName,UserMobile是发起预约的人的昵 ...

  10. 通过纯css实现图片居中的多种实现方式

    html结构: <div class="demo" style="width: 800px;height: 600px; border:1px solid #ddd ...