Codeforces Round #372 Complete The Graph

来源:互联网 发布:软件工程硕士 不统考 编辑:程序博客网 时间:2024/06/13 23:16

题目链接:点击打开链接


题意:

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 from0 to n - 1 andm edges between them. Each edge of the graph is weighted, each weight is apositive integer.

The next day, ZS the Coder realized that some of the weights were erased! So he wants to reassignpositive integer weight to each of the edges which weights were erased, so that the length of the shortest path between verticess and t in the resulting graph is exactlyL. 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 andvi denote the endpoints of the edge andwi denotes its weight. Ifwi is equal to0 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. Nextm 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 integersui,vi andwi, denoting an edge between verticesui andvi of weightwi. 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 betweens and t must be equal toL.

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