题目链接:

http://codeforces.com/contest/567/problem/E

题意:

给你一个带重边的图,求三类边:

在最短路构成的DAG图中,哪些边是必须经过的;

其他的(包括不在DAG上的边)不是必须经过的边把权值改小多少才能通过,

或者根本不可能通过的。

题解:

从起点s跑一遍最短路得到d[maxn],从终点t跑一遍最短路得到d2[maxn],对于边(u,v,w),如果d[u]+d2[v]+w==d[t]那么这条边在最短路上,对于不在最短路上的边,如果d[u]+d2[v]+w-d[t]+1<w则可以重建,否则输出NO。

对于所有最短路构成的DAG(建成无向图)跑一遍tarjan求割边,所有的割边输出YES。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std; typedef __int64 LL;
#define INF (1LL<<61) struct Edge {
int u, v, type,bri,id;
LL w;
Edge(int u, int v, LL w,int id) :u(u), v(v), w(w), type(),bri(),id(id) {}
}; void addEdge(vector<int> G[],vector<Edge> &egs,int u, int v, int w,int id=) {
egs.push_back(Edge(u, v, w,id));
G[u].push_back(egs.size() - );
} const int maxn = 2e5 + ;
const int maxm = maxn * ;
int n, m, s, t; vector<int> G[maxn],G2[maxn];
vector<Edge> egs,egs2; struct Heap {
int v; LL d;
Heap(int v, LL d) :v(v), d(d) {}
bool operator < (Heap tmp) const {
return d > tmp.d;
}
}; LL d[maxn], d2[maxn];
int done[maxn];
void dijkstral(vector<int> G[], vector<Edge> &egs,LL *d, int s) {
for (int i = ; i < maxn; i++) d[i] = INF;
memset(done, , sizeof(done));
priority_queue<Heap> pq;
d[s] = , pq.push(Heap(s,));
while (!pq.empty()) {
int u = pq.top().v; pq.pop();
if (done[u]) continue;
done[u] = ;
for (int i = ; i < G[u].size(); i++) {
Edge& e = egs[G[u][i]];
if (d[e.v] > d[u] + e.w) {
d[e.v] = d[u] + e.w;
pq.push(Heap(e.v, d[e.v]));
}
}
}
} vector<int> DAG[maxn];
vector<Edge> egs3; int pre[maxn], low[maxn], dfs_clock;
int dfs(vector<int> G[],vector<Edge> &egs,int u) {
int lowu = pre[u] = ++dfs_clock;
int child = ;
for (int i = ; i < G[u].size(); i++) {
Edge &e = egs[G[u][i]];
if (e.type == ) continue;
egs[G[u][i] ^ ].type = ;
if (!pre[e.v]) {
child++;
int lowv = dfs(G, egs, e.v);
lowu = min(lowu, lowv);
if (lowv > pre[u]) {
e.bri = ;
}
}
else if (pre[e.v] < pre[u]) {
lowu = min(lowu, pre[e.v]);
}
}
low[u] = lowu;
return lowu;
} int main() {
scanf("%d%d%d%d", &n, &m, &s, &t),s--,t--;
for (int i = ; i < m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w),u--,v--;
addEdge(G,egs,u, v, w);
addEdge(G2, egs2, v, u, w);
}
dijkstral(G,egs,d,s);
dijkstral(G2, egs2,d2, t); for (int i = ; i < egs.size(); i++) {
Edge& e = egs[i];
if (e.w+d[e.u]+d2[e.v]==d[t]) {
addEdge(DAG, egs3, e.u, e.v, e.w,i);
addEdge(DAG, egs3, e.v, e.u, e.w,i);
}
} memset(pre, , sizeof(pre));
dfs_clock = ;
dfs(DAG, egs3, s);
for (int i = ; i < egs3.size(); i++) {
Edge& e = egs3[i];
if (e.bri) {
egs[e.id].bri = ;
}
}
for (int i = ; i < egs.size(); i++) {
Edge& e = egs[i];
if (e.bri) printf("YES\n");
else {
LL delta = d[e.u] + d2[e.v] + e.w - d[t] + ;
if (delta<e.w) printf("CAN %I64d\n", delta);
else printf("NO\n");
}
}
return ;
}

总结:

这题跳了两个坑:

1、距离和会爆int:

发现问题之后改了d[maxn],但是一直没发现Heap结构体里面的d没有改!!!!wa了七八次!

2、卡spfa的时间!!!

。。

代码调试出错误的时候,改正一定要彻底!不要有的地方改了有的地方漏了!比如dijkstra,改了d还要改Heap结构体!!!

Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥的更多相关文章

  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 ...

  2. Codeforces Round #Pi (Div. 2) 567E President and Roads ( dfs and similar, graphs, hashing, shortest paths )

    图给得很良心,一个s到t的有向图,权值至少为1,求出最短路,如果是一定经过的边,输出"YES",如果可以通过修改权值,保证一定经过这条边,输出"CAN",并且输 ...

  3. map Codeforces Round #Pi (Div. 2) C. Geometric Progression

    题目传送门 /* 题意:问选出3个数成等比数列有多少种选法 map:c1记录是第二个数或第三个数的选法,c2表示所有数字出现的次数.别人的代码很短,思维巧妙 */ /***************** ...

  4. 构造 Codeforces Round #Pi (Div. 2) B. Berland National Library

    题目传送门 /* 题意:给出一系列读者出行的记录,+表示一个读者进入,-表示一个读者离开,可能之前已经有读者在图书馆 构造:now记录当前图书馆人数,sz记录最小的容量,in数组标记进去的读者,分情况 ...

  5. Codeforces Round #Pi (Div. 2) ABCDEF已更新

    A. Lineland Mail time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #Pi (Div. 2) A. Lineland Mail 水

    A. Lineland MailTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567/proble ...

随机推荐

  1. Apache windows多线程设置

    # WinNT MPM # ThreadsPerChild: constant number of worker threads in the server process # MaxRequests ...

  2. AMQ学习笔记 - 11. Spring-JmsTemplate之执行

    概述 前面我们分别介绍了发送.接收和浏览,这三个的实现都依赖于将要介绍的执行. 执行算是一个相对比较底层的方法系列,一般情况下,我们不需要直接面向将要介绍的方法. 执行 1.关于回调接口 在讲执行之前 ...

  3. GPRS组网的几种方案【来自网络】

    GPRS组网的几种方案:1) 方案一:中心采用ADSL等INTELNET公网连接,采用公网固定IP或者公网动态IP+DNS解析服务.此种方案向先INTERNET运营商申请ADSL等宽带业务.     ...

  4. (转)集成架构:对比 Web API 与面向服务的架构和企业应用程序集成

    摘要:总体上讲,SOA 和 Web API 似乎解决的是同一个问题:以实时的.可重用的方式公开业务功能.本教程将分析这些举措有何不同,以及如何将它们融入到一个不断演变的集成架构中.文中还将讨论 API ...

  5. Android实现Http协议案例

    在Android开发中,使用Http协议实现网络之间的通信是随处可见的,使用http方式主要采用2中请求方式即get和post两种方式. 一.使用get方式: HttpGet httpGet = ne ...

  6. jQuery checkBox 全选的例子

    表单处理时经常会有全选的功能,但是这个功能往往会被忽视一个细节,就是逐个选中 checkBox 直至全选时,经常会忘记修改全选 checkBox 的状态,某知名互联网公司的网盘就会出现这样的问题,问题 ...

  7. C++ 双链表基本操作

    上一篇博客主要总结了单向链表,这次再总结一下双向链表. 1.概念 双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都 ...

  8. Vim保存文件命令 ":wq" 与 ":x" 的区别

    CSDN转载 [1] Vim是Unix/Linux系统最常用的编辑器之一,在保存文件时,我通常选择":wq",因为最开始学习vim的时候,就只记住了几个常用的命令:也没有细究命令的 ...

  9. Flex-box 学习

    .flex-cont{ /*定义为flexbox的“父元素”*/ display: -webkit-box; display: -webkit-flex; display: flex; /*子元素沿主 ...

  10. 批处理cmd背景颜色

    Pause  Echo 其他提示语 & pause > nul     pause暂停 COLOR 设置默认的控制台前景和背景颜色. COLOR [attr] attr 指定控制台输出的 ...