Luogu 2939 [USACO09FEB]改造路Revamping Trails && Luogu 4568 [JLOI2011]飞行路线

双倍经验

写这两题之前被大佬剧透了呜呜呜。

分层图+最短路。

因为有$k$次机会能够把路径的费用变为$0$,我们可以建$k + 1$层图,对于每一层图我们把原来的边权和双向边连到上面去,而对于层与层之间的连接,对于每一条边,我们连上从下层到上层的有向边,边权为$0$。

这样子其实保证了它并不会向下走,也就是说一定在不断消耗着$k$次机会,对应了使用不超过$k$次机会,这样子的话我们最后只要求出第一层的$st$到第$k + 1$层的$ed$之间的最短路就是答案了。

我使用的是堆优化dijkstra。

时间复杂度$O(nlogn)$,这里的$n$应是$nk$级别的。

Code:

#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
typedef pair <int, int> pin;

const int N = 4e5 + 5;
const int M = 3e6 + 5;

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

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

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

inline void read(int &X) {
    X = 0; char ch = 0; int 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 int id(int depth, int now) {
    return n * (depth - 1) + now;
}

priority_queue <pin> Q;
void dij(int st) {
    memset(dis, 0x3f, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    Q.push(pin(dis[st] = 0, st));
    
    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;
            if(dis[y] > dis[x] + e[i].val) {
                dis[y] = dis[x] + e[i].val;
                Q.push(pin(-dis[y], y));
            }
        }
    }
}

int main() {
//    freopen("testdata.in", "r", stdin);
    
    read(n), read(m), read(K);
    
    int st, ed; st = 1, ed = n;
    st = id(1, st), ed = id(K + 1, ed);
    
    for(int x, y, v, i = 1; i <= m; i++) {
        read(x), read(y), read(v);
        for(int j = 1; j <= K + 1; j++)
            add(id(j, x), id(j, y), v), add(id(j, y), id(j, x), v);
        for(int j = 1; j <= K; j++)
            add(id(j, x), id(j + 1, y), 0), add(id(j, y), id(j + 1, x), 0);
    }
    
    dij(st);
    
    printf("%d\n", dis[ed]);
    return 0;
}
Luogu 2939
#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
typedef pair <int, int> pin;

const int N = 2e5 + 5;
const int M = 3e6 + 5;

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

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

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

inline void read(int &X) {
    X = 0; char ch = 0; int 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 int id(int depth, int now) {
    return n * (depth - 1) + now;
}

priority_queue <pin> Q;
void dij(int st) {
    memset(dis, 0x3f, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    Q.push(pin(dis[st] = 0, st));
    
    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;
            if(dis[y] > dis[x] + e[i].val) {
                dis[y] = dis[x] + e[i].val;
                Q.push(pin(-dis[y], y));
            }
        }
    }
}

int main() {
    read(n), read(m), read(K);
    
    int st, ed; read(st), read(ed);
    st = id(1, st + 1), ed = id(K + 1, ed + 1);
    
    for(int x, y, v, i = 1; i <= m; i++) {
        read(x), read(y), read(v);
        x++, y++;
        for(int j = 1; j <= K + 1; j++)
            add(id(j, x), id(j, y), v), add(id(j, y), id(j, x), v);
        for(int j = 1; j <= K; j++)
            add(id(j, x), id(j + 1, y), 0), add(id(j, y), id(j + 1, x), 0);
    }
    
    dij(st);
    
    printf("%d\n", dis[ed]);
    return 0;
}
Luogu 4568

 

posted @ 2018-09-12 20:14  CzxingcHen  阅读(176)  评论(1编辑  收藏  举报