Codeforces 716D D. Complete The Graph

D. Complete The Graph
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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”.

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <climits>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <list>
#include <bitset>
#include <vector>
using namespace std;

#define I64d_OJ

#ifdef I64d_OJ
#define LL __int64
#endif // I64d_OJ
//#ifdef I64d_OJ
//#define LL long long
//#endif // I64d_OJ

typedef unsigned LL ULL;
typedef unsigned uint;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef pair<LL,LL> pLL;
typedef vector<int> vi;

inline LL read(){
    LL x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-f; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ls rt<<1
#define rs rt<<1|1
#define MID(a,b) (((LL)(a)+(LL)(b))>>1)
#define absx(a) ((a)<0?-(a):(a))
#define MK(a,b) make_pair(a,b)
#define PB(a) push_back(a)
#define lowbit(x) ((x)&-(x))

template< typename T >
inline void Max(T &a,T &b){if(a<b) a=b;}
template< typename T>
inline void Min(T &a,T &b){if(a>b) a=b;}

const double pi=(double)acos(-1.0);
const double eps=(double)1e-10;
const int INF= (int) 0x3f3f3f3f;
const int MOD = (int) 1e9+7;
const int MAXN =(int) 1e4+10;
const int MAXM = (int) 1e4+10;

///--------------------START-------------------------
int n,m,s,t;
LL L;
LL dist[MAXN];
//int cnt[MAXN];
//int p[MAXN];
bool vis[MAXN];
int mark[MAXN];

struct Edge{
    int u,v;
    LL w;
    int next;
}edge[MAXM<<1];
int head[MAXN];
int edgenum;

void Add_Edge(int u,int v,LL w){
    edge[edgenum].u=u;
    edge[edgenum].v=v;
    edge[edgenum].w=w;
    edge[edgenum].next=head[u];
    head[u]=edgenum++;
}

void SPFA_BFS(int s){
    queue<int> q;
    for(int i=0;i<MAXN;i++) dist[i]=INF;
    dist[s]=0;
//    memset(cnt,0,sizeof(cnt));
    memset(vis,0,sizeof(vis));

    q.push(s);
    vis[s]=1;
//    cnt[s]=1;
    while(!q.empty()){
        int u=q.front(); q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].v;
            LL w=edge[i].w;
            if(dist[u]+w<dist[v]){
                dist[v]=dist[u]+w;
                if(!vis[v]){
                    vis[v]=1;
//                    cnt[v]++;
                    q.push(v);
                }
            }
        }
    }
}

void work(){
    scanf("%d%d%I64d%d%d",&n,&m,&L,&s,&t);
    memset(head,-1,sizeof(head));
    edgenum=0;
    for(int i=0;i<m;i++){
        int u,v;
        LL w;
        scanf("%d%d%I64d",&u,&v,&w);
        if(w==0){w=1; mark[i]=1;}
        Add_Edge(u,v,w);
        Add_Edge(v,u,w);
    }
    SPFA_BFS(s);
    if(dist[t]==INF||dist[t]>L){printf("NO\n"); return;}
    LL delta=L-dist[t];
    int ok=0;
    if(dist[t]==L) ok=1;
    if(!ok){
        for(int i=0;i<m;i++){
            if(mark[i]){
                edge[i*2].w+=delta;
                edge[i*2+1].w+=delta;
                SPFA_BFS(s);
                if(dist[t]==L){ok=1; break;}
                else delta=L-dist[t];
            }
        }
    }
    if(!ok) printf("NO\n");
    else{
        printf("YES\n");
        for(int i=0;i<m;i++){
            printf("%d %d %I64d\n",edge[i*2].u,edge[i*2].v,edge[i*2].w);
        }
    }
}

///--------------------END-------------------------
int main(){
#ifdef NNever
freopen("data.in","r",stdin);
///freopen("out.txt","w",stdout);
#endif // NNever
    work();
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值