标签:ext 注意事项 puts lse cstring har return ref 最大
传送门
Luogu
先跑一遍 \(\text{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\n", l);
return 0;
}
完结撒花 \(qwq\)
标签:ext 注意事项 puts lse cstring har return ref 最大
原文地址:https://www.cnblogs.com/zsbzsb/p/11745845.html