【CodeForces 567E】President and Roads(最短路)

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 61 2 21 3 102 3 72 4 83 5 34 5 25 6 1

output

YESCAN 2CAN 1CAN 1CAN 1CAN 1YES

input

3 3 1 31 2 102 3 101 3 100

output

YESYESCAN 81

input

2 2 1 21 2 11 2 2

output

YESNO

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<<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});
	path[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]});
				path[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,&w);
		add(u,v,w);
		l[i]=(road){u,v,w};
	}
	dijkstra(0);
	dijkstra(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");
	}
}

  

时间: 2024-08-10 15:10:11

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

CodeForces 567E President and Roads(最短路 + tarjan)

CodeForces 567E President and Roads 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

Codeforces Round #Pi (Div. 2) E. President and Roads (最短路+强连通求割边)

题目地址:codeforces #pi (DIV2) E 题目很水..就是先求两边最短路,然后把可能为最短路的边挑出来,然后判断是否yes只需要转化成无向图跑一遍tarjan,找出割边,割边就是yes,然后剩下的边就让它的值为最短路-1就行了,如果-1后变成了非正数,就是no. 但是!!!居然卡spfa!!那是不是说cf以后就不能用可以卡的算法了..完全可以出组数据来卡这些算法...比如spfa,isap... 于是为了这题,又看了一遍迪杰斯特拉算法.. 代码如下: #include <cstd

Codeforces 191C Fools and Roads(树链剖分)

题目链接:Codeforces 191C Fools and Roads 题目大意:给定一个N节点的数,然后有M次操作,每次从u移动到v,问说每条边被移动过的次数. 解题思路:树链剖分维护边,用一个数组标记即可,不需要用线段树. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1e5 + 5; int N, Q, ne, fir

codeforces 567 E. President and Roads 【 最短路 桥 】

给出一个有向图,从起点走到终点(必须走最短路),问一条边是否一定会被经过,如果不经过它,可以减小它的多少边权使得经过它(边权不能减少到0) 正反向建图,分别求出起点到每个点的最短距离,终点到每个点的最短距离(用这个可以算出减小的边权) 再将在最短路径上的边重新建图.求出里面的桥,就是必须经过的边 wa了一上午------呜呜呜呜 先wa 19  是因为求桥的时候是无向图,数组开小了一半 然后 wa 46 ,是因为dis[]数组初始化为 1 << 30 -1 ,应该再开大点 ,开成 1 <

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

#include<time.h> #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<set> #include<map> #in

Codeforces 543B Destroying Roads(最短路)

题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1:假设最后留下的边是互不相交的两条路径.此时的答案是n-s1到t1的最短路径-s2到t2的最短路径. 2:假设最后留下的边有重合的一段,此时只要枚举重合的这一段的起点和终点,就可以判断.注意此时要考虑这两条路径经过枚举的两点的顺序. 限制的条件比较多,可以用来剪枝的条件也很多. 由于所有边的长度为1,用DF

Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路 删边

题目:有n个城镇,m条边权为1的双向边让你破坏最多的道路,使得从s1到t1,从s2到t2的距离分别不超过d1和d2. #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <stack> #in

codeforces 544 D Destroying Roads 【最短路】

题意:给出n个点,m条边权为1的无向边,破坏最多的道路,使得从s1到t1,s2到t2的距离不超过d1,d2 因为最后s1,t1是连通的,且要破坏掉最多的道路,那么就是求s1到t1之间的最短路 用bfs求出任意两个顶点之间的距离, 如果d[s1][t1]>d1||d[s2][t2]>d2,那么不可能 然后就枚举重叠的区间(就像题解里面说的"H"形一样) 如果枚举区间是1--n,1--i的话,需要交换四次 如果枚举区间是1--n,1--n的话,则只需要交换一次就可以了 看的这一

【poj 1724】 ROADS 最短路(dijkstra+优先队列)

ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12436 Accepted: 4591 Description N cities named with numbers 1 - N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that