Complete The Graph
Time Limit: 4000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u
Submit
Status
Description
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.
Sample Input
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
Hint
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”.
Source
Codeforces Round #372 (Div. 2)
看了网上的题解做的。。感觉很暴利。就是把每一条不确定的边都去试一下
#include "cstring"
#include "cstdio"
#include "string.h"
#include "iostream"
#include "queue"
using namespace std;
int head[20005];
long long dis[20005];
int len=0;
int n,m,l,s,t;
bool mark[20005];
struct edge
{
int to,next,u,v;
long long weight;
}edge[20005];
void addEdge(int u,int v,long long w)
{
edge[len].u=u;
edge[len].v=v;
edge[len].to=v;
edge[len].next=head[u];
edge[len].weight=w;
head[u]=len;
len++;
}
void spfa()
{
queue <int> list;
list.push(s);
bool vis[1005];
memset(vis,0,sizeof(vis));
memset(dis,0x3f3f3f3f,sizeof(dis));
dis[s]=0;
vis[s]=1;
while(!list.empty())
{
int now=list.front();
list.pop();
vis[now]=0;
for(int i=head[now];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
long long w=edge[i].weight;
if(dis[now]+w<dis[to])
{
dis[to]=dis[now]+w;
if(vis[to]==0)
{
vis[to]=1;
list.push(to);
}
}
}
}
}
void gao()
{
spfa();
if(dis[t]==0x3f3f3f||dis[t]>l)
{
printf("NO\n");
return;
}
long long delta=l-dis[t];
int flag=0;
if(dis[t]==l)
flag=1;
if(flag==0)
{
for(int i=0;i<m;i++)
{
if(mark[i])
{
edge[i*2].weight+=delta;
edge[i*2+1].weight+=delta;
spfa();
if(dis[t]==l)
{
flag=1;
break;
}
else
delta=l-dis[t];
}
}
}
if(flag==0)
printf("NO\n");
else
{
printf("YES\n");
for(int i=0;i<m;i++)
{
printf("%d %d %lld\n",edge[i*2].u,edge[i*2].v,edge[i*2].weight);
}
}
}
int main()
{
while(~scanf("%d%d%d%d%d",&n,&m,&l,&s,&t))
{
memset(head,-1,sizeof(head));
memset(mark,0,sizeof(mark));
len=0;
for(int i=0;i<m;i++)
{
int u,v;
long long w;
scanf("%d%d%lld",&u,&v,&w);
if(w==0)
{
w=1;
mark[i]=1;
}
addEdge(u, v, w);
addEdge(v, u, w);
}
gao();
}
}