Codeforces Round #Pi (Div. 2) E. President and Roads tarjan+最短路
E. President and Roads
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/567/problem/E
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 n, m, s 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 ≤ n; ai ≠ 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 Input
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
Sample Output
YES
CAN 2
CAN 1
CAN 1
CAN 1
CAN 1
YES
HINT
题意
一个有向带重边的图,对于每条边,问你最短路是否必须进过这条边,否则的话,问你最少减少这条边的边权多少,就可以最短路经过这个边了
如果还是不行的话,就输出NO
题解:
比较裸的题,跑一发正常的最短路,然后建反向边,跑一发最短路,YES的判断是由带重边的tarjan来求,求桥边就好了
代码来自zenzentorwie
代码
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = ;
#define INF (1LL<<61)
typedef long long ll; struct Dijkstra {
struct node {
ll d;
int u;
bool operator < (const node& b) const {
return d > b.d;
}
node() {}
node(ll _d, int _u): d(_d), u(_u) {}
}; struct Edge {
int from, to, id;
ll dist;
Edge() {}
Edge(int u, int v, ll w) : from(u), to(v), dist(w){}
};
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
bool done[maxn];
ll d[maxn];
int p[maxn]; void init(int n) {
this->n = n;
for (int i = ; i <= n; i++) G[i].clear();
edges.clear();
} void addEdge(int from, int to, ll dist) {
edges.push_back(Edge(from, to, dist));
m = edges.size();
G[from].push_back(m-);
} void dijkstra(int s) {
priority_queue<node> Q;
for (int i = ; i <= n; i++) d[i] = INF;
d[s] = ;
memset(done, , sizeof(done));
Q.push(node(, s));
while (!Q.empty()) {
node x = Q.top(); Q.pop();
int u = x.u;
if (done[u]) continue;
done[u] = true;
for (int i = ; i < G[u].size(); i++) {
Edge& e = edges[G[u][i]];
if (d[e.to] > d[u] + e.dist) {
d[e.to] = d[u] + e.dist;
p[e.to] = G[u][i];
Q.push(node(d[e.to], e.to));
}
}
}
}
} S, T; int dfn[maxn]; // 时间戳
int dfs_clock; // dfs时间变量
int low[maxn]; // u及u的后代在DFS树上能够到达的最早的祖先 struct Edge {
int u, v, w, id;
Edge(int a=, int b=, int w=, int c=) : u(a), v(b), w(w), id(c) {}
} e[*maxn]; vector<Edge> G[maxn];
bool isbridge[*maxn]; int dfs(int u, int la) {
int lowu = dfn[u] = ++dfs_clock; // dfs_clock在调用dfs前要初始化为0
int child = ; // 子节点个数
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i].v;
if (!dfn[v]) { // 未访问过,树边
int lowv = dfs(v, G[u][i].id);
lowu = min(lowu, lowv);
if (lowv > dfn[u]) { // 判断桥
isbridge[G[u][i].id] = ;
}
}
else if (dfn[v] < dfn[u] && G[u][i].id != la) { // 反向边
lowu = min(lowu, dfn[v]);
}
}
low[u] = lowu;
return lowu;
} int ison[*maxn];
int can[*maxn]; int main() {
int n, m, s, t;
scanf("%d%d%d%d", &n, &m, &s, &t);
S.init(n+);
T.init(n+);
int u, v, w;
for (int i = ; i <= m; i++){
scanf("%d%d%d", &u, &v, &w);
e[i] = Edge(u, v, w, i);
S.addEdge(u, v, w);
T.addEdge(v, u, w);
}
S.dijkstra(s);
T.dijkstra(t);
ll ddd = S.d[t];
ll delta;
if (S.d[t] == INF) {
for (int i = ; i <= m; i++) printf("NO\n");
}
else {
for (int i = ; i <= m; i++) {
u = e[i].u;
v = e[i].v;
w = e[i].w;
if (S.d[u] + w == S.d[v] && T.d[v] + w == T.d[u]) {
G[u].push_back(Edge(u, v, w, i));
G[v].push_back(Edge(v, u, w, i));
ison[i] = ;
}
}
dfs(s, -);
for (int i = ; i <= m; i++) {
if (isbridge[i]) {
printf("YES\n");
}
else {
delta = S.d[e[i].u] + T.d[e[i].v] + e[i].w - ddd + ;
if (delta < e[i].w) printf("CAN %I64d\n", delta);
else printf("NO\n");
}
}
} return ;
}
Codeforces Round #Pi (Div. 2) E. President and Roads tarjan+最短路的更多相关文章
- Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥
题目链接: http://codeforces.com/contest/567/problem/E 题意: 给你一个带重边的图,求三类边: 在最短路构成的DAG图中,哪些边是必须经过的: 其他的(包括 ...
- Codeforces Round #Pi (Div. 2) 567E President and Roads ( dfs and similar, graphs, hashing, shortest paths )
图给得很良心,一个s到t的有向图,权值至少为1,求出最短路,如果是一定经过的边,输出"YES",如果可以通过修改权值,保证一定经过这条边,输出"CAN",并且输 ...
- map Codeforces Round #Pi (Div. 2) C. Geometric Progression
题目传送门 /* 题意:问选出3个数成等比数列有多少种选法 map:c1记录是第二个数或第三个数的选法,c2表示所有数字出现的次数.别人的代码很短,思维巧妙 */ /***************** ...
- 构造 Codeforces Round #Pi (Div. 2) B. Berland National Library
题目传送门 /* 题意:给出一系列读者出行的记录,+表示一个读者进入,-表示一个读者离开,可能之前已经有读者在图书馆 构造:now记录当前图书馆人数,sz记录最小的容量,in数组标记进去的读者,分情况 ...
- Codeforces Round #Pi (Div. 2) ABCDEF已更新
A. Lineland Mail time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships set乱搞
D. One-Dimensional Battle ShipsTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...
- Codeforces Round #Pi (Div. 2) C. Geometric Progression map
C. Geometric Progression Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- Codeforces Round #Pi (Div. 2) B. Berland National Library set
B. Berland National LibraryTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- Codeforces Round #Pi (Div. 2) A. Lineland Mail 水
A. Lineland MailTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567/proble ...
随机推荐
- web项目Log4j日志输出路径配置问题
问题描述:一个web项目想在一个tomcat下运行多个实例(通过修改war包名称的实现),然后每个实例都将日志输出到tomcat的logs目录下实例名命名的文件夹下进行区分查看每个实例日志,要求通过尽 ...
- Oracle数据库导出
一. pl/SQL方式 1.打开plsql,找到工具栏,导出表
- Headmaster's Headache
题意: s门课程,现任老师有m个给出工资,和他们能教的课,现在有n个应聘的老师,给出费用和能教的课程标号,求使每门课都至少有两个老师教的最小花费 分析: n个老师选或不选有背包的特征,n很小想到用状压 ...
- bjfu1100 圆环
这题也是2011百度之星的一道题.知道做法后代码极简单. 不过我做完后随便上网搜了一下,发现竟然还有很多不同的做法.别的做法我就不管了,我只把我的做法的原理说清楚.我做题时是按如下顺序逐步找到规律的: ...
- CAKeyframeAnimation
之所以叫做关键帧动画是因为,这个类可以实现,某一属性按照一串的数值进行动画,就好像制作动画的时候一帧一帧的制作一样. 一般使用的时候 首先通过 animationWithKeyPath 方法 创建一 ...
- List转换成Json、对象集合转换Json等
#region List转换成Json /// <summary> /// List转换成Json /// </summary> public static string Li ...
- Libsvm的MATLAB调用和交叉验证
今天听了一个师兄的讲课,才发现我一直在科研上特别差劲,主要表现在以下几个方面,(现在提出也为了督促自己在以后的学习工作道路上能够避免这些问题) 1.做事情总是有头无尾,致使知识点不能一次搞透,每次在用 ...
- ORA-15018: diskgroup cannot be created
创建ASM磁盘组的时候出错,具体报错如下: SQL> create diskgroup kel external redundancy disk 'ORCL:KEL1','ORCL:KEL2'; ...
- Tomcat中的线程池StandardThreadExecutor
之所以今天讨论它,因为在motan的的NettyServer中利用它这个线程池可以作为业务线程池,它定制了一个自己的线程池.当然还是基于jdk中的ThreadExecutor中的构造方法和execut ...
- Linux下用Intel编译器编译安装NetCDF-Fortan库(4.2以后版本)
本来这个问题真的没必要写的,可是真的困扰我太久%>_<%,决定还是记录一下. 首先,最权威清晰的安装文档还是官方的: Building the NetCDF-4.2 and later F ...