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 ...
随机推荐
- 坑到了,EF执行带事物的存储过程
用EF开发项目,今天调用 带事物 存储过程,始终报错,"EXECUTE 后的事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配.上一计数 = 1,当前计数 = 0.\r\nEXEC ...
- vs怎么创建MVC及理解其含义
怎么创建MVC项目 一·1.点击 文件à新建à项目à模板àVisua C#(选择 .NET Framework 4.0或以上版本) à选择 MVC 3 Web应用程序 或者MVC 4 Web应用程序à ...
- Exchange之准备AD及域
1. 若有旧版本的Exchange 2003,则需要执行以下命令: setup.com /PrepareLegacyExchangePermissions 2. 准备架 ...
- wsus安装与部署——下
转载请注明原出处 write by xiaoyang 一. 测试 1. 使用客户机或者在域环境下编辑GPO打开组策略 2. 配置自动更新 3. ...
- 项目中重新引用WCF报错
今天在一个项目里,重新更新WCF引用的时候,居然报错了,提示根本找不到那个WCF接口,我赶紧跑去新建了一个空项目,试着用相同的地址引用一下,发现是可以的,完全ok 既然是虚惊一场,那就得想办法把这个W ...
- 利用kvc对UITabBar上的UITabBarButton的尝试修改.md
一.前言 一次比较懒的想法,不想自定义UITabBar,也不想用第三方框架,于是想尝试修改苹果私有类来达到部分效果 效果如下 点击tabBar 上的按钮,图片有变大再变小的动画 tabBar 上某个按 ...
- 细说SQL 连接
连接条件可在FROM或WHERE子句中指定,建议在FROM子句中指定连接条件.WHERE和HAVING子句 也可以包含搜索条件,以进一步筛选连接条件所选的行. ...
- AMQ学习笔记 - 11. Spring-JmsTemplate之执行
概述 前面我们分别介绍了发送.接收和浏览,这三个的实现都依赖于将要介绍的执行. 执行算是一个相对比较底层的方法系列,一般情况下,我们不需要直接面向将要介绍的方法. 执行 1.关于回调接口 在讲执行之前 ...
- 9款精美别致的CSS3菜单和按钮
1.超具立体感的CSS3 3D菜单 菜单项带小图标 记得之前向大家分享过不少CSS3 3D菜单,比如CSS3 3D动画菜单 3D立方体菜单项和HTML5/CSS3自定义下拉框 3D卡片折叠动画,效果都 ...
- transport
#include<iostream> using namespace std; int transport(int a) { ; ) ; else a=a/; d=; ) { a=a*+; ...