最短路分層圖專題 洛谷P2939,4822,4568

2020.6.3
今天主要練習了下分層圖。看洛谷題解每次都能有新收穫。今天本來想練dp,後來感覺可能會太自閉了,不如先來一發最短路,畢竟看家本領不能忘。然後點進了北京某年wc的一道題,讓求1-n的最短路,有k條邊的費用可以減半,我一開始還是想着用spfa加一個fa數組溯回去再sort一遍來搞一搞,其實我知道這肯定不對,畢竟1-n可以有多條最短路來着。2-3-4和1-2-3-1-1這樣的費用是一樣,但是這樣判斷最短路,加上k條可以費用/2那就不一定了。還是需要更聰明一點的判斷方法,結果就看到大家在刷分層圖,正好想起naq17也有一張分層圖的題,是有一張免費機票,就來了。

那道題我ac了,但是當時我記得我是用dijkstra + 前向星強行刪邊莽過去了(n不大),但是那樣不太科學,畢竟如果有多於1條邊可以歸零,那就需要一些系統性的方法去搞了,那樣我真的暴力也打不太出來了。大概是用於當我們可以改變k個圖中邊的權值的時候,就可以上分層圖了。

研究了一下,感覺和我之前的想法很像,都是重新編號然後給層與層之間建立聯繫,但我當時礙於自己數學能力有限也沒有想出來怎麼搞。看了這個恍然大悟,艹!!np!!!然後趕緊看了看,敲了dij,然後過了這道題(感覺不像noi d1t1那樣卡spfa其實spfa更快??),之後根據題單又過了幾道題,連帶上次naq17的,新技能get,我覺得這個有用多了hhhh,不過dp再搞一搞。

洛谷P4822 凍結

#include <bits/stdc++.h>
using namespace std;
#define limit (1000000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步兩步
#define EPS 1e-6
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%lld\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(int i = a; i <= b ; ++i)
#define per(i, a, b) for(int i = b ; i >= a ; --i)
#define mint(a,b,c) min(min(a,b), c)
#define MOD 998244353
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\data.txt", "rt", stdin)
typedef long long ll;
typedef unsigned long long ull;
ll read(){
    ll sign = 1, x = 0;char s = getchar();
    while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
    while(s >= '0' && s <= '9'){x = x * 10 + s - '0';s = getchar();}
    return x * sign;
}//快讀
void write(ll x){
    if(x < 0) putchar('-'),x = -x;
    if(x / 10) write(x / 10);
    putchar(x % 10 + '0');
}
int n , m ,k;
int head[limit], cnt;
int dist[limit], vis[limit];
void init(){
    cnt = 0;
    memset(head, -1 , sizeof(head));
    memset(dist, INF, sizeof(dist));
}
struct node{
    int to, w, next;
}edge[limit<<1];
void add(int u, int v, int w = 0){
    edge[cnt].to = v;
    edge[cnt].w = w;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}

struct nod{
    int to, w;
    bool operator<(const nod & rhs)const{
        return w > rhs.w;
    }
};
void dijkstra(){
    priority_queue<nod>q;
    q.push({1,0});
    dist[1] = 0;
    while(!q.empty()){
        nod u = q.top();
        q.pop();
        vis[u.to] = 1;
        for(int i  = head[u.to] ; ~i ; i = edge[i].next){
            int v = edge[i].to, w = edge[i].w;
            if(vis[v])continue;
            if(dist[u.to] + w < dist[v]){
                dist[v] = dist[u.to] + w;
                q.push({v, dist[v]});
            }
        }
    }
}
int main() {
#ifdef LOCAL
    FOPEN;
#endif
    n = read() ,m = read(), k = read();
    init();
    rep(j ,1, m){
        int x = read(), y = read(), w = read();
        rep(i ,0, k){
            add(i * n + x, i * n + y, w);
            add(i * n + y , i * n + x , w);
        }
        rep(i ,0,k-1 ){
            add(i * n + x, (i + 1) * n + y, w / 2);
            add(i * n + y , (i + 1) * n + x , w/ 2);
        }
    }
    dijkstra();
    int ans = INF;
    rep(i ,0 , k){
        ans = min(ans, dist[i * n + n]);
    }
    write(ans);
    return 0;
}

JLOI P4568

#include <bits/stdc++.h>
using namespace std;
#define limit (10000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步兩步
#define EPS 1e-6
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%lld\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(int i = a; i <= b ; ++i)
#define per(i, a, b) for(int i = b ; i >= a ; --i)
#define mint(a,b,c) min(min(a,b), c)
#define MOD 998244353
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\data.txt", "rt", stdin)
typedef long long ll;
typedef unsigned long long ull;
ll read(){
    ll sign = 1, x = 0;char s = getchar();
    while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
    while(s >= '0' && s <= '9'){x = x * 10 + s - '0';s = getchar();}
    return x * sign;
}//快讀
void write(ll x){
    if(x < 0) putchar('-'),x = -x;
    if(x / 10) write(x / 10);
    putchar(x % 10 + '0');
}
int n , m ,k;
int head[limit], cnt;
int dist[limit], vis[limit];
void init(){
    cnt = 0;
    memset(head, -1 , sizeof(head));
    memset(dist, INF, sizeof(dist));
}
struct node{
    int to, w, next;
}edge[limit<<1];
void add(int u, int v, int w = 0){
    edge[cnt].to = v;
    edge[cnt].w = w;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}

struct nod{
    int to, w;
    bool operator<(const nod & rhs)const{
        return w > rhs.w;
    }
};
void dijkstra(int s){
    priority_queue<nod>q;
    q.push({s,0});
    dist[s] = 0;
    while(!q.empty()){
        nod u = q.top();
        q.pop();
        vis[u.to] = 1;
        for(int i  = head[u.to] ; ~i ; i = edge[i].next){
            int v = edge[i].to, w = edge[i].w;
            if(vis[v])continue;
            if(dist[u.to] + w < dist[v]){
                dist[v] = dist[u.to] + w;
                q.push({v, dist[v]});
            }
        }
    }
}
int main() {
#ifdef LOCAL
    FOPEN;
#endif
    n = read() ,m = read(), k = read();
    int s= read() ,t = read();
    init();
    rep(q, 1, m){
        int x= read(), y = read() , w = read();
        rep(i ,0 , k){
            add(i * n + x , i * n + y , w);
            add(i * n + y , i * n + x , w);
        }
        rep(i , 0 , k - 1){
            add(i * n + x , (i + 1) * n + y , 0);
            add(i * n + y , (i + 1) * n + x, 0);
        }
    }
    dijkstra(s);
    int ans = INF;
    rep(i ,0, k)ans = min(ans, dist[i * n + t]);
    write(ans);
    return 0;
}

洛谷P2939
Revamping Trails

#include <bits/stdc++.h>
using namespace std;
#define limit (5000000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步兩步
#define EPS 1e-6
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%lld\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(int i = a; i <= b ; ++i)
#define per(i, a, b) for(int i = b ; i >= a ; --i)
#define mint(a,b,c) min(min(a,b), c)
#define MOD 998244353
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\data.txt", "rt", stdin)
typedef long long ll;
typedef unsigned long long ull;
ll read(){
    ll sign = 1, x = 0;char s = getchar();
    while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
    while(s >= '0' && s <= '9'){x = x * 10 + s - '0';s = getchar();}
    return x * sign;
}//快讀
void write(ll x){
    if(x < 0) putchar('-'),x = -x;
    if(x / 10) write(x / 10);
    putchar(x % 10 + '0');
}
int n , m ,k;
int head[limit], cnt;
int dist[limit], vis[limit];
void init(){
    cnt = 0;
    memset(head, -1 , sizeof(head));
    memset(dist, INF, sizeof(dist));
}
struct node{
    int to, w, next;
}edge[limit<<1];
void add(int u, int v, int w = 0){
    edge[cnt].to = v;
    edge[cnt].w = w;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}

struct nod{
    int to, w;
    bool operator<(const nod & rhs)const{
        return w > rhs.w;
    }
};
void dijkstra(int s=1){
    priority_queue<nod>q;
    q.push({s,0});
    dist[s] = 0;
    while(!q.empty()){
        nod u = q.top();
        q.pop();
        vis[u.to] = 1;
        for(int i  = head[u.to] ; ~i ; i = edge[i].next){
            int v = edge[i].to, w = edge[i].w;
            if(vis[v])continue;
            if(dist[u.to] + w < dist[v]){
                dist[v] = dist[u.to] + w;
                q.push({v, dist[v]});
            }
        }
    }
}
int main() {
#ifdef LOCAL
    FOPEN;
#endif
    n = read() ,m = read(), k = read();
    init();
    rep(q, 1, m){
        int x= read(), y = read() , w = read();
        rep(i ,0 , k){
            add(i * n + x , i * n + y , w);
            add(i * n + y , i * n + x , w);
        }
        rep(i , 0 , k - 1){
            add(i * n + x , (i + 1) * n + y , 0);
            add(i * n + y , (i + 1) * n + x, 0);
        }
    }
    dijkstra();
    int ans = INF;
    rep(i ,0, k)ans = min(ans, dist[i * n + n]);
    write(ans);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章