题目链接:

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。

代码:

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<vector>
  5. #include<queue>
  6. using namespace std;
  7.  
  8. typedef __int64 LL;
  9. #define INF (1LL<<61)
  10.  
  11. struct Edge {
  12. int u, v, type,bri,id;
  13. LL w;
  14. Edge(int u, int v, LL w,int id) :u(u), v(v), w(w), type(),bri(),id(id) {}
  15. };
  16.  
  17. void addEdge(vector<int> G[],vector<Edge> &egs,int u, int v, int w,int id=) {
  18. egs.push_back(Edge(u, v, w,id));
  19. G[u].push_back(egs.size() - );
  20. }
  21.  
  22. const int maxn = 2e5 + ;
  23. const int maxm = maxn * ;
  24. int n, m, s, t;
  25.  
  26. vector<int> G[maxn],G2[maxn];
  27. vector<Edge> egs,egs2;
  28.  
  29. struct Heap {
  30. int v; LL d;
  31. Heap(int v, LL d) :v(v), d(d) {}
  32. bool operator < (Heap tmp) const {
  33. return d > tmp.d;
  34. }
  35. };
  36.  
  37. LL d[maxn], d2[maxn];
  38. int done[maxn];
  39. void dijkstral(vector<int> G[], vector<Edge> &egs,LL *d, int s) {
  40. for (int i = ; i < maxn; i++) d[i] = INF;
  41. memset(done, , sizeof(done));
  42. priority_queue<Heap> pq;
  43. d[s] = , pq.push(Heap(s,));
  44. while (!pq.empty()) {
  45. int u = pq.top().v; pq.pop();
  46. if (done[u]) continue;
  47. done[u] = ;
  48. for (int i = ; i < G[u].size(); i++) {
  49. Edge& e = egs[G[u][i]];
  50. if (d[e.v] > d[u] + e.w) {
  51. d[e.v] = d[u] + e.w;
  52. pq.push(Heap(e.v, d[e.v]));
  53. }
  54. }
  55. }
  56. }
  57.  
  58. vector<int> DAG[maxn];
  59. vector<Edge> egs3;
  60.  
  61. int pre[maxn], low[maxn], dfs_clock;
  62. int dfs(vector<int> G[],vector<Edge> &egs,int u) {
  63. int lowu = pre[u] = ++dfs_clock;
  64. int child = ;
  65. for (int i = ; i < G[u].size(); i++) {
  66. Edge &e = egs[G[u][i]];
  67. if (e.type == ) continue;
  68. egs[G[u][i] ^ ].type = ;
  69. if (!pre[e.v]) {
  70. child++;
  71. int lowv = dfs(G, egs, e.v);
  72. lowu = min(lowu, lowv);
  73. if (lowv > pre[u]) {
  74. e.bri = ;
  75. }
  76. }
  77. else if (pre[e.v] < pre[u]) {
  78. lowu = min(lowu, pre[e.v]);
  79. }
  80. }
  81. low[u] = lowu;
  82. return lowu;
  83. }
  84.  
  85. int main() {
  86. scanf("%d%d%d%d", &n, &m, &s, &t),s--,t--;
  87. for (int i = ; i < m; i++) {
  88. int u, v, w;
  89. scanf("%d%d%d", &u, &v, &w),u--,v--;
  90. addEdge(G,egs,u, v, w);
  91. addEdge(G2, egs2, v, u, w);
  92. }
  93. dijkstral(G,egs,d,s);
  94. dijkstral(G2, egs2,d2, t);
  95.  
  96. for (int i = ; i < egs.size(); i++) {
  97. Edge& e = egs[i];
  98. if (e.w+d[e.u]+d2[e.v]==d[t]) {
  99. addEdge(DAG, egs3, e.u, e.v, e.w,i);
  100. addEdge(DAG, egs3, e.v, e.u, e.w,i);
  101. }
  102. }
  103.  
  104. memset(pre, , sizeof(pre));
  105. dfs_clock = ;
  106. dfs(DAG, egs3, s);
  107. for (int i = ; i < egs3.size(); i++) {
  108. Edge& e = egs3[i];
  109. if (e.bri) {
  110. egs[e.id].bri = ;
  111. }
  112. }
  113. for (int i = ; i < egs.size(); i++) {
  114. Edge& e = egs[i];
  115. if (e.bri) printf("YES\n");
  116. else {
  117. LL delta = d[e.u] + d2[e.v] + e.w - d[t] + ;
  118. if (delta<e.w) printf("CAN %I64d\n", delta);
  119. else printf("NO\n");
  120. }
  121. }
  122. return ;
  123. }

总结:

这题跳了两个坑:

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. Part 3 ViewData and ViewBag in mvc

    ViewBag and ViewData is a mechanism(机制) to pass data from controller to view. We use '@' symbol(符号) ...

  2. DataGridView 操作

    //dataGridView 删除选中行 int num = dataGridView2.SelectedRows.Count; ) { DataGridViewRow r = dataGridVie ...

  3. Centos6的VSFTP服务器配置使用教程

    Centos 6 的VSFTP 关闭SELinux,在终端机输入 vi /etc/selinux/config SELINUX=enforcing 改成 SELINUX=disabled 关闭seli ...

  4. 根据DateTime来获取当天是周几(已完结)

    只需要以下代码: @System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(item.CreateTime. ...

  5. josephus问题

    问题描述 n个人围成一圈,号码为1-n,从1开始报数,报到2的退出,剩下的继续从1开始报数,求最后一个人的号码. 算法分析 最直观的算法是用循环链表模拟.从首节点开始,不断删除第二个节点,直到只剩一个 ...

  6. Fedora 20 创建桌面快捷方式

    创建desktop文件 sudo touch /usr/share/applications/sublime.desktop 添加内容 [Desktop Entry] Encoding=UTF-8 N ...

  7. 打造简单实用的Thinkphp分页样式(Bootstrap版本)

    先吐槽一下ThinkPHP3.1版的分页样式,虽然看起来也很简单大方,但是所有的页码全是使用简单的数字,之间的空隙比较小,不大容易点,还有那个“前5页”和“后5页”显得有点多余,因为点击当前显示第一页 ...

  8. GitHub for Windows离线安装的方法

    这几天一直在尝试安装GitHub for windows ,安装程序是从https://windows.github.com/ 下载到的OneClick 部署程序,版本号为2.11.0.5.可能是因为 ...

  9. php生成圆形图片

    http://files.cnblogs.com/files/adtuu/circle_image.zip

  10. hadoop架构

    HADOOP中可以分为两个大的模块,存储模块和计算模块.HDFS作为存储模块,JobTracker,TaskTracker构成计算模块.   1.HADOOP的文件是以HDFS格式存储的   HDFS ...