「CF852D」 Exploration plan
Posted zsbzsb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了「CF852D」 Exploration plan相关的知识,希望对你有一定的参考价值。
传送门
Luogu
解题思路
先跑一遍 ( ext{Floyd}) 预处理任意两点距离。
然后再二分时间,将每个人与该时间内可以到达的点连边,建一张二分图。
若最大匹配数大于等于所需,那么就缩小二分范围,最后记得判无解,然后输出答案即可。
细节注意事项
- 网络流好像不是很好写这道题?
参考代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
const int _ = 700;
const int INF = 2147483647;
int n, m, p, k, x[_];
int dis[_][_], vis[_], bel[_], g[_][_];
inline int dfs(int u) {
for (rg int i = 1; i <= n; ++i) {
if (vis[i] || !g[u][i]) continue;
vis[i] = 1;
if (bel[i] == 0 || dfs(bel[i]))
return bel[i] = u, 1;
}
return 0;
}
inline bool check(int mid) {
memset(g, 0, sizeof g);
for (rg int i = 1; i <= p; ++i)
for (rg int j = 1; j <= n; ++j)
g[i][j] = (int) dis[x[i]][j] <= mid;
int res = 0;
memset(bel, 0, sizeof bel);
for (rg int i = 1; i <= p; ++i)
memset(vis, 0, sizeof vis), res += dfs(i);
return res >= k;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(m), read(p), read(k);
for (rg int i = 1; i <= p; ++i) read(x[i]);
for (rg int i = 1; i <= n; ++i)
for (rg int j = 1; j <= n; ++j)
dis[i][j] = 1e9;
for (rg int i = 1; i <= n; ++i) dis[i][i] = 0;
for (rg int u, v, d, i = 1; i <= m; ++i) {
read(u), read(v), read(d);
dis[v][u] = dis[u][v] = min(dis[u][v], d);
}
for (rg int k = 1; k <= n; ++k)
for (rg int i = 1; i <= n; ++i)
for (rg int j = 1; j <= n; ++j)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
int l = 0, r = 1731311 + 1;
while (l < r) {
int mid = (l + r) >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
if (l > 1731311) puts("-1");
else printf("%d
", l);
return 0;
}
完结撒花 (qwq)
以上是关于「CF852D」 Exploration plan的主要内容,如果未能解决你的问题,请参考以下文章
强化学习笔记5:learning&planning, exploration&exploitation
[codeforces 852 D] Exploration Plan 解题报告 (二分+最大匹配)