Codeforces Round #372 (Div. 1) D. Complete The Graph(最短路,想法题,好题)

时间:2023-02-03 09:25:43

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

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

Codeforces Round #372 (Div. 1) D. Complete The Graph(最短路,想法题,好题)

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



题意:

给出n个点(编号0-n-1),m条边,最短路长度l,起点s,终点t。

所有边的权值都为整数,且为双向边,不过有些边权值被抹去了,称为特殊边,输入时特殊边权值为0,问是否能够找到一种分配,将所以特殊边权值固定,使得s到t的最短路长度为l。

若有解输出YES,并按输入顺序输出所有边。

否则输出NO。


题解:

首先,有两种情况无解:

1.先将所有特殊边都看成不存在,即权值为INF,跑一边最短路后如果d[t]<l,说明无解。

2.由于所有边权权值为正,那么所有特殊边权值至少为1,将所以特殊边权值都赋值为1,若d[t]>l,说明无解。


其他情况都是有解,那么如何找到解呢?

首先可以将所以特殊边都除去(将边权赋值为INF),此时d[t]必定大于l,然后将边一条条加进去,将边权赋值为1,跑完最短路后若d[t]<l,说明该边为“关键边”,那么此时就将这条边权值加上l-d[t],这就是解了。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
const int MAXN=1000+100;
const int MAXM=10000+100;
const ll INF=0x3fffffffffffff;
int head[MAXN];
ll d[MAXN];
struct node
{
int from,to,next;
ll w;
}edge[MAXM*2];
int tol=0;
int n;
void add(int u,int v,ll w)
{
edge[tol].from=u,edge[tol].to=v,edge[tol].w=w,edge[tol].next=head[u],head[u]=tol++;
edge[tol].from=v,edge[tol].to=u,edge[tol].w=w,edge[tol].next=head[v],head[v]=tol++;
}
void init()
{
tol=0;
memset(head,-1,sizeof(head));
}
void spfa(int s,bool flag)
{
int q[MAXN],inq[MAXN];
memset(inq,0,sizeof(inq));
for(int i=0;i<n;i++) d[i]=INF;
int front=0,rear=0;
q[rear++]=s,d[s]=0;
inq[s]=1;
while(front!=rear)
{
int u=q[front++];
inq[u]=0;
if(front>=MAXN) front=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
ll w=edge[i].w;
if(flag&&w==0) continue;
if(d[v]>d[u]+w)
{
d[v]=d[u]+w;
if(inq[v]) continue;
q[rear++]=v,inq[v]=1;
if(rear>=MAXN) rear=0;
}
}
}
}
int main()
{
int m,s,t;
ll l;
while(~scanf("%d%d%I64d%d%d",&n,&m,&l,&s,&t))
{
init();
VI e;
while(m--)
{
int u,v;
ll w;
scanf("%d%d%I64d",&u,&v,&w);
add(u,v,w);
if(!w) e.push_back(tol-2);
}
spfa(s,true);
if(d[t]<l)
{
puts("NO");
continue;
}
else if(d[t]==l)
{
puts("YES");
for(int i=0;i<tol;i+=2)
{
printf("%d %d ",edge[i].from,edge[i].to);
if(!edge[i].w) printf("%I64d\n",l);//
else printf("%I64d\n",edge[i].w);
}
continue;
}
for(int i=0;i<e.size();i++)
{
int ee=e[i];
edge[ee].w=edge[ee^1].w=1;
}
spfa(s,false);
if(d[t]>l)
{
puts("NO");
continue;
}
puts("YES");
for(int i=0;i<e.size();i++)
{
int ee=e[i];
edge[ee].w=edge[ee^1].w=l;
}
for(int i=0;i<e.size();i++)
{
int ee=e[i];
edge[ee].w=edge[ee^1].w=1;
spfa(s,false);
if(d[t]<=l)
{
edge[ee].w+=l-d[t];
edge[ee^1].w+=l-d[t];
break;
}
}
for(int i=0;i<tol;i+=2)
printf("%d %d %I64d\n",edge[i].from,edge[i].to,edge[i].w);
}
return 0;
}