Complete The Graph

题解:

比较特殊的dij的题目。

dis[x][y] 代表的是用了x条特殊边, y点的距离是多少。

然后我们通过dij更新dis数组。

然后在跑的时候,把特殊边都先当做1在跑,并且经过特殊边的时候,记得将x更新。

然后如果dis[0][t] < L 则代表不用特殊边也会小于L。所以无论是特殊的边答案是多少,dis[0][t]<L也是固定的。

然后我们不断检查dis[c][t] (for c 1 to N) 是不是 <= L 。

找到对应的dis[c][t]之后, 把最短路上的特殊边更新成符合答案的值。 其他特殊边更新成L+1。

代码从学长那里学的, 可以说十分像了。

对我来说比较新颖的是, 把边初始值赋值为L+1,这样就可以在int的范围内跑完全程的dij了, 毕竟如果路径长度 > L 之后就没必要再跑了。

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
  4. #define LL long long
  5. #define ULL unsigned LL
  6. #define fi first
  7. #define se second
  8. #define pb push_back
  9. #define lson l,m,rt<<1
  10. #define rson m+1,r,rt<<1|1
  11. #define lch(x) tr[x].son[0]
  12. #define rch(x) tr[x].son[1]
  13. #define max3(a,b,c) max(a,max(b,c))
  14. #define min3(a,b,c) min(a,min(b,c))
  15. typedef pair<int,int> pll;
  16.  
  17. const int inf = 0x3f3f3f3f;
  18. const int _inf = 0xc0c0c0c0;
  19. const LL INF = 0x3f3f3f3f3f3f3f3f;
  20. const LL _INF = 0xc0c0c0c0c0c0c0c0;
  21. const LL mod = (int)1e9+;
  22. const int N = 1e3 + ;
  23. const int M = 2e4 + ;
  24. int n, m, L, s, t;
  25. int head[N], to[M], val[M], nt[M], tot;
  26. void add(int u, int v, int w){
  27. to[tot] = v;
  28. val[tot] = w;
  29. nt[tot] = head[u];
  30. head[u] = tot++;
  31. }
  32. int dis[N][N];
  33. pll pre[N][N];
  34. typedef tuple<int,int,int> tup;
  35. priority_queue<tup, vector<tup>, greater<tup> > pq;
  36. bool dij(){
  37. for(int i = ; i < N; ++i)
  38. fill(dis[i], dis[i] + N, L+);
  39. dis[][s] = ;
  40. pq.push(tup(,,s));
  41. /// dis , 0-edge, now-point
  42. while(!pq.empty()){
  43. int dd, c, u;
  44. tie(dd, c, u) = pq.top();
  45. pq.pop();
  46. if(dd != dis[c][u]) continue;
  47. for(int i = head[u]; ~i; i = nt[i]){
  48. int v = to[i];
  49. int w = dd + val[i] + (!val[i]);
  50. int nc = c + (!val[i]);
  51. if(nc >= N) continue;
  52. if(dis[nc][v] > w){
  53. dis[nc][v] = w;
  54. pq.push(tup(w, nc, v));
  55. pre[nc][v] = pll(u, i);
  56. }
  57. }
  58. }
  59. if(dis[][t] < L) return false;
  60. if(dis[][t] == L) return true;
  61. for(int c = ; c < N; ++c){
  62. if(dis[c][t] <= L){
  63. int add = L - dis[c][t];
  64. int cc = c, now = t;
  65. while(now != s){
  66. pll tt = pre[cc][now];
  67. now = tt.fi;
  68. if(val[tt.se] == ){
  69. val[tt.se] = val[tt.se^] = add + ;
  70. add = ;
  71. cc--;
  72. }
  73. }
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. int main(){
  80. memset(head, -, sizeof head);
  81. scanf("%d%d%d%d%d", &n, &m, &L, &s, &t);
  82. int u, v, w;
  83. for(int i = ; i <= m; ++i){
  84. scanf("%d%d%d", &u, &v, &w);
  85. add(u, v, w);
  86. add(v, u, w);
  87. }
  88. if(dij()){
  89. puts("YES");
  90. for(int i = ; i < tot; i += ){
  91. if(val[i] == ) val[i] = L + ;
  92. printf("%d %d %d\n", to[i], to[i^], val[i]);
  93. }
  94. }
  95. else puts("NO");
  96. return ;
  97. }

CodeForces 715B Complete The Graph 特殊的dijkstra的更多相关文章

  1. Codeforces 715B. Complete The Graph 最短路,Dijkstra,构造

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF715B.html 题解 接下来说的“边”都指代“边权未知的边”. 将所有边都设为 L+1,如果dis(S,T ...

  2. 715B Complete The Graph

    传送门 题目大意 给出一个图,一些边带权,另一些边等待你赋权(最小赋为1).请你找到一种赋权方式,使得 s 到 t 的最短路为 L n ≤ 1e3 ,m ≤ 1e4 ,L ≤ 1e9 分析 二分所有边 ...

  3. Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))

    B. Complete The Graph time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  4. 【Codeforces】716D Complete The Graph

    D. Complete The Graph time limit per test: 4 seconds memory limit per test: 256 megabytes input: sta ...

  5. ACM - 最短路 - CodeForces 295B Greg and Graph

    CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...

  6. Codeforces 459E Pashmak and Graph(dp+贪婪)

    题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...

  7. CF715B. Complete The Graph

    CF715B. Complete The Graph 题意: 给一张 n 个点,m 条边的无向图,要求设定一些边的边权 使得所有边权都是正整数,最终 S 到 T 的最短路为 L 1 ≤ n ≤ 100 ...

  8. codeforces 372 Complete the Word(双指针)

    codeforces 372 Complete the Word(双指针) 题链 题意:给出一个字符串,其中'?'代表这个字符是可变的,要求一个连续的26位长的串,其中每个字母都只出现一次 #incl ...

  9. codeforces 715B:Complete The Graph

    Description ZS the Coder has drawn an undirected graph of n vertices numbered from 0 to n - 1 and m ...

随机推荐

  1. NYOJ 53 最少步数

    题      目    http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=58 思路借鉴   DFS-Deep First Search-深度优先 ...

  2. linux文本编辑vim命令

    1.Vim Vim  是一个功能强大的全屏幕文本编辑器,是 Linux/UNIX 上最常用的文本编辑器,它的作用是建立.编辑.显示文本文件. Vim 没有菜单,只有命令 2.Vim 工作模式 3.插入 ...

  3. JDK的命令行工具系列 (一) jps、jstat

    概述 在我们进行故障定位和性能分析时, 可以使用Java Dump(也叫Dump文件)来帮助排查问题, 它记录了JVM运行期间的内存占用和线程执行等情况.其中Heap Dump文件是二进制格式, 它保 ...

  4. Python机器学习·微教程

    Python目前是机器学习领域增长最快速的编程语言之一. 该教程共分为11小节.在这个教程里,你将学会: 如何处理数据集,并构建精确的预测模型 使用Python完成真实的机器学习项目 这是一个非常简洁 ...

  5. 闯荡Ext-第一篇

    今天在网上找到了一本非常好的书:<Ext江湖>,这本书是由大漠穷秋大神写的,刚看到这本书的时候,心里面的那个激动劲啊,本来原先的时候心里面就一直念叨着想要学习Ext,但是苦于找不到好的资料 ...

  6. Java基础:数组Array转成List的几种方法

    在编写Java程序中,经常要用的一个转换就是数组和List对象之间的互转. 最简单的方法就是遍历 数组,然后将数组元素依次添加进list中. 此方法略,虽然方法很简单,但总感觉这样的方法有点笨 第二种 ...

  7. android——SQLite数据库存储(操作)

    public class MyDatabaseHelper extends SQLiteOpenHelper { //把定义SQL建表语句成字符串常量 //图书的详细信息 //ID.作者.价格.页数. ...

  8. 大话 Spring Session 共享

    javaweb中我们项目稍微正规点,都会用到单点登录这个技术.实现它的方法各家有各界的看法.这几天由于公司项目需求正在研究.下面整理一下最近整理的心得. 简介 在分布式项目中另我们头疼的是多项目之间的 ...

  9. Tomcat源码分析 (五)----- Tomcat 类加载器

    在研究tomcat 类加载之前,我们复习一下或者说巩固一下java 默认的类加载器.楼主以前对类加载也是懵懵懂懂,借此机会,也好好复习一下. 楼主翻开了神书<深入理解Java虚拟机>第二版 ...

  10. 给debian的docker容器添加crontab定时任务

    现在大部分的docke镜像是基于debian # cat /etc/issue Debian GNU/Linux 9 \n \l Docker容器是不支持后台服务的,像systemctl servic ...