codeforces 716D. Complete The Graph 堆优化dij+暴力

D. 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
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 :

codeforces 716D. Complete The Graph 堆优化dij+暴力_第1张图片

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条边,起点为s,终点为t的一个无向图。图中边权为0的边表示表示边权待定,现在要求改变所有这些边的边权(必须为正值),使得s到t最短路为L。

解法:首先把边权为0的边用vector存起来,其他该怎么建怎么建,然后跑一遍dij,如果最短路小于L,那么其他的0边无论怎么变化,最短路不会变化,这时输出NO,如果等于L,直接输出这个图,vector里面的0边搞成INF输出就行了。

最短路大于L的情况,这时就需要添加0边来看是否能找到最短路等于L的方案。具体的实现方法是把vector存的边一条一条的添加进原图中,添加的时候边权设为1。添加一条跑一次dij,如果添加到某条边使得最短路<=L,把最后添加进的这条边的权值加上L-dis[t],然后记录下这条边在vector的位置,输出这个图,剩余的0边权值改为INF输出即可。如果添加了所有边进去还是找不到最短路<=L的方案,那么就不存在方案,输出NO。

看着点不多,没用堆优化,t了,还在想哪里错了,简直zz。

CODE

#include 
using namespace std;
typedef long long LL;
const LL INF = 1e15;
const int N = 1000+10;
struct node{
    int u,v,next;
    LL w;
}E[N*30];       ///原图的边

int n,m,s,t,top;
LL L;
int head[N];    ///邻接表头结点
LL dis[N];      ///dij的距离
bool vis[N];    ///dij的标记
vector V; ///存0边

void add(int u,int v,LL w) ///加边
{
    E[top].u = u;
    E[top].v = v;
    E[top].w = w;
    E[top].next = head[u];
    head[u] = top++;
}

struct Node{     ///堆优化dij用的结构体
    int id;
    LL d;
    Node(){}
    Node(int id,int d):
        id(id),d(d){}
    bool friend operator < (Node a,Node b){
        return a.d > b.d;
    }
};

void dij()      ///堆优化dij
{
    for(int i = 0;i < N;i++) dis[i] = INF,vis[i] = false;
    dis[s] = 0;
    priority_queue q;
    q.push(Node(s,0));
    while(!q.empty()){
        Node u = q.top();
        q.pop();
        if(vis[u.id]) continue;
        vis[u.id] = true;
        for(int i = head[u.id];i != -1;i = E[i].next){
            int v = E[i].v;
            if(!vis[v] && dis[v] > dis[u.id]+E[i].w){
                dis[v] = dis[u.id]+E[i].w;
                q.push(Node(v,dis[v]));
            }
        }
    }
}

int main(void)
{
    memset(head,-1,sizeof head);
    scanf("%d%d%I64d%d%d",&n,&m,&L,&s,&t);
    for(int i = 1;i <= m;i++){
        int u,v;
        LL w;
        scanf("%d%d%I64d",&u,&v,&w);
        if(w != 0){
            add(u,v,w);
            add(v,u,w);
        }
        else{
            node edge;
            edge.u = u,edge.v = v,edge.w = w,edge.next = -1;
            V.push_back(edge);   ///0边保存到结构体
        }
    }
    dij();
    if(dis[t] < L){       ///原图最短路小于L,无解
        puts("NO");
        return 0;
    }
    else if(dis[t] == L){ ///等于L直接输出就行了
        puts("YES");
        for(int i = 0;i < top;i += 2){
            printf("%d %d %I64d\n",E[i].u,E[i].v,E[i].w);
        }
        for(int i = 0;i < V.size();i++){
            printf("%d %d %I64d\n",V[i].u,V[i].v,INF);
        }
        return 0;
    }
    int pos = -1;
    for(int i = 0;i < V.size();i++){
        add(V[i].u,V[i].v,1);
        add(V[i].v,V[i].u,1);
        dij();
        if(dis[t] <= L){
            E[top-2].w += L-dis[t];
            E[top-1].w += L-dis[t];
            pos = i;
            break;
        }
    }
    if(pos == -1){
        puts("NO");
        return 0;
    }
    puts("YES");
    for(int i = 0;i < top;i += 2){
        printf("%d %d %I64d\n",E[i].u,E[i].v,E[i].w);
    }
    for(int i = pos+1;i < V.size();i++){
        printf("%d %d %I64d\n",V[i].u,V[i].v,INF);
    }
    return 0;
}




你可能感兴趣的:(图论)