题目连接:http://codeforces.com/contest/716/problem/D
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?
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.
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.
5 5 13 0 4 0 1 5 2 1 2 3 2 3 1 4 0 4 3 4
YES 0 1 5 2 1 2 3 2 3 1 4 8 4 3 4
2 1 123456789 0 1 0 1 0
YES 0 1 123456789
2 1 999999999 1 0 0 1 1000000000
NO
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".
题目大意:
在一个无向图里,边的初始权值>=0,若权值==0,则需要把它变为一个正整数(不超过1e18),现在问你有没有一种方法,
使图中的边权值都变为正整数的时候,从 S 到 T 的最短路恰好等于 L。
若没有输出 "NO",否则输出 "YES",同时输出新图中的所有边权值。
解题思路:首先把权值为0的边变成1,走一遍最短路,若最短路>L,则没有符合条件的。
同样在0边变成无穷大时最短路<L,也没有符合条件。
然后接下来可以把所有的0边加进去,从1到无穷大的长度,进行二分,不断对0边进行调整,直到找到符合条件为止。
/* ***********************************************
┆ ┏┓ ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃ ┃ ┆
┆┃ ━ ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃ ┃ ┆
┆┃ ┻ ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆ ┃ 勒 ┃ ┆
┆ ┃ 戈 ┗━━━┓ ┆
┆ ┃ 壁 ┣┓┆
┆ ┃ 的草泥马 ┏┛┆
┆ ┗┓┓┏━┳┓┏┛ ┆
┆ ┃┫┫ ┃┫┫ ┆
┆ ┗┻┛ ┗┻┛ ┆
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <bitset>
using namespace std;
#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define all(a) a.begin(), a.end()
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
const int N = 1000500;
struct Edge
{
int to,nx,w;
}edge[N*2];
int cnt,head[N];
void addedge(int u,int v,int w)
{
edge[cnt] = Edge{v,head[u],w};
head[u] = cnt++;
}
int s,e,L,d[N],vis[N];
int n,m;
vi p;
int spfa()
{
for(int i=0;i<n;i++)
{
d[i] = inf_add;
vis[i] = 0;
}
d[s] = 0;
queue<int>q;
q.push(s);
while(!q.empty())
{
int u = q.front(); q.pop();
for(int i=head[u];~i;i=edge[i].nx)
{
int v = edge[i].to;
vis[v] = 0;
if(d[v]>d[u]+edge[i].w)
{
d[v] = d[u] + edge[i].w;
if(!vis[v])
{
vis[v] = 1;
q.push(v);
}
}
}
}
return d[e];
}
int check(LL sum)
{
for(int x:p)
{
edge[x].w = edge[x^1].w = 1 + min(sum,1000000000LL);
sum -= edge[x].w - 1;
}
return spfa();
}
int num[N];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
memset(head,-1,sizeof head);
scanf("%d%d%d%d%d",&n,&m,&L,&s,&e);
for(int i=1;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
if(!w)p.pb(cnt);
num[cnt] = u;
addedge(u,v,w);
addedge(v,u,w);
}
LL l=0,r=p.size() * (1e9);
if(check(l)>L || check(r)<L) return 0*printf("NO\n");
while(l<=r)
{
LL mid = (l+r) >> 1;
int res = check(mid);
if(res<L) l = mid + 1;
else if(res>L) r = mid - 1;
else break;
}
printf("YES\n");
for(int i=0;i<2*m;i+=2)
{
printf("%d %d %d\n",num[i],edge[i].to,edge[i].w);
}
return 0;
}