CodeForces - 715 B. Complete The Graph(dijkstra+暴力)

B. 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
Copy
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
Copy
2 1 123456789 0 1
0 1 0
output
YES
0 1 123456789
input
Copy
2 1 999999999 1 0
0 1 1000000000
output
NO


题意:n个点,m条边,起点s,终点t,当边的权值为0时,可以改变为任意的权值,求是否可以通过改变权值为0的边,使s到t的最短路长度为L


由于求的是最短路首先想到最短路问题,

当改变一条边的权值后,s到t的最短路也会发生变化,于是此时最短路要重新计算

共有两种情况:

1.不经过权值为0的边,此时的最短路已经

2不经过权值为0的边,此时的最短路>=L,此时就要考虑加上这些权值为0的边,使得最短路变小,满足条件

当经过一条权值为0的边,可以先将该边赋权值为1(因为题目要求边权为正整数),求出最短距离,如果此时的最短距离

然后暴力将每一个权值为0的边加入图即可

若加入所有边后,此时的最短距离仍>L,输出NO

否则,输出YES

#include
#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;iq;
    dis[s]=0;
    q.push(point(s,0));
    while(!q.empty())
    {
        point u=q.top();
        q.pop();
        if(dis[u.x]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>u>>v>>w;
        add(u,v,w);
        add(v,u,w);
    }
    dijkstra();
    if(dis[t]=L的情况
    {
       for(int i=0;iL)puts("NO");//加完所有边后距离仍>L
       else//不会出现


你可能感兴趣的:(【最短路】)