1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| #include<bits/stdc++.h> using namespace std; #define ll long long const int maxn=1e5+10; const ll inf=0x3f3f3f3f3f3f3f3f; struct edge{ ll u,v,w,nxt; }e[3][maxn<<1]; ll h[3][maxn],cnt[3]; void add(ll u,ll v,ll w,int k){ e[k][++cnt[k]]=(edge){u,v,w,h[k][u]}; h[k][u]=cnt[k]; } struct reedge{ ll u,v,w,nxt,num; }re[maxn<<1]; ll rcnt=0,rh[maxn]; void addedge(ll u,ll v,ll w,ll i){ re[++rcnt].u=u;re[rcnt].v=v;re[rcnt].w=w; re[rcnt].nxt=rh[u];re[rcnt].num=i; rh[u]=rcnt; } ll n,m,s,t,u,v,w,dis[3][maxn]; struct node{ ll i,dis; bool operator <(const node&x)const{ return dis>x.dis; } }; void dijstra(ll be,int k){ priority_queue<node> q; for(int i=0;i<=n;i++) dis[k][i]=inf; dis[k][be]=0; q.push((node){be,0}); while(!q.empty()){ ll u=q.top().i,dist=q.top().dis; q.pop(); if(dis[k][u]!=dist) continue; for(int i=h[k][u];i;i=e[k][i].nxt){ ll v=e[k][i].v,w=e[k][i].w; if(dis[k][v]>dis[k][u]+w){ dis[k][v]=dis[k][u]+w; q.push((node){v,dis[k][v]}); } } } } ll dfn[maxn],low[maxn],tot=0,fa[maxn],flag[maxn<<2],ans[maxn<<2]; void tarjan(ll u){ dfn[u]=low[u]=++tot; for(int i=rh[u];i;i=re[i].nxt){ int v=re[i].v; if(!dfn[v]){ fa[v]=re[i].num; tarjan(v); low[u]=min(low[u],low[v]); if(dfn[u]<low[v]){ flag[re[i].num]=1; } }else if(fa[u]!=re[i].num){ low[u]=min(low[u],dfn[v]); } } } int main(){ scanf("%lld%lld%lld%lld",&n,&m,&s,&t); for(int i=1;i<=m;i++){ scanf("%lld%lld%lld",&u,&v,&w); add(u,v,w,0); add(v,u,w,1); } dijstra(s,0); dijstra(t,1); ll minn=dis[0][t]; for(int i=1;i<=m;i++){ ll u=e[0][i].u,v=e[0][i].v,w=e[0][i].w; if(dis[0][u]==inf||dis[1][v]==inf){ flag[i]=-2; } else if(dis[0][u]+dis[1][v]+w==minn){ addedge(u,v,w,i); addedge(v,u,w,i); } else if(dis[0][u]+dis[1][v]+w>minn){ flag[i]=-1; ans[i]=w-(minn-1-dis[0][u]-dis[1][v]); } } tarjan(s); for(int i=1;i<=m;i++){ if(flag[i]==-1){ if(ans[i]<e[0][i].w&&ans[i]>0) printf("CAN %lld\n",ans[i]); else puts("NO"); } else if(flag[i]==1){ puts("YES"); }else{ if(e[0][i].w>1&&flag[i]!=-2) puts("CAN 1"); else puts("NO"); } } return 0; }
|