题解 CF567E 【President and Roads】

· · 题解

你是否 Wrong answer on test 104?

问一条边是否一定会被经过,这非常简单:

只需要正图反图统计两遍最短路数量。

记 s\rightarrow i 的最短路数量为 d_i

记 i\rightarrow t 的最短路数量为 d_i'

如果有一条边 u\rightarrow v 必须被经过,那么必然满足 d_u\times d_v'=d_t。

因为显然最短路数可以非常多,必须要取模。

显然 cf 数据非常毒瘤,必须要双模数。

然后注意边权不能减成负的,大力分类讨论即可。

好像模数稍微针对了一下 1e9+7

我用的是

#define mod (998244353)
#define mod2 (192608173)

代码

#include <cstdio>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <queue>

#define rg register
#define il inline
#define MX (100000 + 33)
#define LL long long
#define mod (998244353)
#define mod2 (192608173)

int read(){
    char k = getchar(); int x = 0;
    while(k < '0' || k > '9') k = getchar();
    while(k >= '0' && k <= '9')
        x = x * 10 + k - '0' ,k = getchar();
    return x;
}

int pos[MX] ,neg[MX] ,tot;
struct edge{
    int fr ,node ,next ,w;
}h[MX << 1];
void addedge(int u ,int v ,int w ,int *head){
    h[++tot].next = head[u] ,head[u] = tot;
    h[tot].fr = u ,h[tot].node = v,h[tot].w = w;
}

struct node{
    int d; LL dis;
    bool operator <(const node& b)const{
        return dis > b.dis;
    }
};
LL distoT[MX] ,disfrS[MX] ,cnttoT[MX] ,cnt1[MX] ,cnt2[MX] ,cntfrS[MX];
int s ,t ,n ,m ,vis[MX];
void dijkstra(int ST ,int *head ,LL *dist ,LL *cnt ,LL *CNT){
    for(int i = 1 ; i <= n ; ++i) dist[i] = 1LL << 50;
    dist[ST] = 0 ,cnt[ST] = 1 ,memset(vis ,0 ,sizeof vis);
    std::priority_queue<node> q; q.push((node){ST ,0});
    while(!q.empty()){
        node tmp = q.top(); int x = tmp.d; q.pop();
        if(tmp.dis != dist[x] || vis[x]) continue;
        vis[x] = true;
        for(int i = head[x] ,d ; i ; i = h[i].next){
            d = h[i].node;
            if(dist[x] + h[i].w == dist[d]){
                (cnt[d] += cnt[x]) %= mod;
                (CNT[d] += CNT[x]) %= mod2;
            }
            if(dist[x] + h[i].w < dist[d]){
                dist[d] = dist[x] + h[i].w ,cnt[d] = cnt[x];
                CNT[d] = CNT[x];
                if(!vis[d]) q.push((node){d ,dist[d]});
            }
        }
    }
}

int main(){
    n = read() ,m = read() ,s = read() ,t = read();
    for(int i = 1 ,u ,v ,w ; i <= m ; ++i){
        u = read() ,v = read() ,w = read();
        addedge(u ,v ,w ,pos) ,addedge(v ,u ,w ,neg);
    }
    dijkstra(s ,pos ,disfrS ,cntfrS ,cnt1);
    dijkstra(t ,neg ,distoT ,cnttoT ,cnt2);
    for(int i = 1 ,u ,v ,w ; i <= m * 2 ; i += 2){
        u = h[i].fr ,v = h[i].node ,w = h[i].w;
        if(disfrS[u] + distoT[v] >= (1LL << 50))    puts("NO");
        else if(disfrS[u] + w + distoT[v] == disfrS[t]){
            if(cntfrS[t] == cntfrS[u] * cnttoT[v] % mod &&
            cnt1[t] == cnt1[u] * cnt2[v] % mod2)    puts("YES");
            else{
                if(w == 1) puts("NO");
                else puts("CAN 1");
            }
        }else{
            LL del = disfrS[u] + w + distoT[v] - disfrS[t] + 1;
            if(del >= w) puts("NO");
            else printf("CAN %lld\n" ,del);
        }
    }return 0;
}