codeforces 716D. 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 integerweight to each of the edges which weights were erased, so that the length of the shortest path between vertices sand 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 wiis 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 anypositive 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 toL.

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 0to 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 with123456789.

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就代表这条边是未知的),题目告诉我们起点s、终点t以及起点和终点之间的最短路l,问给这些权值未知的点赋值之后st之间的最短路是不是等于l。


思路:

先假设权值未知的边不存在,求出此时的最短路d1,如果d1<l,那肯定不存在;如果d1=l,那就将权值未知的点赋值为极大值,直接输出。

接着,将权值未知的边赋值为1,找到此时的最短路d2,如果d2>l,也肯定是不存在的;如果d2=l,直接输出;如果d2<l,那就将最短路径上曾今权值未知的边的其中一条赋值为【权值+l-d2】

循环判断此时的最短路d是不是等于l,如果不等于l,那就继续将最短路径上曾今权值未知的边的其中一条赋值为【权值+l-d】,直到最短路径跟l相等。

#include <bits/stdc++.h>
using namespace std;
#define ll __int64

const int MAXN=1010; 
const int N=1e5+10;
const ll INF=1e18; 
struct node 
{  
	int v;
	ll cost;  
	node(int vi=0,ll ci=0):v(vi),cost(ci) {} 
}; 
vector<node>vec[2][MAXN]; 
//vec[0]包含权值为0的情况,vec[1]里面不包含权值为0的情况 
void addedge(int id, int u,int v,ll w) 
{  
	vec[id][u].push_back(node(v,w));
} 
int vis[MAXN];
ll dist[MAXN]; 
int L[N], R[N];
ll V[MAXN][MAXN];
int fa[N];

bool spfa(int id, int start,int n) 
{  
	memset(vis, 0, sizeof(vis));  
	for(int i=0; i<n; i++) dist[i]=INF, fa[i] = i;  
	vis[start]=1, dist[start]=0;  
	queue<int>que;  
	while(!que.empty()) que.pop();  
	que.push(start);    
	while(!que.empty())  
	{   
		int u=que.front();   
		que.pop();   
		vis[u]=0;   
		for(int i=0; i<vec[id][u].size(); i++)   
		{    
			int v = vec[id][u][i].v; 
			ll w = V[u][v];
			if(dist[v]>dist[u]+abs(w))    
			{     
			    dist[v]=dist[u]+abs(w);  
			    fa[v] = u;   
				if(!vis[v])     
				{      
					vis[v]=1;      
					que.push(v);      
				}    
			}   
		}  
	}  
} 

//将不在最短路径上并且权值一开始未知的边赋值 
void FUZHI(int n, int m, int s, int t, int l){
	int x = t, flag = 0;
	while(1){
		int y = fa[x];
		if(V[x][y] == -1){
			V[x][y] = V[y][x] = -2;
		}
		if(y == fa[y]) break; 
		x = y;
	}
	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++){
			if(V[i][j] == -1) V[i][j] = INF;
		}
	}
	x = t;
	while(1){
		int y = fa[x];
		if(V[x][y] == -2){
			V[x][y] = V[y][x] = -1;
		}
		if(y == fa[y]) break; 
		x = y;
	}
}

bool solve(int n, int s, int t, int l){
	spfa(0, s, n);
	int x = t, flag=0;
	if(dist[t] == l) return true;
	while(1){
		int y = fa[x];
		if(!flag && V[x][y]<0){
			ll w = abs(V[x][y]);
			V[x][y] = V[y][x] = w+l-dist[t];
			flag=1;
		}
		if(y == fa[y]) break;
		x = y;
	}
	
	return false;
}

int main(){
	int n, m, l, s, t;
	scanf("%d%d%d%d%d", &n, &m, &l, &s, &t);
	for(int i=0; i<m; i++){
		int u, v;
		ll w;
		scanf("%d%d%I64d", &u, &v, &w);
		if(w){
			addedge(1, u, v, w);
			addedge(1, v, u, w);
		}
		if(w==0) w=-1;
		addedge(0, u, v, w);
		addedge(0, v, u, w);
		L[i] = u, R[i] = v;
		V[u][v] = V[v][u] = w;
	}
	
	spfa(1, s, n);
	if(dist[t]<l){
		printf("NO\n");
		return 0;
	}
	if(dist[t] == l){
		printf("YES\n");
		for(int i=0; i<m; i++){
			int u = L[i], v = R[i];
			ll w = V[u][v];
			if(w==-1) w = INF;
			printf("%d %d %I64d\n", u, v, w);
		}
		return 0;
	}
	
	spfa(0, s, n);
	FUZHI(n, m, s, t, l);
	if(dist[t]>l){
		printf("NO\n");
		return 0;
	}
	
	solve(n, s, t, l);
	while(!solve(n, s, t, l)) solve(n, s, t, l);
	
	printf("YES\n");
	for(int i=0; i<m; i++){
		int u = L[i], v = R[i];
		ll w = V[u][v];
		if(w<0) w = -w;
		printf("%d %d %I64d\n", u, v, w);
	}
	return 0;
} 












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值