题目链接: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. ElasticSearch 常用查询语句

    为了演示不同类型的 ElasticSearch 的查询,我们将使用书文档信息的集合(有以下字段:title(标题), authors(作者), summary(摘要), publish_date(发布 ...

  2. leetcode c++做题思路和题解(5)——堆的例题和总结

    堆和优先队列 堆的简介, 是一种二叉树, 有最大堆和最小堆miniheap. 通常用于构建优先队列. 0. 目录 数据流中的第K大元素 1. 数据流中的第K大元素 数据流中的第K大元素 复杂度为log ...

  3. Nexus3 集成 crowd 插件

    公司使用的软件开发和协作工具为 Atlassian 系列软件,所以统一使用 crowd 来实现统一登录(SSO). crowd 配置 具体操作细节见我之前写的 Atlassian 系列软件安装(Cro ...

  4. ASE team work proposal

    Hi,我们是Azure Wrapper,欢迎来到我们的blog~我们将在这里记录下ASE课程的滴滴点点,美妙的旅程就要开始啦! 以下是每位队员提交的关于ASE 团队项目的提议: 朱玉影: 随着信息时代 ...

  5. H - Knight Moves DFS

    A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the sh ...

  6. 词向量模型word2vector详解

    目录 前言 1.背景知识 1.1.词向量 1.2.one-hot模型 1.3.word2vec模型 1.3.1.单个单词到单个单词的例子 1.3.2.单个单词到单个单词的推导 2.CBOW模型 3.s ...

  7. api测试用例(编写思路)

    在API的自动化测试维度中,测试维度分为两个维度,一个是单独的对API的验证,客户端发送一个请求后,服务端得到客户端的请求并且响应回复给客户端: 另外一个维度是基于业务场景的测试,基于业务场景的也就是 ...

  8. 3. JS生成32位随机数

    function randomWord ( randomFlag,min,max ) { var str = " ", range = min, arr = ['0','1','2 ...

  9. 靠!安装了macOS Catalina(10.15.4)后,文件系统都乱套了

    最近闲来无事,决定将我的两台apple电脑升级成最新的苹果系统(macOS Catalina),当然,由于以前升级过多次mac系统,所以毫不犹豫从app store下载了最新的macOS Cetali ...

  10. PHP把PNG图片转化为JPG时透明背景变黑色

    $type = exif_imagetype($srcimg); switch($type) { case 1: $simg = imagecreatefromgif($srcimg); break; ...