Codeforces Round #372 Complete The Graph

题目链接:点击打开链接


题意:

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


大意:给你一个无向图n(1000)个点,m条边(10000),起点s,终点t,与两点间最短路距离L。然后再告诉你每条边u,v,w为权值,如果w为0,表示其被擦去,不是真实值,求出w为0时w的真实值的一种图的可能性,使得从s到t最短路为L.


思路:首先,假设w为0的边为可变边,当这些边都大于L的时候,如果此时最短路长度还是小于L,就是不可能的。同理,当边长都为1的时候,如果最短路大于L了,也是不可能的图;于是除了这两种情况后,其他都能通过构造边长来满足最短路长度为L。

         但是,这里就有问题了,我之前的做法是把所有可变边的权值赋值为1,然后跑最短路,求出实际最短和理论最短的差值, 再加给其中一个可变边。

         然后就gg了。答案不对;

         后来才发现 原来如果存在次小路<L的话,就会导致实际最短路小于L,于是实际要跑多遍最短路,直到最短路为L为止。

代码:

#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=10001;
const long long inf =1e15+7;
struct edge
{
    int u,v;
    long long w;
    int next,flag;
}e[maxn*2];
int head[maxn];
int vis[maxn];
long long  dists[maxn];
int pre[maxn];
int n,m,t,L;
void add(int i,int j,int w)
{
    e[t].u=i;
    e[t].v=j;
    e[t].w=w;
    if(w==0)
    {
         e[t].flag=1;
         e[t].w=1e9+7;
    }
    else
        e[t].flag=0;
    e[t].next=head[i];
    head[i]=t++;
}
void spfa1(int s)
{
    queue <int> q;
    for(int i=0;i<=n;i++)
    dists[i]=inf;
    memset(vis,false,sizeof(vis));
    q.push(s);
    dists[s]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].v;
            if(dists[v]>dists[u]+e[i].w)
            {
                dists[v]=dists[u]+e[i].w;
                pre[v]=i;
                if(!vis[v])
                {
                    vis[v]=true;
                    q.push(v);
                }
            }
        }
    }
}
void judge(int s,int ee)
{
    int now=pre[ee],flag=1;
    while(e[now].u!=s)
    {
        if(e[now].flag==1)
        {
            e[now].flag=e[now^1].flag=2;
            e[now].w=e[now^1].w=L-dists[ee]+1;
            flag=0;
            break;
        }
        now=pre[e[now].u];
    }
    if(e[now].flag==1&&flag)
    {
       e[now].flag=e[now^1].flag=2;
       e[now].w=e[now^1].w=L-dists[ee]+1;
    }
    return ;
}
int main()
{
    int a,b,c,s,ee;
    scanf("%d%d%d%d%d",&n,&m,&L,&s,&ee);
    t=0;
    memset(head,-1,sizeof(head));
    while(m--)
    {
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);
        add(b,a,c);
    }
    spfa1(s);
    if(dists[ee]<L)
    {
        puts("No");
        return 0;
    }
    for(int i=0;i<t;i++)
    {
        if(e[i].flag==1)
        {
            e[i].w=1;
        }
    }
    spfa1(s);
    while(dists[ee]<L)
    {
        judge(s,ee);
        spfa1(s);
    }
    if(dists[ee]>L)
    {
        puts("No");
        return 0;
    }
    puts("YES");
    for(int i=0;i<t;i+=2)
    {
        printf("%d %d %d\n",e[i].u,e[i].v,e[i].w);
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值