题目就是给你一个图,图中部分边没有赋权值,要求你把无权的边赋值,使得s->t的最短路为l。

卡了几周的题了,最后还是经群主大大指点……做出来的……

思路就是跑最短路,然后改权值为最短路和L的差值,直到最短路为L,或者每条无权边都赋值为止。

点是0~n-1的,因为这个错了好几次= =

dijkstra超时,spfa卡过。

看到很多题解说二分,但是实在不能理解那个思路阿…………

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. const int N = ;
  7. const ll INF = 1e13+;
  8.  
  9. int n, m, l, s, t;
  10. struct edge { int from, to, next, w; } e[N*N];
  11. int head[N], cntE;
  12. void addedge(int u, int v, int w) {
  13. e[cntE].from = u; e[cntE].to = v; e[cntE].next = head[u]; e[cntE].w = w; head[u] = cntE++;
  14. e[cntE].from = v; e[cntE].to = u; e[cntE].next = head[v]; e[cntE].w = w; head[v] = cntE++;
  15. }
  16.  
  17. int cnt[N], pre[N];
  18. ll d[N];
  19. bool inq[N];
  20. int spfa() {
  21. queue<int> q;
  22. memset(inq, , sizeof inq);
  23. memset(cnt, , sizeof cnt);
  24. for (int i = ; i <= n; ++i) d[i] = INF;
  25. d[s] = ;
  26. inq[s] = true;
  27. q.push(s);
  28.  
  29. while (q.size()) {
  30. int u = q.front(); q.pop();
  31. inq[u] = false;
  32. for (int i = head[u]; ~i; i = e[i].next) {
  33. int v = e[i].to;
  34. int c = e[i].w;
  35. if (d[u] < INF && d[v] > d[u] + c) {
  36. d[v] = d[u] + c;
  37. pre[v] = u;
  38. if (!inq[v]) {
  39. q.push(v); inq[v] = true;
  40. }
  41. }
  42. }
  43. }
  44. return d[t];
  45. }
  46.  
  47. vector<int> zeroedge;
  48. int main()
  49. {
  50. //freopen("in.txt", "r", stdin);
  51. while (~scanf("%d%d%d%d%d", &n, &m, &l, &s, &t)) {
  52. memset(head, -, sizeof head); cntE = ; zeroedge.clear();
  53. int u, v, c;
  54. for (int i = ; i < m; ++i) {
  55. scanf("%d%d%d", &u, &v, &c);
  56. addedge(u, v, c==?:c);
  57. if (c == ) zeroedge.push_back(cntE-);
  58. }
  59. ll tmp = spfa();
  60. if (tmp > l) {
  61. puts("NO");
  62. continue;
  63. }
  64. if (tmp == l) { // may be dont have zero edge
  65. puts("YES");
  66. for (int i = ; i < cntE; i += ) {
  67. printf("%d %d %d\n", e[i].from, e[i].to, e[i].w);
  68. }
  69. continue;
  70. }
  71. int fg = false;
  72. for (int i = ; i < zeroedge.size(); ++i) {
  73. int x = zeroedge[i];
  74. e[x].w = e[x ^ ].w = l - tmp + ;
  75. tmp = spfa();
  76. if (tmp == l) {
  77. fg = true;
  78. puts("YES");
  79. for (int i = ; i < cntE; i += ) {
  80. printf("%d %d %d\n", e[i].from, e[i].to, e[i].w);
  81. }
  82. break;
  83. }
  84.  
  85. }
  86. if (!fg) puts("NO");
  87. }
  88. return ;
  89. }

Codeforces Round #372 (Div. 1) B. Complete The Graph (枚举+最短路)的更多相关文章

  1. Codeforces Round #372 (Div. 1) B. Complete The Graph

    题目链接:传送门 题目大意:给你一副无向图,边有权值,初始权值>=0,若权值==0,则需要把它变为一个正整数(不超过1e18),现在问你有没有一种方法, 使图中的边权值都变为正整数的时候,从 S ...

  2. Codeforces Round #372 (Div. 2) A .Crazy Computer/B. Complete the Word

    Codeforces Round #372 (Div. 2) 不知不觉自己怎么变的这么水了,几百年前做A.B的水平,现在依旧停留在A.B水平.甚至B题还不会做.难道是带着一种功利性的态度患得患失?总共 ...

  3. Codeforces Round #372 (Div. 2)

    Codeforces Round #372 (Div. 2) C. Plus and Square Root 题意 一个游戏中,有一个数字\(x\),当前游戏等级为\(k\),有两种操作: '+'按钮 ...

  4. 构造图 Codeforces Round #236 (Div. 2) C. Searching for Graph

    题目地址 /* 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结束:) */ ...

  5. 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 ...

  6. Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))

    B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. B. Complete the Word(Codeforces Round #372 (Div. 2)) 尺取大法

    B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. Codeforces Round #372 (Div. 2) A B C 水 暴力/模拟 构造

    A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  9. Codeforces 715A & 716C Plus and Square Root【数学规律】 (Codeforces Round #372 (Div. 2))

    C. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

随机推荐

  1. Java API —— File类

    1.File类的概述         文件和目录路径名的抽象表示形式,创建File对象后,仅仅是一个路径的表示,不代码具体的事物一定是存在的. 2.构造方法         · public File ...

  2. java--面向接口编程

    之前看的一本书的笔记,上周再看设计模式的时候,想到了这篇之前在看某本书时候的笔记. 面向接口编程很重要的一点就是接口回调,用接口声明的变量称作接口变量,属于引用型变量,可以存放实现该接口的类的实例的引 ...

  3. linux 命令学习大全

    转载  http://blog.csdn.net/xiaoguaihai/article/details/8705992/

  4. hibernate--关联映射(多对一,一对一)

    多对一 关联映射 --- many-to-one 场景:用户和组:从用户角度来,多个用户属于一个组(多对一 关联) 使用hibernate开发的思路:先建立对象模型(领域模型),把实体抽取出来. 目前 ...

  5. UINavigationController学习笔记

    http://site.douban.com/129642/widget/notes/5513129/note/187701199/ 1-view controllers的关系:Each custom ...

  6. Android L 使用ART能提高多少性能?

    点击打开链接 刚刚结束的 Google I/O 大会上,Android 下一代操作系统「L」带来不少惊喜.新系统运行更快.更省电. 然而开发者对这个新系统也有颇多疑问,比如新的运行模式 ART 对开发 ...

  7. oracle db shutdown immediate–multi Instance

    [oracle@redhat4 ~]$ sqlplus / as sysdba@orcl SQL*Plus: Release 11.2.0.1.0 Production on Tue Oct 6 21 ...

  8. CentOS 6.5系统使用yum方式安装LAMP环境和phpMyAdmin详细过程

    介绍如何在CentOs6.2下面使用YUM配置安装LAMP环境,一些兄弟也很喜欢使用编译的安装方法,个人觉得如果不是对服务器做定制,用yum安装稳定简单,何必去download&make&am ...

  9. Ionic开发中常见问题和解决方案记录

    1npm按装包失败 更换源:npm config set registry https://registry.npm.taobao.org 或者使用cnpm sudo npm install -g c ...

  10. Codeforces Round #271 (Div. 2)

    A. Keyboard 题意:一个人打字,可能会左偏一位,可能会右偏一位,给出一串字符,求它本来的串 和紫书的破损的键盘一样 #include<iostream> #include< ...