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
题意:n个点,m条边,起点s,终点t,当边的权值为0时,可以改变为任意的权值,求是否可以通过改变权值为0的边,使s到t的最短路长度为L
由于求的是最短路首先想到最短路问题,
当改变一条边的权值后,s到t的最短路也会发生变化,于是此时最短路要重新计算
共有两种情况:
1.不经过权值为0的边,此时的最短路已经<L,那么这种情况肯定是输出NO
2不经过权值为0的边,此时的最短路>=L,此时就要考虑加上这些权值为0的边,使得最短路变小,满足条件
当经过一条权值为0的边,可以先将该边赋权值为1(因为题目要求边权为正整数),求出最短距离,如果此时的最短距离<L,表示此时该边一定在最短路径中,将该边权改为L-dis[t]+1,使最短路为L
然后暴力将每一个权值为0的边加入图即可
若加入所有边后,此时的最短距离仍>L,输出NO
否则,输出YES
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int N=1e3+20;
int n,m,s,t,L,first[N],tot;
ll dis[N];
bool vis[N];
struct node
{
int v,next,flag;
ll w;
} e[N*20];
void add(int u,int v,ll w)
{
e[tot].v=v;
e[tot].w=w;
if(w==0)e[tot].flag=1;
e[tot].next=first[u];
first[u]=tot++;
}
struct point
{
point(int a,ll b)
{
x=a,val=b;
}
int x;
ll val;
bool friend operator <(point A,point B)
{
return A.val>B.val;
}
};
void dijkstra()
{
for(int i=0;i<n;i++)dis[i]=1000000000000000000;
priority_queue<point>q;
dis[s]=0;
q.push(point(s,0));
while(!q.empty())
{
point u=q.top();
q.pop();
if(dis[u.x]<u.val)continue;
for(int i=first[u.x]; ~i; i=e[i].next)
{
int v=e[i].v;
if(e[i].w==0)continue;
if(dis[v]>dis[u.x]+e[i].w)
{
dis[v]=dis[u.x]+e[i].w;
q.push(point(v,dis[v]));
}
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
mem(first,-1);
int u,v;
ll w;
cin>>n>>m>>L>>s>>t;
for(int i=0; i<m; i++)
{
cin>>u>>v>>w;
add(u,v,w);
add(v,u,w);
}
dijkstra();
if(dis[t]<L)puts("NO");//不经过权值为0的边,最短路已经<L
else//>=L的情况
{
for(int i=0;i<tot;i+=2)
{
if(!e[i].flag)continue;
e[i].w=e[i^1].w=1;
dijkstra();//暴力将每条边都加入图,求得最短路
if(dis[t]<L)//将最小距离补够L
e[i].w=e[i^1].w=L-dis[t]+1;
}
if(dis[t]>L)puts("NO");//加完所有边后距离仍>L
else//不会出现<L的情况,因为每次都补够了L
{
puts("YES");
for(int i=0;i<tot;i+=2)
printf("%d %d %lld\n",e[i^1].v,e[i].v,e[i].w);
}
}
return 0;
}