NEERC17 J Journey from Petersburg to Moscow

Posted czxingchen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NEERC17 J Journey from Petersburg to Moscow相关的知识,希望对你有一定的参考价值。

CF上可以提交。   链接

依然是很妙的解法。

我们可以枚举每一个出现过的边权$L$,然后把所有边的边权减掉这个$L$,如果小于$L$就变为$0$,然后跑一遍最短路然后加上$k * L$更新答案即可。

注意$L$也可以取到$0$。

这样做就是强制让出了前$k$大的边的边权,所以能计算到所有答案。

时间复杂度$O(n^2logn)$。

Code:

技术分享图片
#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair <ll, int> pin;

const int N = 3005;
const ll inf = 0x3f3f3f3f3f3f3f3f;

int n, m, K, vCnt = 0, tot = 0, head[N];
ll eVal[N], dis[N];
bool vis[N];

struct Edge {
    int to, nxt;
    ll val;
} e[N << 1];

inline void add(int from, int to, ll val) {
    e[++tot].to = to;
    e[tot].val = val;
    e[tot].nxt = head[from];
    head[from] = tot;
}

template <typename T>
inline void read(T &X) {
    X = 0; char ch = 0; T op = 1;
    for(; ch > 9 || ch < 0; ch = getchar())
        if(ch == -) op = -1;
    for(; ch >= 0 && ch <= 9; ch = getchar())
        X = (X << 3) + (X << 1) + ch - 48;
    X *= op;
}

inline ll max(ll x, ll y) {
    return x > y ? x : y;
}

template <typename T>
inline void chkMin(T &x, T y) {
    if(y < x) x = y;
}

priority_queue <pin> Q;
void dij(ll nowL) {
    memset(dis, 0x3f, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    Q.push(pin(dis[1] = 0LL, 1));
    for(; !Q.empty(); ) {
        int x = Q.top().second; Q.pop();
        if(vis[x]) continue;
        vis[x] = 1;
        for(int i = head[x]; i; i = e[i].nxt) {
            int y = e[i].to; ll val = max(0LL, e[i].val - nowL) + dis[x];
            if(val < dis[y]) {
                dis[y] = val;
                Q.push(pin(-dis[y], y));
            }
        }
    }
}

int main() {
    read(n), read(m), read(K);
    for(int i = 1; i <= m; i++) {
        int x, y; ll v;
        read(x), read(y), read(v);
        add(x, y, v), add(y, x, v);
        eVal[++vCnt] = v; 
    }
    eVal[++vCnt] = 0LL;
    
    sort(eVal + 1, eVal + 1 + vCnt);
    vCnt = unique(eVal + 1, eVal + 1 + vCnt) - eVal - 1;
    
    ll ans = inf;
    for(int i = 1; i <= vCnt; i++) {
        dij(eVal[i]);
        chkMin(ans, dis[n] + 1LL * K * eVal[i]);
    }
    
    printf("%lld
", ans);
    return 0;
}
View Code

 

以上是关于NEERC17 J Journey from Petersburg to Moscow的主要内容,如果未能解决你的问题,请参考以下文章

2017 新疆赛区网络赛 J Our Journey of Dalian Ends 费用流

2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17)

CodeForces 721C Journey

模拟NEERC15 A Adjustment Office (2015-2016 ACM-ICPC)(Codeforces GYM 100851)

2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17)题解+补题

CodeForces 721B Journey (DP)