Gym 101615G Security Badge——离散化+dfs

版权声明:欢迎大家转载,转载请注明出处 https://blog.csdn.net/hao_zong_yin/article/details/82423352

离散化后dfs判断可行性

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e4 + 10;
int mem, head[maxn];
struct Edge { int to, next, down, up; }edges[maxn];
void init_edges() {
    mem = 0;
    memset(head, -1, sizeof(head));
}
void addedge(int from, int to, int down, int up) {
    edges[mem].to = to, edges[mem].down = down, edges[mem].up = up; edges[mem].next = head[from]; head[from] = mem++;
}
int n, m, k, s, t;
int cnt, T[maxn];
bool vis[maxn];
void dfs(int u, int x) {
    vis[u] = 1;
    for (int i = head[u]; ~i; i = edges[i].next) {
        int v = edges[i].to, down = edges[i].down, up = edges[i].up;
        if (!vis[v] && down <= x && x <= up) dfs(v, x);
    }
}
int main() {
    scanf("%d%d%d%d%d", &n, &m, &k, &s, &t);
    init_edges();
    int a, b, c, d;
    cnt = 0;
    for (int i = 1; i <= m; i++) {
        scanf("%d%d%d%d", &a, &b, &c, &d);
        addedge(a, b, c, d);
        T[++cnt] = c-1; T[++cnt] = d;
    }
    sort(T + 1, T + 1 + cnt);
    int ans = 0;
    for (int i = 1; i <= cnt; i++) {
        for (int j = 0; j <= n; j++) vis[j] = 0;
        dfs(s, T[i]);
        if (vis[t]) {
            ans += T[i]- T[i-1];
        }
    }
    printf("%d\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hao_zong_yin/article/details/82423352