Gym 101652T Security Badge (dfs)

Origin address: http://codeforces.com/gym/101652/attachments

Question: There are nn points, mm edges, and a total upper bound kk. You need to go from ss point to t point. For each edge u, v, l, R u, v, l, r, the weight you hold needs to be within the range of [l,r][l,r] to go from u u to V v. Ask how many different weights can be successfully transferred from ss point to t t.

Thought: At first, I thought about dfsdfs from the scope of [1,k][1,k]. Finally, every time I found a feasible interval, I saved it. Finally, I merged all the data to get the final answer. Then I found TT after writing. Probably it would be too time-consuming to go back every time.

Finally, we know a very ingenious judgment method, which stores the upper and lower bounds of each door. We can know that the endpoint values of the final legitimate intervals must come from these points. Then we only need to judge the endpoint values violently every time. Then u can know that this interval is not feasible.

#include <bits/stdc++.h>
#define eps 1e-8
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define CLR(x,y) memset((x),y,sizeof(x))
#define fuck(x) cerr << #x << "=" << x << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int seed = 131;
const int maxn = 1e5 + 5;
const int mod = 998244353;
int n, m, k, s, t;
struct edge {
    int u, v, l, r, nxt;
} e[maxn];
int tot, head[maxn];
void add_edge(int u, int v, int l, int r) {
    e[tot].v = v;
    e[tot].u = u;
    e[tot].l = l;
    e[tot].r = r;
    e[tot].nxt = head[u];
    head[u] = tot++;
}

int a[maxn << 1];
int cnt, ans, vis[maxn];
void dfs(int u, int num) {
    vis[u] = 1;
    for (int i = head[u]; ~i; i = e[i].nxt) {
        int v = e[i].v;
        if (vis[v]) continue;
        if (e[i].l <= num && e[i].r >= num) dfs(v, num);
    }
}
int main() {
    scanf("%d%d%d%d%d", &n, &m, &k, &s, &t);
    CLR(head, -1);
    for (int i = 1; i <= m; i++) {
        int a1, b, c, d;
        scanf("%d%d%d%d", &a1, &b, &c, &d);
        add_edge(a1, b, c, d);
        a[cnt++] = c - 1;//This is to calculate a single point.
        a[cnt++] = d;
    }
    sort(a, a + cnt);
    for (int i = 0; i < cnt; i++) {
        CLR(vis, 0);
        dfs(s, a[i]);
        if (vis[t]) ans += a[i] - a[i - 1];
    }
    printf("%d\n", ans);
    return 0;
}

Posted by garrisonian14 on Mon, 04 Feb 2019 00:42:16 -0800