惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
博客园 - 三生石上(FineUI控件)
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
腾讯CDC
酷 壳 – CoolShell
酷 壳 – CoolShell
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Jina AI
Jina AI
爱范儿
爱范儿
博客园 - 【当耐特】
雷峰网
雷峰网
S
SegmentFault 最新的问题
美团技术团队
Blog — PlanetScale
Blog — PlanetScale
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
J
Java Code Geeks

Shiroha白羽的博客

Golang 踩坑 —— interface 为参数的时候传 nil 指针 Codeforces Round 925 (Div. 3) Codeforces Round 924 (Div. 2) Codeforces Round 923 (Div. 3) Codeforces Round 922 (Div. 2) Codeforces Round 921 (Div. 2) Educational Codeforces Round 161 (Rated for Div. 2) Codeforces Round 920 (Div. 3) Codeforces Round 919 (Div. 2) Hello 2024 Good Bye 2023 Codeforces Round 918 (Div. 4) 个人备份的常用 macOS 清理命令 Codeforces Round 917 (Div. 2) Pinely Round 3 (Div. 1 + Div. 2) Educational Codeforces Round 160 (Rated for Div. 2) Codeforces Round 915 (Div. 2) Codeforces Round 914 (Div. 2) Codeforces Round 913 (Div. 3) Educational Codeforces Round 159 (Rated for Div. 2) Codeforces Round 912 (Div. 2) Codeforces Round 911 (Div. 2) CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!) Educational Codeforces Round 158 (Rated for Div. 2) Codeforces Round 910 (Div. 2) Codeforces Round 909 (Div. 3) Codeforces Round 908 (Div. 2) Educational Codeforces Round 157 (Rated for Div. 2) C++自定义的字面量 Codeforces Round 907 (Div. 2)
2020牛客暑期多校训练营(第二场)I-Interval——最大流转对偶...
Shiroha · 2020-07-16 · via Shiroha白羽的博客

但是给出了一系列的限制 $l, r, dir, c$ ,表示当前区间为 $[l, r]$ 时,限制当前的区间不能进行操作 $1$(dir = L)或者操作 $2$ (dir = R),而启用这个限制则需要 $c$ 的费用,你可以选择是否启用这个限制

从 $1, n$ 能否转变为 $l = r$ 可以通过最短路来求算。但是无法求知当最短路无法到达时(即题目要求的不能转变)最少需要多少的限制条件,而这些条件又是什么。所以采用最大流来解决

画出网格图
将所有可以转换的两个状态之间用边连接,如果有提供限制的,将流量限制为费用,如果没有限制的,则设置为 $INF$
对于整个矩阵而言,只需要一半的点用于建图,所以将汇点放在另外一半点中。所有 $l = r$ 的点与汇点连接,而源点为 $[1, n]$
对于样例可以得到如下图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <bits/stdc++.h>

using namespace std;

#define ll long long
const int maxn = 510;

int n, m;
ll dis[maxn * maxn];
char si;
vector<pair<ll, int>> G[maxn * maxn];

void addedge(int u, int v, int cost) {
G[u].push_back({cost, v});
}

ll dijkstra(int s, int t) {
memset(dis, 0x3f, sizeof(dis));
dis[s] = 0;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;
q.push({0ll, s});
while (!q.empty()) {
ll u = q.top().second, c = q.top().first;
q.pop();
if (dis[u] < c)continue;
for (auto i : G[u]) {
ll cc = i.first, v = i.second;
if (dis[v] > dis[u] + cc) {
dis[v] = dis[u] + cc;
q.push({dis[v], v});
}
}
}
return dis[t];
}

inline int id(int x, int y) {
return x * (n + 3) + y;
}

void solve() {
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u, v, w;
char c;
cin >> u >> v >> c >> w;
if (c == 'L') {
addedge(id(u, v), id(u, v + 1), w);
addedge(id(u, v + 1), id(u, v), w);
} else {
addedge(id(u, v), id(u - 1, v), w);
addedge(id(u - 1, v), id(u, v), w);
}
}

for (int i = 1; i <= n; ++i) {
addedge(id(0, 0), id(0, i), 0);
addedge(id(i, n + 1), id(n + 1, n + 1), 0);
}

dijkstra(id(0, 0), id(n + 1, n + 1));
if (dis[id(n + 1, n + 1)] >= 0x3f3f3f3f3f3f3f3f)
cout << -1 << endl;
else
cout << dis[id(n + 1, n + 1)] << endl;
}

signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef ACM_LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
int test_index_for_debug = 1;
char acm_local_for_debug;
while (cin >> acm_local_for_debug) {
if (acm_local_for_debug == '$') exit(0);
cin.putback(acm_local_for_debug);
if (test_index_for_debug > 20) {
throw runtime_error("Check the stdin!!!");
}
auto start_clock_for_debug = clock();
solve();
auto end_clock_for_debug = clock();
cout << "Test " << test_index_for_debug << " successful" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
}
#else
solve();
#endif
return 0;
}