题目
题意概要
给出一个
n
n
n 个点
m
m
m 条边的带边权有向图,不保证无重边。给出起点
s
s
s 和终点
t
t
t ,对每一条边输出其分类。类别如下:
- s s s 到 t t t 的最短路(简称“最短路”,下同)一定 经过这条边,输出 Y E S \tt YES YES 。
- 最短路不一定经过这条边。但是我们可以将这条边的权值修改为 v ( v > 0 ) v(v>0) v(v>0) ,使得最短路 一定 经过这条边,输出 C A N \tt CAN CAN ,并输出原来的边权与 max v \max v maxv 的差。
- 不属于上述两类。输出 N O \tt NO NO 。
数据范围与提示
n
,
m
n,m
n,m 均为
1
0
5
10^5
105 以内的正整数。
2
≤
n
2\le n
2≤n 且
1
≤
s
,
t
≤
n
1\le s,t\le n
1≤s,t≤n 。
思路
好难啊。为啥是个蓝题啊。小蒟蒻的生存空间已经到了岌岌可危的地步了吗……
最初的想法是,去掉每一条边,看看最短路有没有变长。不仅会超时,而且无法区分第二类边和第三类边。
悄悄看一眼题解。方案数?有道理啊!最短路不一定经过这条边 ⇒ \Rightarrow ⇒ 不是所有最短路都经过这条边。所以只有经过这条边的最短路数量与所有最短路 数量相等时 才一定经过。
这可以用两次最短路,顺带着求一下方案数量。可惜方案数量很大。比如一条链,相邻两个点之间是两条等长的边(重边是被允许的),那就会是 2 n 2^n 2n 条最短路。怎么办?黑科技——取模。相当于 哈希。
当然,这里也有真正的图论做法,那就是建出最短路图,然后跑割边。
第二类呢?一定是使得最短路长度变短了。否则,根据上面的“去掉一条边”判断法可知,不合法。
代码
因为我用 V S C o d e \tt VS\;Code VSCode ,所以注释的风格比较独特 😂
#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef long long int_;
inline int readint(){
int a = 0; char c = getchar(), f = 1;
for(; c<'0'||c>'9'; c=getchar())
if(c == '-') f = -f;
for(; '0'<=c&&c<='9'; c=getchar())
a = (a<<3)+(a<<1)+(c^48);
return a*f;
}
const int MaxN = 100005;
int n, m;
struct Edge{
int to, nxt, val;
Edge(){ }
Edge(int T,int N,int V){
to = T, nxt = N, val = V;
}
};
Edge e[MaxN<<1];
int head[MaxN], cntE;
/** @param save If true, keep origin. */
void clear_graph(bool save = false){
for(int i=1; i<=n; ++i)
head[i] = -1;
if(!save) cntE = 0;
}
/** @brief add a directed edge */
void addEdge(int a,int b,int c){
e[cntE] = Edge(b,head[a],c);
head[a] = cntE ++;
}
const int_ infty = (1ll<<60)-1;
int_ dis[MaxN]; // 最短路的距离
const int Mod = 1004535809;
int cnt[MaxN]; // 最短路的数量 % Mod
struct Node{
int_ prio; int id;
Node(int_ P,int I):prio(P),id(I){ }
bool operator < (const Node &x) const {
return prio > x.prio;
}
};
priority_queue< Node > pq;
/** @brief 求出到达每个点的最短路距离、数量 */
void dijkstra(int from){
for(int i=1; i<=n; ++i)
dis[i] = infty;
dis[from] = 0, cnt[from] = 1;
pq.push(Node(0,from));
while(!pq.empty()){
Node t = pq.top(); pq.pop();
if(dis[t.id] < t.prio)
continue; // 已经处理过了
for(int i=head[t.id]; ~i; i=e[i].nxt){
int_ xjh = t.prio+e[i].val;
if(dis[e[i].to] > xjh){
dis[e[i].to] = xjh;
cnt[e[i].to] = 0; // no more
pq.push(Node(xjh,e[i].to));
}
if(dis[e[i].to] == xjh){
cnt[e[i].to] += cnt[t.id];
cnt[e[i].to] %= Mod;
}
}
}
}
int_ redis[MaxN]; // 反图最短路
int tmp[MaxN]; // 反图最短路数量
int main(){
n = readint(), m = readint();
int s = readint(), t = readint();
clear_graph(); // before using
for(int i=0,a,b; i<m; ++i){
a = readint(), b = readint();
addEdge(b,a,readint()); // 反图
e[cntE ++].to = b; // 记录 from
}
dijkstra(t);
for(int i=1; i<=n; ++i){
tmp[i] = cnt[i];
redis[i] = dis[i];
}
clear_graph();
for(int i=0,a,b; i<m; ++i){
a = e[i<<1].to;
b = e[i<<1|1].to;
addEdge(a,b,e[i<<1].val);
e[cntE ++].to = a; // 记录 from
}
dijkstra(s);
for(int i=0; i<m; ++i){
int a = e[i<<1|1].to;
int b = e[i<<1].to;
int_ wyl = dis[a]+redis[b];
wyl += e[i<<1].val;
int_ way = 1ll*cnt[a]*tmp[b];
if(wyl == dis[t]) // 最短路的一员
if(way%Mod == cnt[t]){
puts("YES"); continue;
}
wyl -= (dis[t]-1);
if(wyl < e[i<<1].val)
printf("CAN %lld\n",wyl);
else puts("NO");
}
return 0;
}