码迷,mamicode.com
首页 > 其他好文 > 详细

「CF852D」 Exploration plan

时间:2019-10-26 22:50:49      阅读:82      评论:0      收藏:0      [点我收藏+]

标签: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\)

「CF852D」 Exploration plan

标签:ext   注意事项   puts   lse   cstring   har   return   ref   最大   

原文地址:https://www.cnblogs.com/zsbzsb/p/11745845.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!