Codeforces E. President and Roads (优先队列Dijkstar + 强连通 找必最短路上的必须边(桥))

来源:互联网 发布:rc4算法原理图 编辑:程序博客网 时间:2024/05/20 05:28
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 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 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 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 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<stdio.h>#include<string.h>#include<queue>#include<stack>using namespace std;#define ll __int64const 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 到每个点的最短时间 和 从 vt 反向到每个点的最短时间bool inq[N];void init(){    eid=0;    memset(head,-1,sizeof(head));}void addEdg(int u ,int v,ll t,ll tt=0){    edg[eid].to=v; edg[eid].next=head[u];    edg[eid].t=t; head[u]=eid++;    edg[eid].to=u; edg[eid].next=head[v];    edg[eid].t=tt; head[v]=eid++;}void dijstar(int flag){    priority_queue<NNNN>q;    NNNN now , pre;    int u,v;    for(int i=1; i<=n; i++){        if(flag==0) time1[i]=INF;  else   time2[i]=INF;        inq[i]=0;    }    now.t = 0;    if(flag==0)        time1[vs]=0 , now.u = vs;    else        time2[vt]=0 , now.u = vt;    q.push(now);     while(!q.empty())     {         pre=q.top(); q.pop();         u=pre.u;         if(inq[u]) continue;         inq[u]=1;         for(int i=head[u]; i!=-1; i=edg[i].next){            v=edg[i].to;            if(inq[v])continue;            if(flag==0){                if(time1[v]>time1[u]+edg[i].t&&edg[i].t!=INF){                    time1[v]=time1[u]+edg[i].t;                    now.t = time1[v] ;                    now.u = v;                    q.push(now);                }            }            else{ //反向求最短时间                int ti=i^1;                if(time2[v]>time2[u]+edg[ti].t&&edg[i].t==INF){                    time2[v]=time2[u]+edg[ti].t;                    now.t = time2[v] ;                    now.u = v;                    q.push(now);                }            }         }     }}bool ok[N] , vistedg[N];int low[N] , dfn[N] , deep ;void tarjar(int u ){    low[u] = dfn[u] = ++deep;    for(int i=head[u] ; i!=-1; i=edg[i].next){        int v=edg[i].to ;        if(vistedg[edg[i].t]){ //有重边找必经路,只能用标记边的办法才会正确,不用 v==f             head[u] = edg[i].next; continue;        }        head[u] = edg[i].next; //每条边只用一次        vistedg[edg[i].t] = 1;        if( !dfn[v] ){            tarjar( v ) ;            low[u] = low[u] < low[v] ? low[u] : low[v] ;            if(dfn[u] < low[v]) ok[edg[i].t]=1;  //桥,则是必经路        }        else if(low[u]>dfn[v])            low[u] = dfn[v] ;    }}int main(){    int m , u , v ;    ll t;    while(scanf("%d%d%d%d",&n,&m,&vs,&vt)>0){        init();        for(int i=0; i<m; i++){            scanf("%d%d%I64d",&u,&v,&t);            node[i].u=u;            node[i].v=v;            node[i].t=t;            addEdg(u , v , t , INF);        }        dijstar(0);        dijstar(1);        ll shorttime=time1[vt];        deep=0;        init();        memset(ok , 0 , sizeof( ok ));        memset(dfn , 0 , sizeof( dfn ));        memset(vistedg , 0 , sizeof( vistedg ));        for(int i=0; i<m; i++){ //重新建一个只有在最短路上的边的无向图,只用于找必须边,也就是找 桥             u=node[i].u;            v=node[i].v;            if(time1[u]+node[i].t+time2[v]==shorttime)                addEdg(u,v,i,i) ;        }        tarjar( vs ) ;        for(int i=0; i<m; i++)        if(ok[i])            printf("YES\n");        else {            u=node[i].u;            v=node[i].v;            t=shorttime-time1[u]-time2[v]-1;             if(t>0)                printf("CAN %I64d\n",node[i].t-t);            else                printf("NO\n");        }    }}


阅读全文
0 0
原创粉丝点击