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 ...
随机推荐
- Linux下的sniffer工具--TcpDump的安装和使用
在如今众多的黑客技术中,嗅探器(sniffer)是最常见,也是最重要的技术之一. 用过windows平台上的sniffer工具(例如,netxray和sniffer pro软件)的朋友可能都知道,在共 ...
- [Papers]NSE, $u_3$, Lebesgue space [Cao-Titi, IUMJ, 2008]
$$\bex u_3\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=\frac{2}{3}+\frac{2}{3q},\quad \fra ...
- ASP.NET MVC3 系列教程 - Razor视图引擎基础语法
http://www.cnblogs.com/highend/archive/2011/04/09/aspnet_mvc3_razor_engine.html 4. 关于所有带"_" ...
- Java——泛型(最易懂的方式讲解泛型)
来自: 代码大湿 代码大湿 写在前面: 只要认真看过,基本能很熟悉泛型的特性.泛型是JDK1.5之后出现的,比如JDK1.5之前的ArrayList,会出现2个问题 1:向ArrayList当中添加对 ...
- SRM DIV1 500pt DP
SRM 501 DIV1 500pt SRM 502 DIV1 500pt SRM 508 DIV1 500pt SRM 509 DIV1 500pt SRM 511 DIV1 500pt SRM 5 ...
- 在Windows上,如何卸载RabbitMQ服务
打开运行->CMD->sc delete RabbitMQ 如果报错..... 打开运行->regedit 找到RabbitMQ节点,删掉即可.(右侧看到的都是启动服务时,需要的配置 ...
- mongoDB 3.0 安全权限访问控制 -摘自网络
"E:\Program Files\MongoDB\Server\3.0\bin\mongod.exe" --logpath E:\mongodb\log\mongodblog.l ...
- Java邮件服务学习之四:邮箱服务客户端Spring Mail
一.Spring Mail API Spring邮件抽象层的主要包为org.springframework.mail,Spring提供的邮件发送不仅支持简单邮件的发送.添加附件. 1.邮件发送的核心接 ...
- javascript表单(form)序列化
function serialize(form){ var part =[]; var field = null; var i; var j; var len; var optLen; var opt ...
- HTML5实现动画三种方式
编者注:作者以一个运动的小车为例子,讲述了三种实现HTML5动画的方式,思路清晰,动画不仅仅是canvas,还有css3和javascript.通过合理的选择,来实现最优的实现. PS:由于显卡.录制 ...