Complete The Graph CodeForces - 715B 最短路詳解

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 0then 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 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
Copy
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
Copy
2 1 123456789 0 1
0 1 0
output
YES
0 1 123456789
input
Copy
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".

題意:給你n個點,和m條雙向邊,每條邊的長度有它的長度(正整數),若此邊的長度爲0,則是要你修改此長度,改爲成正整數,使得從出發點到終點,最短距離等於L,。若修改可以滿足題意,輸出所有邊的信息。,若無法使得最短路距離爲L,則輸出NO。

思路:先不用那些邊長爲0的邊 求出最短路,若此距離小於L,那麼增大邊長爲0的邊,也不會使得最短距離變大。此種情況輸出NO;   若此距離等於L,那麼直接輸出這些邊的信息,若邊長爲0,則輸出1e18即可。

若此距離大於L,那麼就要用上邊長爲0的邊,使得最短路變小了;因爲每條邊爲正整數,所以把那些邊長爲0的邊全部變成1;  然後求出最短路,若大於L則輸出NO,若等於L則輸出YES,若小於L,用F數組記錄最短路的路徑,看這個最短路離L還差多少,讓這條最短路徑上任意一個可以改變的值(以前是0的邊,可以標記一下)增加上這個差值。然後讓這條路徑以外的可變邊全部變爲1e18,使得只留下一條最短路徑爲L的路。  
然後重新用最短路算法跑一邊,找一下改變後的圖的最短路還是不是L,若是L則輸出YES,若小於L,說明剛纔最短路增長後,最短路變成了另一條,  那麼重複以上黑體字操作。  .......直到除了此條最短路爲L以外,再沒有其路能小於L的了,然後輸出答案即可。

思路當天沒想出來,結果晚上回去的路上,突然就想到了多次找最短路,補全L後,修改其他邊爲1e18。直到找到最短路爲L爲止。  用這個思路,代碼一遍就過了。終於A掉了,美滋滋!!

代碼:(因本人代碼水平有限,寫的挺長,代碼用時:2000ms

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<algorithm>
#define inf 0x3f3f3f3f
#include <algorithm>
#define N 10005
#define LL long long
#define mem(a,b) memset(a,b,sizeof(a));
using namespace std;
LL ansu[N],ansv[N];
LL answ[N];//存邊的信息
struct node
{
    LL u,v,nex,id;
    LL w,flag;
} e[400500];
LL head[N],f[N],vis[N];
LL cnt,S,T,n,m;
LL dis[N],L;
void add(LL u,LL v,LL w,LL id)
{
    e[cnt].id=id;//哪條邊
    e[cnt].u=u;
    e[cnt].v=v;
    e[cnt].w=w;
    if(w==0) e[cnt].flag=0;//標記,邊長爲0,標記爲0
    else e[cnt].flag=1;
    e[cnt].nex=head[u];
    head[u]=cnt++;
}
void init()
{
    mem(head,-1);
    cnt=0;
}
queue<LL>q;
map<LL,LL>ma;
void spfa(LL flag)
{
    mem(f,-1);
    mem(vis,0);
    memset(dis,0x3f,sizeof dis);//初始化
    dis[S]=0LL;//dis[i]表示i到出發點S的最短距離
    vis[S]=1;//標記是否已經加入隊列
    while(!q.empty())
        q.pop();
    q.push(S);
    while(!q.empty())
    {
        LL u=q.front();
        q.pop();
        for(LL i=head[u]; ~i; i=e[i].nex)
        {
            LL v=e[i].v;
            LL w=e[i].w;
            if(flag&&!w) continue;//flag爲1的話,算最短路不帶邊長爲0的邊
            if(dis[v]<=dis[u]+w) continue;
            dis[v]=dis[u]+w;//更新v到S的最短距離
            if(!flag) f[v]=i;//更新dis[v]後,更新f[v],f[v]存u到v的這條路的編號i,通過這個來找到最短路路徑。
            if(vis[v]) continue;
            vis[v]=1;
            q.push(v);
        }
        vis[u]=0;
    }
}
int main()
{
    init();
    scanf("%lld%lld%lld%lld%lld",&n,&m,&L,&S,&T);
    for(LL i=0; i<m; i++)
    {
        scanf("%lld%lld%lld",&ansu[i],&ansv[i],&answ[i]);
        add(ansu[i],ansv[i],answ[i],i);
        add(ansv[i],ansu[i],answ[i],i);
    }
    spfa(1);
    if(dis[T]<L)//計算出最短路(不帶0的邊),若太小,改變邊長爲0的邊,也無法改變這條最短路的值。
    {
        printf("NO\n");
        return 0;
    }
    else if(dis[T]==L)//找到答案
    {
        printf("YES\n");
        for(LL i=0; i<m; i++)
            printf("%lld %lld %lld\n",ansu[i],ansv[i],answ[i]==0LL?L+10:answ[i]);
        return 0;
    }
    for(int i=0;i<cnt;i++)//讓所有的邊長爲0的邊變成1(正整數)中的最小值
        if(!e[i].flag)
            answ[e[i].id]=e[i].w=1;
    while(1)
    {
        spfa(0);//求出最短路
        if(dis[T]>L)
        {
            printf("NO\n");//第一次求出此值,這是整個圖的最短路,因爲邊長爲零的邊一定至少是1,第一次就沒有大於L,那麼以後也不可能再大於L。
            return 0;
        }
        else if(dis[T]==L)
        {
            printf("YES\n");
            for(LL i=0; i<m; i++)
                printf("%lld %lld %lld\n",ansu[i],ansv[i],answ[i]);
            return 0;
        }
        else
        {
            LL u=T;
            ma.clear();
            LL sum=L-dis[T];
            while(u!=S)
            {
                if(!e[f[u]].flag)
                {
                    answ[e[f[u]].id]=e[f[u]].w=e[f[u]].w+sum;//將sum(差值)添加到第一個可修改邊上。
                    sum=0;
                    ma[e[f[u]].id]=1;//標記此最短路上可修改的邊的id
                }
                u=e[f[u]].u;//f[u]爲找到上一個節點的編號。
            }
            for(int i=0;i<cnt;i++)//此條最短路以外的所有可修改的邊全部修改成1e18,
                if(!e[i].flag&&!ma[e[i].id])
                    answ[e[i].id]=e[i].w=L+10;
        }
    }
}






發佈了156 篇原創文章 · 獲贊 56 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章