博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Codeforces】716D Complete The Graph
阅读量:5255 次
发布时间:2019-06-14

本文共 4344 字,大约阅读时间需要 14 分钟。

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 ui, vi 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
5 5 13 0 4 0 1 5 2 1 2 3 2 3 1 4 0 4 3 4
output
YES 0 1 5 2 1 2 3 2 3 1 4 8 4 3 4
input
2 1 999999999 1 0 0 1 1000000000
output
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)

// This file is made by YJinpeng,created by XuYike's black technology automatically.// Copyright (C) 2016 ChangJun High School, Inc.// I don't know what this program is.#include 
#include
#include
#include
#include
#include
#include
#include
#define IN inline#define RG register#define MOD 1000000007#define INF 1e9+1using namespace std;typedef long long LL;const int MAXN=1010;const int MAXM=20010;inline int gi() { register int w=0,q=0;register char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-')ch=getchar(); if(ch=='-')q=1,ch=getchar(); while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar(); return q?-w:w;}int tot,cnt,S[MAXM],T[MAXM];struct Dijskra{ static const int N=1010,M=(N*10)<<1; int n,t,l;int d[N],fr[N];int to[M],ne[M],be[M],W[M];bool u[N]; struct node{ int s,p; bool operator<(node a)const{return s>a.s;} }; priority_queue
q; IN void link(RG int u,RG int v,RG int w){ //if(w>l)return; this some edges didn't get to[++t]=v;ne[t]=fr[u];fr[u]=t;W[t]=w;be[t]=u; } int Dij(int begin,int end){ for(int i=0;i
l)break; for(int o=fr[x],y;y=to[o],o;o=ne[o]) if(d[x]+W[o]
<=l){ d[y]=d[x]+W[o]; q.push((node){d[y],y}); } } return d[end]; } void pri(int end){ printf("YES\n"); for(int i=1;i
cnt+1)return 0*puts("NO"); e.pri(t); return 0;}

 

转载于:https://www.cnblogs.com/YJinpeng/p/5966092.html

你可能感兴趣的文章
给你的网站404页面加上“宝贝寻亲”公益页面
查看>>
整理推荐的CSS属性书写顺序
查看>>
ServerSocket和Socket通信
查看>>
css & input type & search icon
查看>>
源代码的下载和编译读后感
查看>>
Kafka学习笔记
查看>>
Octotree Chrome安装与使用方法
查看>>
Windows 环境下基于 Redis 的 Celery 任务调度模块的实现
查看>>
趣谈Java变量的可见性问题
查看>>
C# 强制关闭当前程序进程(完全Kill掉不留痕迹)
查看>>
ssm框架之将数据库的数据导入导出为excel文件
查看>>
语音识别中的MFCC的提取原理和MATLAB实现
查看>>
验证组件FluentValidation的使用示例
查看>>
0320-学习进度条
查看>>
解决windows系统的oracle数据库不能启动ora-00119和ora-00130的问题
查看>>
ip相关问题解答
查看>>
MetaWeblog API Test
查看>>
反弹SHELL
查看>>
关闭Chrome浏览器的自动更新和升级提示
查看>>
移动、尺寸改变
查看>>