设为首页 加入收藏

TOP

Codeforces E. President and Roads (优先队列Dijkstar + 强连通 找必最短路上的必须边(桥))经典(一)
2015-11-21 00:55:46 来源: 作者: 【 】 浏览:2
Tags:Codeforces President and Roads 优先 队列 Dijkstar 连通 路上 必须 经典
E. President and Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

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 n, m, s 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?≤?n; ai?≠?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 froms to 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).

Sample test(s) 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 or1?→?2?→?3?→?5?→?6.

题意 : 给 n<=10^5 个点 m <=10^5 条带权(即时间 1<=t<=10^6)边的有向图 (有重边), 现在要从起点VS 到终点 VT ,走的是最短时间。输出 m 行,如果第 i 行输出的是YES 表示 第 i 条边 是最短路的必经路。如果输出的是 CAN x ,则表示 修改 第 i 条边(第 i 条边的边权 减少了 x )使得原来的最短路减少1,必经过这条边。如果输出 NO 表示无法修改减少边权 使得最短路减少 1 。修改边的原则是:修改后的边权>0。

解题: 先用优先队列优化Dijkstar(用spfa超时),求起点vs到每个点的最短时间 time1[],再求从终点vt 反向图到每个点的最短时间 time2[] 。现在要判断必经路:重新建一个无向图,图中的边都是属于求出的最短路上的有边。用 强连通 判断桥(即必经路也是必须路),因为断了桥图就变成了两个连通块,所以桥即必经路。

?

#include
  
   
#include
   
     #include
    
      #include
     
       using namespace std; #define ll __int64 const int N = 100005; const ll INF = (ll)999999999999; struct nnn { int u,v; ll t; }node[N]; struct EDG{ int to,next; ll t; }edg[N*3]; struct NNNN{ int u; ll t; friend bool operator<(NNNN aa , NNNN bb){ return aa.t>bb.t; } }; int eid,head[N] , vs , vt , n ; ll time1[N],time2[N]; //从 vs 到每个点的最短时
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDOJ 5358 First One 暴力 下一篇Seek the Name, Seek the Fame

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: