P5590 [Racing]

Sure enough, I was too \ (Naive \) a

First, there are some points / side actually makes no sense, if not to the point of departure from or not from the point 1 to n, this point can not control. This process can be both positive and negative sides \ (dfs / bfs \) implemented

After then delete those points, the new figure if there is a ring, then obviously no solution

Figure it now and then transformed into a \ (DAG \)

Because of (1-> n \) \ all paths is constant, then \ (1 -> \) new figures path at all points should be a fixed value (evidence to the contrary will know)

Then we found that for every edge \ (u-> v \) , in fact, to meet \ (1≤dis_v - dis_u ≤ 9 \) , it is not difficult to find a difference constraint model

Me remove the item, we have: \ (dis_u + 1≤dis_v \) , \ (dis_v-9≤dis_u \)

So for every edge \ ((U, v) \) , we even a \ ((u, v, 1 ) \) and a \ ((u, v, -9 ) \) to

\(Code:\)

I do not know why only 30, first cushions for a while, tune out and put

Well, tune out, the starting point of the end of those sides do not communicate forget output

#include<bits/stdc++.h>
using namespace std;
#define il inline
#define re register
il int read() {
    re int x = 0, f = 1; re char c = getchar();
    while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
    return x * f;
}
#define rep(i, s, t) for(re int i = s; i <= t; ++ i)
#define _ 20005
int n, m, head1[_], cnt, head2[_], head3[_], vis[_], dis[_], num[_], pax[_], v1[_], v2[_];
struct edge {
    int u, v, w, next;
}e1[_], e2[_], e3[_ << 1];
il void add1(int u, int v) {
    e1[++ cnt] = (edge){u, v, 0, head1[u]}, head1[u] = cnt;
    e2[cnt] = (edge){v, u, 0, head2[v]}, head2[v] = cnt;
}
il void add2(int u, int v) {
    e3[++ cnt] = (edge){u, v, 1, head3[u]}, head3[u] = cnt;
    e3[++ cnt] = (edge){v, u, -9, head3[v]}, head3[v] = cnt;
}
il void dfs1(int u) {
    v1[u] = 1;
    for(re int i = head1[u]; i; i = e1[i].next) if(!v1[e1[i].v]) dfs1(e1[i].v);
}
il void dfs2(int u) {
    v2[u] = 1;
    for(re int i = head2[u]; i; i = e2[i].next) if(!v2[e2[i].v]) dfs2(e2[i].v);
}
queue<int>q;
il void SPFA() {
    memset(dis, -64, sizeof(dis)), q.push(1), dis[1] = 0;
    while(!q.empty()) {
        int u = q.front(); q.pop(), vis[u] = 0;
        for(re int i = head3[u]; i; i = e3[i].next) {
            int v = e3[i].v;
            if(dis[v] < dis[u] + e3[i].w) {
                dis[v] = dis[u] + e3[i].w;
                if(!vis[v]) q.push(v), vis[v] = 1, ++ num[v];
                if(num[v] > n) puts("-1"), exit(0);
            }
        }
    }
}
int main() {
    n = read(), m = read();
    rep(i, 1, m) {
        int u = read(), v = read();
        add1(u, v);
    }
    dfs1(1), dfs2(n), cnt = 0;
    if(!v1[n]) return puts("-1"), 0;
    rep(i, 1, n) if(v1[i] && v2[i]) pax[i] = 1;
    rep(i, 1, m) if(pax[e1[i].u] && pax[e1[i].v]) add2(e1[i].u, e1[i].v);
    SPFA(), printf("%d %d\n", n, m);
    rep(i, 1, m) {
        if(pax[e1[i].u] && pax[e1[i].v]) {
            printf("%d %d %d\n", e1[i].u, e1[i].v, dis[e1[i].v] - dis[e1[i].u]);
        }
        else printf("%d %d %d\n", e1[i].u, e1[i].v, 9);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/bcoier/p/11774586.html