您的位置:首页 > 其它

Codeforces 716D Complete The Graph【思维+最短路】

2017-10-18 13:07 537 查看
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 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 :



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个点,M条无向边,在输入的时候,对于边权值为0的边,表示这条边的边权值暂时不知道,问我们能否给这些权值为0的边赋值(必须大于0),使得从s到t的最短路的权值为L、

思路:

我们一开始设定所有权值为0的边的权值为INF,此时跑一次单源最短路,对于此时从s到t的最短路有三种情况:

①dist>l 可能存在解。

②dist==l,此时将所有边权为0的边就赋值为INF就行,直接输出。

③dist<l 不可能存在解。

对于满足①的情况下,我们考虑将边权为0的边,设定为边权为1.

我们每一次将一条边的权值从INF改成1,如果此时这条边改完权值之后在跑SPFA的dist:

①dist>l 可能存在解。但是需要我们继续将边权为INF的改成1才可能有解,如果已经所有边的权值都是1了,那么无解。

②dist==l,此时将所有边权为0的边就赋值为INF就行,直接输出。

③dist<l ,有解,将当前改权值的这条边的权值设定为1+L-dist即可,输出。

过程维护一下暴力去跑就行。

Ac代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
#define ll __int64
struct node
{
ll from,to,w,next,flag;
}e[1500000];
ll n,m,L,ss,tt;
ll cont;
ll head[150000];
void add(ll from,ll to,ll w,ll flag)
{
e[cont].from=from;
e[cont].to=to;
e[cont].w=w;
e[cont].flag=flag;
e[cont].next=head[from];
head[from]=cont++;
}
ll dist[1050][1050];
ll vis[1050];
void SPFA(ll ss)
{
queue<ll>s;
for(ll i=0;i<=n;i++)dist[ss][i]=1000000000000000ll;
dist[ss][ss]=0;memset(vis,0,sizeof(vis));
s.push(ss);
while(!s.empty())
{
ll u=s.front();
vis[u]=0;
s.pop();
for(ll i=head[u];i!=-1;i=e[i].next)
{
ll v=e[i].to;
ll w=e[i].w;
if(dist[ss][v]>dist[ss][u]+w)
{
dist[ss][v]=dist[ss][u]+w;
if(vis[v]==0)
{
vis[v]=1;
s.push(v);
}
}
}
}
}
ll Slove(ll mid)
{
for(ll i=0;i<cont;i++)
{
if(e[i].flag==1)
{
e[i].w=mid;
}
}
SPFA(ss);
return dist[ss][tt];
}
int main()
{
while(~scanf("%I64d%I64d%I64d%I64d%I64d",&n,&m,&L,&ss,&tt))
{
cont=0;
memset(head,-1,sizeof(head));
for(ll i=1;i<=m;i++)
{
ll x,y,w;scanf("%I64d%I64d%I64d",&x,&y,&w);
if(w==0)add(x,y,1000000000000000ll,1ll),add(y,x,1000000000000000ll,1ll);
else add(x,y,w,0),add(y,x,w,0);
}
SPFA(ss);
if(dist[ss][tt]>L)
{
int ok=0;
for(int i=0;i<cont;i+=2)
{
if(e[i].flag==1)
{
e[i].w=1;
e[i+1].w=1;
SPFA(ss);
if(dist[ss][tt]<=L)
{
ok=1;
e[i].w=1+L-dist[ss][tt];
e[i+1].w=1+L-dist[ss][tt];
break;
}
}
}
if(ok==1)
{
printf("YES\n");
for(int i=0;i<cont;i+=2)
{
printf("%I64d %I64d %I64d\n",e[i].from,e[i].to,e[i].w);
}
}
else printf("NO\n");
}
else if(dist[ss][tt]==L)
{
printf("YES\n");
for(int i=0;i<cont;i+=2)
{
printf("%I64d %I64d %I64d\n",e[i].from,e[i].to,e[i].w);
}
}
else printf("NO\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces 716D