D. Complete The Graph

time limit per test: 4 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

ZS the Coder has drawn an undirected graph of n vertices numbered from 0 to n - 1 and m edges between them. Each edge of the graph is weighted, each weight is a positive integer.

The next day, ZS the Coder realized that some of the weights were erased! So he wants to reassign positive integer weight to each of the edges which weights were erased, so that the length of the shortest path between vertices s and t in the resulting graph is exactly L. Can you help him?

Input

The first line contains five integers n, m, L, s, t (2 ≤ n ≤ 1000,  1 ≤ m ≤ 10 000,  1 ≤ L ≤ 109,  0 ≤ s, t ≤ n - 1,  s ≠ t) — the number of vertices, number of edges, the desired length of shortest path, starting vertex and ending vertex respectively.

Then, m lines describing the edges of the graph follow. i-th of them contains three integers, ui, vi, wi(0 ≤ ui, vi ≤ n - 1,  ui ≠ vi,  0 ≤ wi ≤ 109). ui and vi denote the endpoints of the edge and wi denotes its weight. If wi is equal to 0 then the weight of the corresponding edge was erased.

It is guaranteed that there is at most one edge between any pair of vertices.

Output

Print "NO" (without quotes) in the only line if it's not possible to assign the weights in a required way.

Otherwise, print "YES" in the first line. Next m lines should contain the edges of the resulting graph, with weights assigned to edges which weights were erased. i-th of them should contain three integers uivi and wi, denoting an edge between vertices ui and vi of weight wi. The edges of the new graph must coincide with the ones in the graph from the input. The weights that were not erased must remain unchanged whereas the new weights can be any positive integer not exceeding 1018.

The order of the edges in the output doesn't matter. The length of the shortest path between s and t must be equal to L.

If there are multiple solutions, print any of them.

Examples

input
  1. 5 5 13 0 4
    0 1 5
    2 1 2
    3 2 3
    1 4 0
    4 3 4
output
  1. YES
    0 1 5
    2 1 2
    3 2 3
    1 4 8
    4 3 4
input
  1. 2 1 999999999 1 0
    0 1 1000000000
output
  1. NO

Note

Here's how the graph in the first sample case looks like :

In the first sample case, there is only one missing edge weight. Placing the weight of 8 gives a shortest path from 0 to 4 of length 13.

In the second sample case, there is only a single edge. Clearly, the only way is to replace the missing weight with 123456789.

In the last sample case, there is no weights to assign but the length of the shortest path doesn't match the required value, so the answer is "NO".

Understanding

给一个无向图,有一些权值为0的边要重构成正数权值,使得s→t的最短路为l

Solution

特判掉原本最短路(没有加入0边)<l→"NO"

分析一下,s→t的最短路,贪心,加入0边,先不管取值,先一条条赋为1(最小positive integer),如果当前最短路<l是不是说明当前这条边是最短路的一部分,所以答案就出来了,将这条边赋值为l-d[t]+1,如果最短路仍>l,而这已经是这条边的最优贡献,所以赋值为1继续做.

然后注意一下细节即可(WA了4发~~)

复杂度:O(nmlogn)(优化:对了,每次做最短路,当前值已经>l可以直接退出,所以其实跑得很快296MS)

  1. // This file is made by YJinpeng,created by XuYike's black technology automatically.
  2. // Copyright (C) 2016 ChangJun High School, Inc.
  3. // I don't know what this program is.
  4.  
  5. #include <iostream>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <cstring>
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <cmath>
  12. #include <queue>
  13. #define IN inline
  14. #define RG register
  15. #define MOD 1000000007
  16. #define INF 1e9+1
  17. using namespace std;
  18. typedef long long LL;
  19. const int MAXN=1010;
  20. const int MAXM=20010;
  21. inline int gi() {
  22. register int w=0,q=0;register char ch=getchar();
  23. while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
  24. if(ch=='-')q=1,ch=getchar();
  25. while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
  26. return q?-w:w;
  27. }
  28. int tot,cnt,S[MAXM],T[MAXM];
  29. struct Dijskra{
  30. static const int N=1010,M=(N*10)<<1;
  31. int n,t,l;int d[N],fr[N];int to[M],ne[M],be[M],W[M];bool u[N];
  32. struct node{
  33. int s,p;
  34. bool operator<(node a)const{return s>a.s;}
  35. };
  36. priority_queue<node>q;
  37. IN void link(RG int u,RG int v,RG int w){
  38. //if(w>l)return; this some edges didn't get
  39. to[++t]=v;ne[t]=fr[u];fr[u]=t;W[t]=w;be[t]=u;
  40. }
  41. int Dij(int begin,int end){
  42. for(int i=0;i<n;i++)d[i]=INF;
  43. q.push((node){d[begin]=0,begin});memset(u,0,sizeof(u));
  44. while(!q.empty()){
  45. while(u[q.top().p]&&!q.empty())q.pop();
  46. if(q.empty())break;
  47. int x=q.top().p;q.pop();u[x]=1;
  48. if(x==end||d[x]>l)break;
  49. for(int o=fr[x],y;y=to[o],o;o=ne[o])
  50. if(d[x]+W[o]<d[y]&&d[x]+W[o]<=l){
  51. d[y]=d[x]+W[o];
  52. q.push((node){d[y],y});
  53. }
  54. }
  55. return d[end];
  56. }
  57. void pri(int end){
  58. printf("YES\n");
  59. for(int i=1;i<t-1;i+=2)
  60. printf("%d %d %d\n",be[i],to[i],W[i]);
  61. printf("%d %d %d\n",be[t-1],to[t-1],l-d[end]+W[t-1]);//this W[t-1]
  62. for(int i=tot;i<=cnt;i++)printf("%d %d %d\n",S[i],T[i],(int)INF);exit(0);
  63. }
  64. }e;
  65. int main()
  66. {
  67. freopen("D.in","r",stdin);
  68. freopen("D.out","w",stdout);
  69. int n=gi(),m=gi(),l=gi(),s=gi(),t=gi();e.l=l;e.n=n;
  70. for(int i=1;i<=m;i++){
  71. int u=gi(),v=gi(),w=gi();
  72. if(w)e.link(u,v,w),e.link(v,u,w);
  73. else S[++cnt]=u,T[cnt]=v;
  74. }tot=1;//this
  75. if(e.Dij(s,t)<l){printf("NO");return 0;}
  76. if(e.d[t]==l)e.pri(t);
  77. for(;tot<=cnt;tot++){
  78. e.link(S[tot],T[tot],1);e.link(T[tot],S[tot],1);
  79. if(e.Dij(s,t)<=l)break;
  80. }tot++;
  81. if(tot>cnt+1)return 0*puts("NO");
  82. e.pri(t);
  83. return 0;
  84. }

【Codeforces】716D Complete The Graph的更多相关文章

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

  2. 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环

    [题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一 ...

  3. 【Codeforces】Round #491 (Div. 2) 总结

    [Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...

  4. 【Codeforces】Round #488 (Div. 2) 总结

    [Codeforces]Round #488 (Div. 2) 总结 比较僵硬的一场,还是手速不够,但是作为正式成为竞赛生的第一场比赛还是比较圆满的,起码没有FST,A掉ABCD,总排82,怒涨rat ...

  5. 【CodeForces】841D. Leha and another game about graph(Codeforces Round #429 (Div. 2))

    [题意]给定n个点和m条无向边(有重边无自环),每个点有权值di=-1,0,1,要求仅保留一些边使得所有点i满足:di=-1或degree%2=di,输出任意方案. [算法]数学+搜索 [题解] 最关 ...

  6. 【CodeForces】601 D. Acyclic Organic Compounds

    [题目]D. Acyclic Organic Compounds [题意]给定一棵带点权树,每个点有一个字符,定义一个结点的字符串数为往下延伸能得到的不重复字符串数,求min(点权+字符串数),n&l ...

  7. 【Codeforces】849D. Rooter's Song

    [算法]模拟 [题意]http://codeforces.com/contest/849/problem/D 给定n个点从x轴或y轴的位置p时间t出发,相遇后按对方路径走,问每个数字撞到墙的位置.(还 ...

  8. 【CodeForces】983 E. NN country 树上倍增+二维数点

    [题目]E. NN country [题意]给定n个点的树和m条链,q次询问一条链(a,b)最少被多少条给定的链覆盖.\(n,m,q \leq 2*10^5\). [算法]树上倍增+二维数点(树状数组 ...

  9. 【CodeForces】925 C.Big Secret 异或

    [题目]C.Big Secret [题意]给定数组b,求重排列b数组使其前缀异或和数组a单调递增.\(n \leq 10^5,1 \leq b_i \leq 2^{60}\). [算法]异或 为了拆位 ...

随机推荐

  1. GROUP函数

    GROUP_ID 首先我们看看官方的解释: 大意是GROUP_ID用于区分相同分组标准的分组统计结果. 解释起来比较抽象,下面我们来看看具体的案例. 例1:单一分组 SQL> select gr ...

  2. django找不到报错 ‘zsh: command not found: django-admin.py’

    >>python -m django --version >> 1.11.4 说明django安装成功,但是django-admin 报错 ‘zsh: command not ...

  3. aggregate和annotate方法使用详解与示例

    aggregate和annotate方法的使用场景 Django的aggregate和annotate方法属于高级查询方法,主要用于组合查询.当我们需要对查询集(queryset)的某些字段进行计算或 ...

  4. 灰度直方图均衡化----python实现

    直方图均衡化是使用图像直方图进行对比度调整的图像处理的方法. 该方法通常会增加许多图像的整体对比度,尤其是当图像的可用数据由接近的对比度值表示时. 通过这种调整,强度可以更好地分布在直方图上. 这允许 ...

  5. MyBaties 异常之 java.lang.UnsupportedOperationException

    sql语句 对应的接口为: 包错误的详情为: java.lang.UnsupportedOperationException 原因: resultType返回的是集合中的元素类型,而不是集合本身 SQ ...

  6. dsu+树链剖分+树分治

    dsu,对于无修改子树信息查询,并且操作支持undo的问题 暴力dfs,对于每个节点,对所有轻儿子dfs下去,然后再消除轻儿子的影响 dfs重儿子,然后dfs暴力恢复轻儿子们的影响,再把当前节点影响算 ...

  7. POJ 3469 网络流最小割

    将两个CPU分别视作源点和汇点 对于那些不在同一个CPU中的模块会产生的代价作为一条双向的容量弧 这里每个模块可以在任意一个CPU中运行,相当于寻找一个割,分割后,在S集合中的模块安装在第一个CPU中 ...

  8. 2018/2/20 Springretry,Feign,以及用通俗的语言(自认为)教会你关于Hystrix的复杂概念

    本来想昨天写的,但临时有事.弄的一晚上都没睡觉,现在头好晕,所以此笔记如果有语言措辞的组织不当,还请见谅:最后,本文可能涉及到大量专业名词,我会尽量用通俗的语句去阐述清楚它们的意思,但如果还是没看懂, ...

  9. DNS域名服务器配置

    ========================DNS域名服务器===================== 1)bind安装: yum -y install bind* ............... ...

  10. spring 数据源JNDI 基于tomcat mysql配置

    关键代码 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean&q ...