Codeforces 567E President and Roads (shortest circuit + Tarjan)

Source: Internet
Author: User

Codeforces 567E President and Roads

Description

Berland has n cities, the capital was located in city S, and the historic home town of the president are in City T (s?≠?t). The cities is connected by one-way roads, the travel time for each of the road is a positive integer.

Once a year the president visited his historic home town T, for which he motorcade passes along some path from S to T (he Always returns on a personal plane). Since The president is a very busy man, he always chooses the path from S to T, along which he'll travel the fastest.

The Ministry of Roads and Railways wants to learn for each of the Road:whether the president would definitely pass through It during his travels, and if not, whether it's possible to repair it's so that it would definitely being included in the SHO Rtest path from the capital to the historic home town of the president. Obviously, the road can is repaired so, and the travel time on it is less than one. The Ministry of Berland, like any other, are interested in maintaining the budget, so it wants-know the minimum cost of Repairing the road. Also, it's very fond of accuracy, so it repairs the roads so, the travel time on them are always a positive integer.

Input

The first lines contain four integers n, m, s and T (2?≤?n?≤?105; 1?≤?m?≤?105; 1?≤?s,?t?≤?n)-the number of cities and RO Ads in Berland, the numbers of the capital and of the Presidents ' home town (s?≠?t).

Next m lines contain the roads. Each road is given as a group of three integers ai,?bi,?li (1?≤?ai,?bi?≤?n; ai?≠?bi; 1?≤?li?≤?106)-the cities The is C Onnected by the I-th Road and the time needed to ride along it. The road is directed from city Ai to city bi.

The cities is numbered from 1 to N. Each pair of cities can has multiple roads between them. It is guaranteed this there is a path from S to t along the roads.

Output

Print m lines. The i-th line should contain information about the I-th road (the roads is numbered in the order of appearance in the INP UT).

If the president would definitely ride along it during his travels and the line must contain a single word "YES" (without the Quotes).

Otherwise, if the i-th road can be repaired so, the travel time on it remains positive and then president would definit Ely Ride Along it, print space-separated word "CAN" (without the quotes), and the minimum cost of repairing.

If we can ' t make the road is such that president would definitely ride along it, print "NO" (without the quotes).

Sample Input
Input

6 7 1 6
1 2 2
1 3 10
2 3 7
2 4 8
3 5 3
4 5 2
5 6 1

Output

YES
CAN 2
CAN 1
CAN 1
CAN 1
CAN 1
YES

Input

3 3 1 3
1 2 10
2 3 10
1 3 100

Output

YES
YES
CAN 81

Input

2 2 1 2
1 2 1
1 2 2

Output

YES
NO

Hint

The cost of repairing the road was the difference between the time needed to ride along it before and after the repairing.

In the first sample president initially could choose one of the following ways for a ride:1?→?2?→?4?→?5?→?6 or 1?→?2?→? 3?→?5?→?6.

The president wants to go home, going through some streets, each street is one-way and has the right value. Now, in order for the president to get home better, operate on every street: 1 if the street must be on the shortest path, the output is "YES". 2) If the street is repaired, the shortest path of the edge can replace the original shortest, then output "CAN x", X is the cost of modifying the street, is the value of the weight reduction. 3) If the street is a disconnected street, or if the weight value is less than or equal to 0, the output is "NO". Problem-solving ideas: positive to take the edge, to find the shortest time to get d1[], and then reverse the edge, and then find the shortest time to get d2[]. Then start judging whether each side is on the shortest path. The initial shortest circuit is min, when d 1[u]+d 2[v]+d Is==M IN , the first to determine whether the edge is necessarily the shortest way side, is the word output "YES", otherwise in judging the weight of the side is greater than 1, is the word output "CAN 1", otherwise output "NO". When d 1[u]+d 2[v]+d Is>M IN , the first to determine the difference between the two sections of the value plus a will be greater than or equal to the difference in the side, if so, output "no", otherwise output "CAN difference value plus one", the rest of the output "no". There is also a question, how to determine whether the side must be in the shortest way. First take out the shortest, re-build an image, and then use the Tarjan algorithm to find out which side is the bridge. The bridge is definitely on the shortest road side.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace STD;typedef Long LongllConst intN =200005;Const intM =400005;Constll INF =1e17;intN, M, S, T;ll Min;intvis[2][n];ll d[2][n];intEninthead[3][M], no[m], fa[m];intDFN[M], low[m], DN;intTj[m], FLAG;structpos{intU ll D;BOOL operator< (Constpos& x)Const{returnD > x.d; }};structNode {intTo, next; ll Dis;} edge[3][M];structrec{intU, V, E; ll c;} REC[M];voidInit () {en =0; DN =0;memset(Head,-1,sizeof(head));}voidAddedge (intUintV, LL X,intFlag, LL D) {if(d! = INF) rec[d].u = u, rec[d].v = V, rec[d].c = x, rec[d].e = en;      Edge[flag][en].to = v;      Edge[flag][en].next = Head[flag][u];      Edge[flag][en].dis = x;  Head[flag][u] = en++; }voidADDEDGEMD (intUintV, LL X,intFlagintD) {edge[flag][en].to = v;      Edge[flag][en].next = Head[flag][u];      Edge[flag][en].dis = x;    No[en] = D;      Head[flag][u] = en++;      edge[flag][en].to = u;      Edge[flag][en].next = Head[flag][v];      Edge[flag][en].dis = x;    No[en] = D;   HEAD[FLAG][V] = en++; }voidDijkstra (intFlag) {priority_queue<pos> q; for(inti =1; I <= N;           i++) {D[flag][i] = INF; Vis[flag][i] =0; } D[flag][s] =0; Q.push (POS) {s,0}); while(!q.empty ()) {intU = q.top (). u; Q.pop ();if(Vis[flag][u])Continue; Vis[flag][u] =1; for(inti = Head[flag][u]; I! =-1; i = edge[flag][i].next) {intv = edge[flag][i].to;if(D[flag][v] > D[flag][u] + edge[flag][i].dis)                {D[flag][v] = D[flag][u] + Edge[flag][i].dis;            Q.push ((POS) {V, d[flag][v]}); }        }    }}voidInput () {intA, B; ll C; for(inti =0; I < m; i++) {scanf("%d%d%lld", &a, &b, &c); Addedge (A, B, C,0, i); Addedge (b, A, C,1, INF); }}voidGetd () {Dijkstra (0);    Swap (S, t); Dijkstra (1);    Swap (S, t); Min = d[0][T];}voidTarjan (intu) {dfn[u] = low[u] = dn++; for(inti = head[2][u]; I! =-1; i = edge[2][i].next) {intv = edge[2][i].to;if(!            Dfn[v]) {Fa[v] = No[i];              Tarjan (v); Low[u] = min (Low[u], low[v]);if(Low[v] > Dfn[u]) tj[fa[v] =1; }Else if(Fa[u]! = No[i])        {Low[u] = min (Low[u], dfn[v]); }    }}voidBUILDMD () {en =0; for(inti =0; I < m; i++) {REC r = rec[i];if(d[0][R.U] + d[1][R.V] + r.c = Min) {addedgemd (r.u, R.V, R.C,2, i); }     }}voidSolve () { for(inti =0; I < m;        i++) {REC r = rec[i]; ll D = d[0][R.U] + d[1][R.V] + r.c;if(d[0][R.U] = = INF | | d[1][R.V] = = INF) {printf("no\n");Continue; }if(Tj[i])printf("yes\n");Else if(D = = Min && r.c >1)printf("CAN 1\n");Else if(D > Min) {inttemp = D-min +1;if(R.C <= temp)printf("no\n");Else printf("CAN%d\n", temp); }Else printf("no\n"); }}intMain () {scanf("%d %d%d%d", &n, &m, &s, &t);    Init ();    Input ();    GETD ();    BUILDMD ();    Tarjan (s); Solve ();return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission can also be reproduced, but to indicate the source oh.

Codeforces 567E President and Roads (shortest circuit + Tarjan)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.