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] 然后 把所有的边反向 处理出在反图上终点到 ...
随机推荐
- AngularJS:包含
ylbtech-AngularJS:包含 1.返回顶部 1. AngularJS 包含 在 AngularJS 中,你可以在 HTML 中包含 HTML 文件. 在 HTML 中包含 HTML 文件 ...
- Rails的静态资源管理(六)—— Asset Pipeline缓存存储方式、预处理、升级等
官方文档:http://guides.ruby-china.org/asset_pipeline.html http://guides.rubyonrails.org/asset_pipeline.h ...
- Rails的静态资源管理(三)—— 开发环境的Asset Pipelin
官方文档:http://guides.ruby-china.org/asset_pipeline.html http://guides.rubyonrails.org/asset_pipeline.h ...
- 2015.3.12Arinc424 Tools中SiniArincCls.csParserFile(string sFile)函数正则表达式理解
原文: string RegEx1 = @"(\[ITEM\]\s*=[\S\s]*?(?=\[ITEM\])|\[ITEM\]\s*=[\S\s]*)";//用来识别主记录和后续 ...
- delphi VCL组件同名继承
当我们在扩展一个 vcl 组件功能的时候,既想保留IDE中能拖动大小与直接设置属性的功能,又想减少写创建与释放代码和安装扩展后新组件的麻烦,那么本文中的方法,就非常实用了. 以给TStringGrid ...
- 部署和调优 1.1 nfs部署和优化-2
更改共享目录文件默认的所有者和所属组 已知道客户端有个user11用户 cat /etc/passwd user11:x:501:501::/home/user11:/bin/bash 服务端打开 v ...
- apache server和tomcat集群配置三:水平集群下的tomcat集群配置
在jsp文件中加入以下代码,用来测试是否共享session: SessionID: <%= session.getId() %> 之前尝试在linux中,但是因为模拟环境是虚拟机,虚拟机只 ...
- VS2010 rdlc报表无法显示“数据源”选项
- [poj2398]Toy Storage
接替关键:和上题类似,输出不同,注意输入这道题需要排序. #include<cstdio> #include<cstring> #include<algorithm> ...
- java 矩阵转置算法
工作中用到了行列转置,把这两种情况的算法记下来,以便后用 1.行列数相等的转置 /** * @description 矩阵转置 * @author oldmonk * @time 2017年8月18日 ...