"Codeforces 567E" president and Roads (Shortest way)

Source: Internet
Author: User

Description

Berland have n Cities, the capital was located in City s, and the historic home town of the Presid ENT is in city T (st). 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 he historic home Town  t , for which his MOTORCAD E passes along some path from  s  to  t   (He always returns on a personal plane). Since The president is a very busy mans, he always chooses the path From  s  to&nbs P 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, tn)-the number of cities and roads In Berland, the numbers of the capital and of the Presidents ' home town (st).

NextmLines contain the roads. Each road is given as a group of three integers a i , b I , l i   (1≤ a i , b i n ;   a Sub class= "Lower-index" > i b i ;  1≤ l i ≤106)-the cities that is connected by The  I -th Road and the time needed to ride along it. The road is directed from City  a i  to City  b i .

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 sto 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 O Rder of appearance in the input).

If the president would definitely ride along it during his travels, 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 definitely ride along it, print space-separated word "CAN" (without the quotes), and the minimum cost of Repairi Ng.

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

Examplesinput
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
Note

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 may choose one of the following ways for a ride:1→2→4→5→6 or 1 →2→3→5→6.

The Dijkstra finds the shortest path from S and the shortest path starting from T.

When the edge is stored, the forward and reverse are saved separately, and the shortest-circuit number of each point is computed at the same time.

D[0][i] Indicates the shortest path from S to I, D[1][i] indicates the shortest path from t to I. The weighted value for each edge is W.

When W+d[0][u]+d[1][v] is indicated is the edge of the shortest path from S to T, and if it is the edge that all the short circuits pass through, the path[0][u]*path[1][v]==path[0][t] is satisfied.

Path is the shortest number, because it can explode long long, so you have to modulo it, and it can't be 1000000007.

#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include < algorithm> #define N 100005#define M 5462617#define inf 0x3f3f3f3f3f3f3f3fll#define ll long long#define Add (u,v,w) e[++ Cnt]= (Edge) {v,head[0][u],w};head[0][u]=cnt;e[++cnt]= (edge) {u,head[1][v],w};head[1][v]=cntusing namespace std; struct EDGE{LL to,next,w;} E[n<<1];struct road{ll u,v,w;} L[n];struct qnode{ll V,c;bool operator < (const qnode &r) Const{return c>r.c;}}; ll n,m,s,t,u,v,w,d[2][n],cnt,head[2][n],b[2][n],path[2][n];void Dijkstra (int f) {int i,j,k,pr=f?t:s;for (i=1;i<=n ; i++) d[f][i]=inf;priority_queue<qnode> Q;d[f][pr]=0;q.push ((qnode) {pr,0});p Ath[f][pr]=1;while (!q.empty ()) {Qnode u=q.top (); Q.pop (); if (B[F][U.V]) continue;b[f][pr=u.v]=1;for (j=head[f][pr];j;j=e[j].next) {k=e[j].to;if (d[f ][pr]+e[j].w==d[f][k]) path[f][k]= (PATH[F][K]+PATH[F][PR])%m;else if (D[f][pr]+e[j].w<d[f][k]) {D[F][K]=D[F][PR ]+e[j].w;q.push ((Qnode) {k,d[f][k]});p ATH[F][K]=PATH[F][PR];}}} Int Main () {int i,j;cin>>n>>m>>s>>t;for (i=1;i<=m;i++) {scanf ("%lld%lld%lld", &u,&v, &AMP;W); add (u,v,w); l[i]= (road) {u,v,w};} Dijkstra (0);d Ijkstra (1); for (i=1;i<=m;i++) {u=l[i].u;v=l[i].v;w=l[i].w;if (d[0][u]+d[1][v]+w==d[0][t]) {if (path [0] [U]*path[1][v]%m==path[0][t]) puts ("YES"), else if (w>1) puts ("CAN 1"); else puts ("NO");} else if (D[0][u]+d[1][v]+1<d[0][t]) printf ("CAN%lld\n", d[0][u]+d[1][v]+w-d[0][t]+1); else puts ("NO");}}

  

"Codeforces 567E" president and Roads (Shortest way)

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.