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 ...
随机推荐
- Part 3 ViewData and ViewBag in mvc
ViewBag and ViewData is a mechanism(机制) to pass data from controller to view. We use '@' symbol(符号) ...
- DataGridView 操作
//dataGridView 删除选中行 int num = dataGridView2.SelectedRows.Count; ) { DataGridViewRow r = dataGridVie ...
- Centos6的VSFTP服务器配置使用教程
Centos 6 的VSFTP 关闭SELinux,在终端机输入 vi /etc/selinux/config SELINUX=enforcing 改成 SELINUX=disabled 关闭seli ...
- 根据DateTime来获取当天是周几(已完结)
只需要以下代码: @System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(item.CreateTime. ...
- josephus问题
问题描述 n个人围成一圈,号码为1-n,从1开始报数,报到2的退出,剩下的继续从1开始报数,求最后一个人的号码. 算法分析 最直观的算法是用循环链表模拟.从首节点开始,不断删除第二个节点,直到只剩一个 ...
- Fedora 20 创建桌面快捷方式
创建desktop文件 sudo touch /usr/share/applications/sublime.desktop 添加内容 [Desktop Entry] Encoding=UTF-8 N ...
- 打造简单实用的Thinkphp分页样式(Bootstrap版本)
先吐槽一下ThinkPHP3.1版的分页样式,虽然看起来也很简单大方,但是所有的页码全是使用简单的数字,之间的空隙比较小,不大容易点,还有那个“前5页”和“后5页”显得有点多余,因为点击当前显示第一页 ...
- GitHub for Windows离线安装的方法
这几天一直在尝试安装GitHub for windows ,安装程序是从https://windows.github.com/ 下载到的OneClick 部署程序,版本号为2.11.0.5.可能是因为 ...
- php生成圆形图片
http://files.cnblogs.com/files/adtuu/circle_image.zip
- hadoop架构
HADOOP中可以分为两个大的模块,存储模块和计算模块.HDFS作为存储模块,JobTracker,TaskTracker构成计算模块. 1.HADOOP的文件是以HDFS格式存储的 HDFS ...