CF567E President and Roads
\(\color{#0066ff}{ 题目描述 }\)
给出一个有向图,从起点走到终点(必须走最短路),问一条边是否一定会被经过,如果不经过它,可以减小它的多少边权使得经过它(边权不能减少到0)
\(\color{#0066ff}{输入格式}\)
第一行包含四个整数n,m,s和t(\(2 \leq n \leq 10^5; 1 \leq m \leq 10^5,1 \leq s, t \leq n\)) -在BERLAND城市,道路的数量,首都和总统家乡(s ≠ t)。
接下来的m条线路包含道路。各道路被给定为一组三个整数ai, bi, li (1 ≤ ai, bi ≤ n; ai ≠ bi; 1 ≤ li ≤ \(10^6\)) -由连接在城市第i条道路以及骑行所需的时间。道路从城市a 直达城市b i。
城市编号从1到n。每对城市之间可以有多条道路。保证沿着道路有从s到t的路径
\(\color{#0066ff}{输出格式}\)
打印m行。第i行应包含有关第i条道路的信息(道路按出现在输入中的顺序编号)。
如果总统在旅行期间肯定会骑它,则该行必须包含一个单词“ 是 ”(没有引号)。
否则,如果能够修复第i条道路,使其上的旅行时间保持正面,那么总统肯定会骑在它上面,打印空格分隔的单词“ CAN ”(不带引号)以及修复的最小费用。
如果我们不能让总统肯定会骑在这条路上,请打印“ 否 ”(没有引号)。
注意 修复道路的费用是修复前后骑车所需的时间之差。
\(\color{#0066ff}{输入样例}\)
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
3 3 1 3
1 2 10
2 3 10
1 3 100
2 2 1 2
1 2 1
1 2 2
\(\color{#0066ff}{输出样例}\)
YES
CAN 2
CAN 1
CAN 1
CAN 1
CAN 1
YES
YES
YES
CAN 81
YES
NO
\(\color{#0066ff}{数据范围与提示}\)
none
\(\color{#0066ff}{ 题解 }\)
从S正向建图跑DIJ,从T反向建图跑DIJ
考虑一个边,如果一定在最短路上首先用两端点dis+边权判断,然后如果是必经边,显然是最短路树上的割边
然而好像写挂了qwq
还有一种方法是利用最短路计数来算
一条边必经,当且仅当\(diss[x]+dist[y]+z=diss[t] \&\&cnts[x]*cntt[y]=cnts[t]\)
但是cnt会炸LL
于是开始取模
然而这题TM还卡模数,调了好久
对于非必经边,若让它必经,显然让它减小一些权值使得恰好比最短路小1就行了
判断一下是否到0就行
#include<bits/stdc++.h>
#define LL long long
#define int long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
using std::pair;
using std::make_pair;
const int mox = 998244353;
const int mod = 1004535809;
const int maxn = 1e5 + 200;
const LL inf = 999999999999999LL;
struct node {
int to;
LL dis;
node *nxt;
node(int to = 0, LL dis = 0, node *nxt = NULL): to(to), dis(dis), nxt(nxt) {}
void *operator new(size_t) {
static node *S = NULL, *T = NULL;
return (S == T) && (T = (S = new node[1024]) + 1024), S++;
}
};
struct E {
LL x, y, z;
}e[maxn];
int n, m, S, T;
node *head[maxn], *h[maxn];
LL diss[maxn], dist[maxn], cnts[maxn], cntt[maxn], cntss[maxn], cnttt[maxn];
bool vis[maxn];
std::priority_queue<pair<LL, int>, std::vector<pair<LL, int> >, std::greater<pair<LL, int> > > q;
void add(int from, int to, int dis, node **hh) {
hh[from] = new node(to, dis, hh[from]);
}
void dij(int s, LL *dis, node **hh, LL *cnt, LL *ccnt) {
for(int i = 1; i <= n; i++) dis[i] = inf, vis[i] = false;
q.push(make_pair(dis[s] = 0, s));
cnt[s] = 1;
while(!q.empty()) {
int tp = q.top().second; q.pop();
if(vis[tp]) continue;
vis[tp] = true;
for(node *i = hh[tp]; i; i = i->nxt) {
if(dis[i->to] > dis[tp] + i->dis)
q.push(make_pair(dis[i->to] = dis[tp] + i->dis, i->to)), cnt[i->to] = cnt[tp] % mod, ccnt[i->to] = ccnt[tp] % mox;
else if(dis[i->to] == dis[tp] + i->dis) (cnt[i->to] += cnt[tp]) %= mod, (ccnt[i->to] += ccnt[tp]) %= mox;
}
}
}
signed main() {
n = in(), m = in(), S = in(), T = in();
for(int i = 1; i <= m; i++) {
E &o = e[i];
o.x = in(), o.y = in(), o.z = in();
add(o.x, o.y, o.z, head);
add(o.y, o.x, o.z, h);
}
dij(S, diss, head, cnts, cntss);
dij(T, dist, h, cntt, cnttt);
for(int i = 1; i <= m; i++) {
E o = e[i];
if(diss[o.x] + dist[o.y] + o.z == diss[T] && (cnts[o.x] * cntt[o.y] % mod) == (cnts[T] % mod) && (cntss[o.x] * cnttt[o.y] % mod) == (cntss[T] % mod)) printf("YES\n");
else {
LL now = diss[o.x] + dist[o.y] + o.z;
LL goal = diss[T] - 1;
LL dt = now - goal;
if(o.z - dt <= 0) printf("NO\n");
else printf("CAN %lld\n", dt);
}
}
return 0;
}
CF567E President and Roads的更多相关文章
- cf567E. President and Roads(最短路计数)
题意 题目链接 给出一张有向图,以及起点终点,判断每条边的状态: 是否一定在最短路上,是的话输出'YES' 如果不在最短路上,最少减去多少权值会使其在最短路上,如果减去后的权值\(< 1\),输 ...
- 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 ...
- 【CodeForces 567E】President and Roads(最短路)
Description Berland has n cities, the capital is located in city s, and the historic home town of th ...
- Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥
题目链接: http://codeforces.com/contest/567/problem/E 题意: 给你一个带重边的图,求三类边: 在最短路构成的DAG图中,哪些边是必须经过的: 其他的(包括 ...
- codeforces567E. President and Roads
题目大意:总统要回家,会经过一些街道,每条街道都是单向的并且拥有权值.现在,为了让总统更好的回家,要对每一条街道进行操作:1)如果该街道一定在最短路上,则输出“YES”.2)如果该街道修理过后,该边所 ...
- Codeforces.567E.President and Roads(最短路 Dijkstra)
题目链接 \(Description\) 给定一张有向图,求哪些边一定在最短路上.对于不一定在最短路上的边,输出最少需要将其边权改变多少,才能使其一定在最短路上(边权必须为正,若仍不行输出NO). \ ...
- Codeforces Round #Pi (Div. 2) 567E President and Roads ( dfs and similar, graphs, hashing, shortest paths )
图给得很良心,一个s到t的有向图,权值至少为1,求出最短路,如果是一定经过的边,输出"YES",如果可以通过修改权值,保证一定经过这条边,输出"CAN",并且输 ...
- codeforces 567 E. President and Roads 【 最短路 桥 】
给出一个有向图,从起点走到终点(必须走最短路),问一条边是否一定会被经过,如果不经过它,可以减小它的多少边权使得经过它(边权不能减少到0) 正反向建图,分别求出起点到每个点的最短距离,终点到每个点的最 ...
- 【Henu ACM Round#14 F】 President and Roads
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 处理出起点到任意点的最短路以及最短路条数=>dis[0][i],cnt[0][i] 然后 把所有的边反向 处理出在反图上终点到 ...
随机推荐
- QQ控件时光轴特效总结
1.插入HTML数据 插入html代码,一般的做法是通过document.getElementById("").innerHTML来实现. 然而在该控件中,它通过JS replac ...
- postgresql 数据库,模式,表空间的关系
数据库与模式模式(schema)是对数据库(database)逻辑分割在数据库创建的同时,就已经默认为数据库创建了一个模式--public,这也是该数据库的默认模式.所有为此数据库创建的对象(表.函数 ...
- 2015.2.27 UltraEdit中显示XML结构
1选择菜单项 "视图"->"显示方式(着色文件类型)"->"XML": 2选择菜单项 "格式"->&q ...
- Recovery of DISKGROUP in VXVM (ZT)
http://gurkulindia.com/main/2012/03/recovery-of-diskgroup-in-vxvm-veritas-volume-manager/# Since lon ...
- 10-20C#基础---一维、二维数组&&冒泡排序
一.一维数组 1.定义:是某一种数据类型的数据的组合,数组用来分组基本类型或相同类型的对象.数组中的实体叫做数组的元素或成员. 2. 格式:int[ ] shuzu=new int[ 6];存放int ...
- JS中,关于数组的练习题
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Android键盘属性
在主xml中android:windowSoftInputMode的属性"stateUnspecified"软键盘的状态(是否它是隐藏或可见)没有被指定.系统将选择一个合适的状态或 ...
- SQL基础E-R图基础
ER图分为实体.属性.关系三个核心部分.实体是长方形体现,而属性则是椭圆形,关系为菱形. ER图的实体(entity)即数据模型中的数据对象,例如人.学生.音乐都可以作为一个数据对象,用长方体来表示, ...
- session和cookie个字消除的方法
session消除的方法就是: session_destroy(); cookie消除的方法就是setcookie()函数的时间设为当前时间即可 if(isset($_COOKIE['adminId' ...
- java web前端发送请求的4种方式
表单 action, 链接href,js 绑定按钮 ajax绑定标签 <h1>通过表单提交参数</h1> <form action="/day46_v1/Ser ...