一、题目

  Path

二、分析

  首先肯定要求最短路,然后如何确定所有的最短路其实有多种方法。

  1 根据最短路,那么最短路上的边肯定是可以满足$dist[from] + e.cost = dist[to]$。所以可以求一遍后根据这个公式再向网络图中的加边即可。

  2 可以从源点和汇点分别求最短路,然后根据每条边肯定满足$dist1[e.from] + e.cost + dist2[e.to] = dij$,再加边就行了。

  确定最短路后,由于是求最小割,直接跑$Dinic$求出最大流就可以了。

三、AC代码

  1 #include <bits/stdc++.h>
2
3 using namespace std;
4 #define Min(a, b) ((a)>(b)?(b):(a))
5 #define P pair<ll, int>
6 #define ll long long
7 const ll INF = __LONG_LONG_MAX__;
8 const int maxn = 2e4 + 13;
9 const int maxm = 2e4 + 13;
10 int N, M;
11
12 struct edge
13 {
14 int to, cost;
15 };
16 struct edge2
17 {
18 ll to, cap, rev;
19 };
20 ll dist1[maxn], dist2[maxn];
21 vector<edge> G1[maxn], G2[maxn]; //G1正向图,G2反向图
22 vector<edge2> G[maxn << 2];
23
24 void addedge(ll from, ll to, ll cap)
25 {
26 G[from].push_back((edge2){to, cap, (ll)G[to].size()});
27 G[to].push_back((edge2){from, 0, (ll)G[from].size()-1});
28 }
29
30 void dijkstra()
31 {
32 priority_queue<P, vector<P>, greater<P>> pque;
33 pque.push(P(0, 1));
34 fill(dist1, dist1 + N + 2, INF);
35 dist1[1] = 0;
36 while(!pque.empty())
37 {
38 P q = pque.top();
39 pque.pop();
40 int v = q.second;
41 if(dist1[v] < q.first)
42 continue;
43 for(int i = 0; i < G1[v].size(); i++)
44 {
45 edge e = G1[v][i];
46 if(dist1[v] + e.cost < dist1[e.to])
47 {
48 dist1[e.to] = dist1[v] + e.cost;
49 pque.push(P(dist1[e.to], e.to));
50 }
51 }
52 }
53
54 // pque.push(P(0, N));
55 // fill(dist2, dist2 + N + 2, INF);
56 // dist2[N] = 0;
57 // while(!pque.empty())
58 // {
59 // P q = pque.top();
60 // pque.pop();
61 // int v = q.second;
62 // if(dist2[v] < q.first)
63 // continue;
64 // for(int i = 0; i < G2[v].size(); i++)
65 // {
66 // edge e = G2[v][i];
67 // if(dist2[v] + e.cost < dist2[e.to])
68 // {
69 // dist2[e.to] = dist2[v] + e.cost;
70 // pque.push(P(dist2[e.to], e.to));
71 // }
72 // }
73 // }
74 }
75
76 void getGraph()
77 {
78 dijkstra();
79 ll Dij = dist1[N];
80 for(int i = 1; i <= N; i++)
81 {
82 for(int j = 0; j < G1[i].size(); j++)
83 {
84 edge e = G1[i][j];
85 if(dist1[e.to] - dist1[i] == e.cost)
86 {
87 addedge((ll)i, (ll)e.to, (ll)e.cost);
88 }
89 }
90 }
91 // for(int i = 1; i <= N; i++)
92 // {
93 // for(int j = 0; j < G1[i].size(); j++)
94 // {
95 // edge e = G1[i][j];
96 // if(dist1[i] + e.cost + dist2[e.to] == Dij)
97 // {
98 // addedge((ll)i, (ll)e.to, (ll)e.cost);
99 // }
100 // }
101 // }
102 }
103
104 ll level[maxn], iter[maxn<<2];
105
106 void BFS(ll s)
107 {
108 memset(level, -1, sizeof(level));
109 queue<int> que;
110 que.push(s);
111 level[s] = 0;
112 while(!que.empty())
113 {
114 int v = que.front();
115 que.pop();
116 for(int i = 0; i < G[v].size(); i++)
117 {
118 edge2 e = G[v][i];
119 if(e.cap > 0 && level[e.to] < 0)
120 {
121 level[e.to] = level[v] + 1;
122 que.push(e.to);
123 }
124 }
125 }
126 }
127
128 ll DFS(ll v, ll t, ll f)
129 {
130 if(v == t)
131 return f;
132 for(ll &i = iter[v]; i < G[v].size(); i++)
133 {
134 edge2 &e = G[v][i];
135 if(e.cap > 0 && level[v] < level[e.to])
136 {
137 ll d = DFS(e.to, t, min(f, e.cap));
138 if(d > 0)
139 {
140 e.cap -= d;
141 G[e.to][e.rev].cap += d;
142 return d;
143 }
144 }
145 }
146 return 0;
147 }
148
149 ll Dinic(ll s, ll t)
150 {
151 ll flow = 0;
152 while(1)
153 {
154 BFS(s);
155 if(level[t] < 0)
156 return flow;
157 memset(iter, 0, sizeof(iter));
158 ll f = DFS(s, t, INF);
159 while(f > 0)
160 {
161 flow += f;
162 f = DFS(s, t, INF);
163 }
164 }
165 }
166
167 int main()
168 {
169 //freopen("input.txt", "r", stdin);
170 int T;
171 scanf("%d", &T);
172 while(T--)
173 {
174 int from, to, cost;
175 scanf("%d%d", &N, &M);
176 for(int i = 0; i <= N; i++)
177 {
178 G[i].clear();
179 G1[i].clear();
180 G2[i].clear();
181 }
182 for(int i = 0; i < M; i++)
183 {
184 scanf("%d%d%d", &from, &to, &cost);
185 G1[from].push_back( (edge){to, cost});
186 G2[to].push_back( (edge){from, cost});
187 }
188 getGraph();
189 printf("%lld\n", Dinic(1, N));
190 }
191
192 }

2019HDU多校第一场 6582 Path 【最短路+最大流最小割】的更多相关文章

  1. [2019杭电多校第一场][hdu6582]Path(最短路&&最小割)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6582 题意:删掉边使得1到n的最短路改变,删掉边的代价为该边的边权.求最小代价. 比赛时一片浆糊,赛后 ...

  2. 2019HDU多校第一场1001 BLANK (DP)(HDU6578)

    2019HDU多校第一场1001 BLANK (DP) 题意:构造一个长度为n(n<=10)的序列,其中的值域为{0,1,2,3}存在m个限制条件,表示为 l r x意义为[L,R]区间里最多能 ...

  3. [2019HDU多校第一场][HDU 6578][A. Blank]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题目大意:长度为\(n\)的数组要求分别填入\(\{0,1,2,3\}\)四个数中的任意一个,有 ...

  4. [2019HDU多校第一场][HDU 6580][C. Milk]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6580 题目大意:\(n\times m\)大小的方格上有\(k\)瓶水,喝完每瓶水都需要一定的时间.初 ...

  5. [2019HDU多校第一场][HDU 6584][G. Meteor]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6584 题目大意:求所有满足\(0<\frac{p}{q}\leq1, gcd(p,q)=1,p\ ...

  6. [2019HDU多校第一场][HDU 6590][M. Code]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6590 题目大意(来自队友):二维平面上有\(n\)个点,每个点要么是黑色要么是白色,问能否找到一条直线 ...

  7. [2019HDU多校第一场][HDU 6588][K. Function]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6588 题目大意:求\(\sum_{i=1}^{n}gcd(\left \lfloor \sqrt[3] ...

  8. 2019HDU多校第一场 BLANK DP

    题意:有四种数字,现在有若干个限制条件:每个区间中不同的数字种类必须是多少种,问合法的方案数. 思路: 定义 dp[i][j][k][t] 代表填完前 t 个位置后,{0,1,2,3} 这 4 个数字 ...

  9. 2019HDU多校第一场 String 贪心

    题意:给你一个字符串,问是否存在一个长度为m的子序列,子序列中对应字符的数目必须在一个范围内,问是否存在这样的字符串?如果存在,输出字典序最小的那个. 思路:贪心,先构造一个序列自动机,序列自动机指向 ...

随机推荐

  1. Redis之哨兵机制(sentinel)——配置详解及原理介绍

    说到Redis不得不提哨兵模式,那么究竟哨兵是什么意思?为什么要使用哨兵呢? 接下来一一为您讲解: 1.为什么要用到哨兵 哨兵(Sentinel)主要是为了解决在主从(master-slave)复制架 ...

  2. oslab oranges 一个操作系统的实现 实验一

    实验目的: 搭建基本实验环境,熟悉基本开发与调试工具 对应章节:第一.二章 实验内容: 1.认真阅读章节资料 2.在实验机上安装virtualbox,并安装ubuntu 3.安装ubuntu开发环境, ...

  3. IFIX 5.9 历史数据 曲线 (非SQL模式)

    装完 ifix 5.9 默认是没有Hist 开头的 历史数据源的,没存,至少我装的版本是这样. 那个Historian 也没有安装包,好像还要授权,自己研究不了. 1 先把数据存本地 在你的安装包里 ...

  4. 7816协议时序和采用UART模拟7816时序与智能卡APDU指令协议

    7816时序 7816时一个比较早的老通讯时序了,最近项目上需要用UART模拟所以,简单学习时序. 时序比较简单,熟悉UART的一眼看着就像是串口的时序,只是他没有停止位,取而代之的就是保护时间gur ...

  5. how to use brew install gpg

    how to use brew install gpg https://formulae.brew.sh/formula/gnupg $ brew install gnupg https://gith ...

  6. React Fragment All In One

    React Fragment All In One React还提供了一个无需包装即可呈现多个元素的组件. https://reactjs.org/docs/react-api.html#fragme ...

  7. Beacon API All In One

    Beacon API All In One Beacon API https://developer.mozilla.org/en-US/docs/Web/API/Beacon_API https:/ ...

  8. Web SQL& IndexedDB

    Web SQL& IndexedDB https://developer.mozilla.org/en/docs/Web/API/IndexedDB_API https://mdn.githu ...

  9. ES-Next & ECMAScript 2019

    ES-Next & ECMAScript 2019 Data Types 8 种 js 基本数据类型 8 zhon The latest ECMAScript standard defines ...

  10. NGK推出SPC算力币,开启算力新玩法!

    这两天,NGK公链再度上了热搜.因为既成功的打造DeFi生态以后,NGK又将目光对准了算力市场.试图通过算力代币化,让NGK算力持有者可以获得算力代币,同时,如果不想要了,算力持有者也可以抛售代币. ...