Codeforces Round #372 (Div. 2) D. Complete The Graph 图论、最短路、Dijkstra、路径、分配部分边权

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

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



Source

Codeforces Round #372 (Div. 2)


My Solution

图论、最短路、Dijkstra、路径、分配部分边权

首先去点2种不可能的情况:

1、不记录w == 0 的边的情况下,求出dis[e], 如果 dis[e] < L, 则 w == 0 的边无论怎样分配权值,src 到 e 的最短路 都是这个 dis[e], ans = “NO";

2、把w == 0 的边赋值为1,然后把这些 w == 0的边记录下来,跑一边 Dijkstra,如果 dis[e] > L, 则 由于 w > 0 即 w >= 1, 则无论怎样dis[e] 都大于 L, ans = ”NO“;

然后其它时候都是有答案的

也是把 w == 0的边都赋值为1,然后跑一遍 Dijkstra,得到最短路 dis[e],但 dis[e] == L,则ok, 否则 对于 这条最短路径上任意一个赋值非1的variabel edgae += L - dise[e],然后再跑 Dijkstra,如此循环,由于每一个 varible edge最多在 variabel edgae += L - dise[e]的一次, 所以这样的循环最多执行n次就会得到答案

复杂度 O(nmlogn)


#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> ii;
const int maxn = 1e6 + 8;
const LL INF = 9e18;
vector<ii> G[maxn];
LL dis[maxn], n, src, L;

//O(nlogn)
void dijkstra()
{
    for(int i = 0; i < n; i++) dis[i] = INF;
    dis[src] = 0;
	priority_queue<ii, vector<ii>, greater<ii> > pq;
	pq.push(ii(0, src));
	LL u, d, sz, v, w;
	while(!pq.empty()){
        u = pq.top().second; d = pq.top().first; pq.pop();
        sz = G[u].size();
		for(int i = 0; i < sz; i++){
			v = G[u][i].first; w = G[u][i].second;
			if(d + w < dis[v]){
				dis[v] = d + w;
				pq.push(ii(dis[v], v));
			}
		}
	}
}

vector<ii> g2[maxn];
LL dis2[maxn];

//O(nlogn)
void dijkstra2()
{
    for(int i = 0; i < n; i++) dis2[i] = INF;
    dis2[src] = 0;
	priority_queue<ii, vector<ii>, greater<ii> > pq;
	pq.push(ii(0, src));
	LL u, d, sz, v, w;
	while(!pq.empty()){
        u = pq.top().second; d = pq.top().first; pq.pop();
        sz = g2[u].size();
		for(int i = 0; i < sz; i++){
			v = g2[u][i].first; w = g2[u][i].second;
			if(d + w < dis2[v]){
				dis2[v] = d + w;
				pq.push(ii(dis2[v], v));
			}
		}
	}
}

map<ii, int> variable, haveprint;
void update(const LL& x)
{
	priority_queue<ii, vector<ii>, greater<ii> > pq;
	pq.push(ii(0, src));
	LL u, d, sz, v, w;
	while(!pq.empty()){
        u = pq.top().second; d = pq.top().first; pq.pop();
        sz = g2[u].size();
		for(int i = 0; i < sz; i++){
			v = g2[u][i].first; w = g2[u][i].second;
			if(d + w == dis2[v]){
				if(w == 1 && variable[ii(u, v)] == 1){ //w == 1 表示之前没有被更改过
                    g2[u][i].second += x;
                    int sz2 = g2[v].size();                            //无向边是双向读入,所以2个方向的边权都要更新
                    for(int k = 0; k < sz2; k++){
                        if(g2[v][k].first == u){
                            g2[v][k].second += x;
                            break;
                        }
                    }
                    return;
				}
				else{
                    pq.push(ii(dis2[v], v));
				}
			}
		}
	}
}


int main()
{
    #ifdef LOCAL
    freopen("d.txt", "r", stdin);
    //freopen("o.txt", "w", stdout);
    int T = 1;
    while(T--){
    #endif // LOCAL
    ios::sync_with_stdio(false); cin.tie(0);

    LL m, e, u, v, w;
    cin >> n >> m >> L >> src >> e;
    while(m--){
        cin >> u >> v >> w;
        if(w != 0){
            G[u].push_back(ii(v, w));
            G[v].push_back(ii(u, w));
            g2[u].push_back(ii(v, w));
            g2[v].push_back(ii(u, w));
        }
        else{
            variable[ii(u, v)] = 1;
            variable[ii(v, u)] = 1;
            g2[u].push_back(ii(v, 1));
            g2[v].push_back(ii(u, 1));
        }

    }

    dijkstra();
    if(dis[e] < L){
        cout << "NO" << endl;
    }
    else{
        dijkstra2();
        if(dis2[e] > L){
            cout << "NO" << endl;
        }
        else{

            while(dis2[e] != L){
                update(L - dis2[e]);
                dijkstra2();
            }

            cout << "YES" << endl;
            int sz;
            for(int i = 0; i < n; i++){
                sz = g2[i].size();
                for(int j = 0; j < sz; j++){
                    if(haveprint[ii(i, g2[i][j].first)] == 0 && g2[i][j].second != 0){
                        cout << i << " " << g2[i][j].first << " " << g2[i][j].second << "\n";
                        haveprint[ii(i, g2[i][j].first)] = 1;
                        haveprint[ii(g2[i][j].first, i)] = 1;
                    }
                }
            }
        }
    }



    #ifdef LOCAL
    cout << endl;
    }
    #endif // LOCAL
    return 0;
}

  Thank you!

                                                                                                                                               ------from ProLights

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值