Description

Berland has n cities, the capital is located in city s, and the historic home town of the President is in city t (s ≠ t). The cities are 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 his 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 will travel the fastest.

The ministry of Roads and Railways wants to learn for each of the road: whether the President will definitely pass through it during his travels, and if not, whether it is possible to repair it so that it would definitely be included in the shortest path from the capital to the historic home town of the President. Obviously, the road can not be repaired so that the travel time on it was less than one. The ministry of Berland, like any other, is interested in maintaining the budget, so it wants to know the minimum cost of repairing the road. Also, it is very fond of accuracy, so it repairs the roads so that the travel time on them is always a positive integer.

Input

The first lines contain four integers nms and t (2 ≤ n ≤ 105; 1 ≤ m ≤ 105; 1 ≤ s, t ≤ n) — the number of cities and roads 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 ≤ nai ≠ bi; 1 ≤ li ≤ 106) — the cities that are connected 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 are numbered from 1 to n. Each pair of cities can have multiple roads between them. It is guaranteed that 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 are numbered in the order of appearance in the input).

If the president will 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 that the travel time on it remains positive and then president will definitely ride along it, print space-separated word "CAN" (without the quotes), and the minimum cost of repairing.

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

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

The cost of repairing the road is 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 two following ways for a ride:1 → 2 → 4 → 5 → 6 or 1 → 2 → 3 → 5 → 6.

dijkstra找出从s出发的最短路和从t出发的最短路。

存边的时候正向和逆向分别存起来,并且在求最短路的同时计算到每个点的最短路数量。

d[0][i]表示s出发到i的最短路,d[1][i]表示t出发到i的最短路。每条边的权值为w。

则当w+d[0][u]+d[1][v]时说明是s到t的最短路上的边,如果是所有最短路都经过的边,则满足path[0][u]*path[1][v]==path[0][t]。

path是最短路的数量,因为可能爆long long,因此要取模,而且还不能是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]=cnt
using namespace std;
struct edge{
ll to,next,w;
}e[N<<];
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[][N],cnt,head[][N],b[][N],path[][N];
void dijkstra(int f){
int i,j,k,pr=f?t:s;
for(i=;i<=n;i++)d[f][i]=inf;
priority_queue<qnode> q;
d[f][pr]=;
q.push((qnode){pr,});
path[f][pr]=;
while(!q.empty()){
qnode u=q.top();
q.pop();
if(b[f][u.v])continue;
b[f][pr=u.v]=;
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]});
path[f][k]=path[f][pr];
}
}
}
}
int main() {
int i,j;
cin>>n>>m>>s>>t;
for(i=;i<=m;i++){
scanf("%lld%lld%lld",&u,&v,&w);
add(u,v,w);
l[i]=(road){u,v,w};
}
dijkstra();
dijkstra();
for(i=;i<=m;i++)
{
u=l[i].u;
v=l[i].v;
w=l[i].w;
if(d[][u]+d[][v]+w==d[][t]){
if(path[][u]*path[][v]%M==path[][t])
puts("YES");
else if(w>)
puts("CAN 1");
else puts("NO");
}
else if(d[][u]+d[][v]+<d[][t])
printf("CAN %lld\n",d[][u]+d[][v]+w-d[][t]+);
else
puts("NO");
}
}

  

【CodeForces 567E】President and Roads(最短路)的更多相关文章

  1. Codeforces.567E.President and Roads(最短路 Dijkstra)

    题目链接 \(Description\) 给定一张有向图,求哪些边一定在最短路上.对于不一定在最短路上的边,输出最少需要将其边权改变多少,才能使其一定在最短路上(边权必须为正,若仍不行输出NO). \ ...

  2. Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥

    题目链接: http://codeforces.com/contest/567/problem/E 题意: 给你一个带重边的图,求三类边: 在最短路构成的DAG图中,哪些边是必须经过的: 其他的(包括 ...

  3. Codeforces Round #Pi (Div. 2) 567E President and Roads ( dfs and similar, graphs, hashing, shortest paths )

    图给得很良心,一个s到t的有向图,权值至少为1,求出最短路,如果是一定经过的边,输出"YES",如果可以通过修改权值,保证一定经过这条边,输出"CAN",并且输 ...

  4. Codeforces Gym 100338C Important Roads 最短路+Tarjan找桥

    原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...

  5. cf567E. President and Roads(最短路计数)

    题意 题目链接 给出一张有向图,以及起点终点,判断每条边的状态: 是否一定在最短路上,是的话输出'YES' 如果不在最短路上,最少减去多少权值会使其在最短路上,如果减去后的权值\(< 1\),输 ...

  6. Codeforces Round #Pi (Div. 2) E. President and Roads tarjan+最短路

    E. President and RoadsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567 ...

  7. Codeforces 806 D.Prishable Roads

    Codeforces 806 D.Prishable Roads 题目大意:给出一张完全图,你需要选取其中的一些有向边,连成一个树形图,树形图中每个点的贡献是其到根节点路径上每一条边的边权最小值,现在 ...

  8. Codeforces 191C Fools and Roads(树链拆分)

    题目链接:Codeforces 191C Fools and Roads 题目大意:给定一个N节点的数.然后有M次操作,每次从u移动到v.问说每条边被移动过的次数. 解题思路:树链剖分维护边,用一个数 ...

  9. codeforces 689 Mike and Shortcuts(最短路)

    codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...

随机推荐

  1. [No000055]教你早晨清肠、除口臭、色斑、大肚腩

    [长生不老的秘诀] 释.道.医三家有个共同的秘诀:"要叫人不死. 肠中须无屎". 佛家除要求人们戒杀吃素外,还认为早饭是天食,中饭是人食,晚饭是鬼食. 因此,为了身体健康,不妨碍修 ...

  2. 纯js代码实现手风琴特效

    我知道现在大多数前端开发人员都在使用jQuery等第三方的库来进行开发,这不仅节约了时间,也让效率大大的提高,并让公司的效益增加,何乐而不为呢? 但是,这也会有一定的缺点,比如jQ比js慢,尤其在大型 ...

  3. Docker容器概念讲解

    Docker 是 PaaS 提供商 dotCloud 开源的一个基于 LXC 的高级容器引擎,源代码托管在 Github 上, 基于go语言并遵从Apache2.0协议开源. Docker是通过内核虚 ...

  4. iOS十六进制和字符串的相互转换

    转换代码,崩溃日志有些是十六进制 NSString *dictString = [dict JSONFragment];//组合成的 dictString==={"content" ...

  5. workqueue机制分析之wb_workfn函数

    上面一篇文章说到: process_one_work中最重要的一件事情就是worker->current_func(work); 这里就具体到一项很具体的任务了,由于我要研究文件系统嘛,所以很自 ...

  6. POJ 1743 Musical Theme

    感觉最近好混乱......各种OJ都刷一点,感觉不太好......尤其是这种英文题 这道题一开始还没有看懂.听了ljh大犇的解释后终于明白了.下面我为英语和我一样的人翻译一下题面: 输入n个数.求最长 ...

  7. Linux共享库 socket辅助方法

    //sockhelp.h#ifndef _vx #define _vx #ifdef __cplusplus extern "C" { #endif /** * readn - 读 ...

  8. JavaEE PO VO BO DTO POJO DAO 整理总结

    佩服能将复杂难懂的技术,抽象成简单易懂事物的人. 厌恶将简单易懂的技术,添加一堆专业术语将别人弄的头晕目眩的人. PO VO BO DTO POJO DAO 总体一览: 1.DAO[data acce ...

  9. Windjs应用

    一个异步的js类库,应用价值不大,所以代码也没在维护了.在做h5特效或者游戏动画方面有点用处. $await是Windjs的核心api.具体可以check 浅谈Jscex的$await语义及异步任务模 ...

  10. 【分布式协调器】Paxos的工程实现-Cocklebur状态转移

    集群中的主机经过选举过程由Looking状态变为了Leadering或Following状态.而这些状态之间转移的条件是什么呢?先来个直观的,上状态图. 图 4.1 Cocklebur选举过程中的状态 ...