Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥
题目链接:
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 最短路+桥的更多相关文章
- 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 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 ...
随机推荐
- MFC中改变控件的大小和位置
用CWnd类的函数MoveWindow()或SetWindowPos()可以改变控件的大小和位置. void MoveWindow(int x,int y,int nWidth,int nHeight ...
- SQL Server 2008R2 禁用远程连接
很多人在开发过程中都会用多数据库(这里仅讨论MSSQL),也都会在服务器上装MSSQL,在你装上MSSQL后,机器上的1433端口就被激活了.如果你的服务器是在内网,也许不用过多的关注,如果你的服务器 ...
- JAVA 模糊查询方法
当我们需要开发一个方法用来查询数据库的时候,往往会遇到这样一个问题:就是不知道用户到底会输入什么条件,那么怎么样处理sql语句才能让我们开发的方法不管接受到什么样的条件都可以正常工作呢?这时where ...
- 基于jquery的inputlimiter 实现字数限制功能
html代码: <td>内容摘要:</td> <td> <textarea id="content_summary" rows=5 nam ...
- 理解C#系列 / 核心C# / 判断&循环&跳转
判断&循环&跳转 说明 本节写的是C#语言的控制程序流的语句,“控制程序流”就是控制程序运行流程的意思. 判断 很容易理解:如果……就…… if语句:测试特定条件是否满足,如果满足就执 ...
- 一些常用css技巧的为什么(二)我所理解的line-height
要用到的基本术语和概念: 替换元素:用作为其他内容占位符的一个元素,或说替换元素内容的部分并非由文档内容直接表示.比如img元素它由文档本身之外的一个图像来替换,比如input元素要由一个单选按钮,复 ...
- 10款很酷的HTML5动画和实用的HTML5应用
1.HTML5的画布花朵生成器可生成多种样式的花朵 HTML5非常流行,利用HTML5制作动画也非常方便,今天要分享一款利用HTML5 Canvas制作的花朵生成器,我们只需要在Canvas画布上点击 ...
- storm 入门
Storm的典型用例有哪些呢? 流处理:正如前面的例子中所展示的,和其他流处理系统不同的是,使用Storm不需要中间队列. 连续计算:向客户端持续发送数据,以便它们能实时更新.显示结果,例如网站统计. ...
- C++ 11 之推导关键词
C++ 11新增了两个推导关键词,auto & decltype 1.区别 auto:用于推导变量类型: decltype: 用于推导表达式或者函数返回值 2.直接上代码 intmain() ...
- Fedora 19 vim c语言开发环境
1. Fedora 19 居然没有自带 gcc 和 g++: sudo yum -y install gcc gcc-c++ 2. 安装 vim 和 cvim 插件: sudo yum -y vim ...