2019 ICPC Asia Yinchuan Regional-H. Delivery Route(dijkstra+拓撲排序)

2019 ICPC Asia Yinchuan Regional-H. Delivery Route(dijkstra+拓撲排序)

題面:

題意:

給定一個含有\(\mathit n\) 個點,\(\mathit x\)個雙向邊,\(\mathit y\)個單向邊的圖,其中雙向邊的權值一定爲正整數,單向邊的權值有可能爲負整數。並且保證圖中若有一個\(u->v\)的單向路徑,則一定不存在\(v->u\)的路徑。

求以\(\mathit s\)爲源點到各個點的最短路徑值,若無法到達某個點,則輸出no path。

思路:

根據圖的保證可知:

將雙向邊構成的連通塊縮成點時,整個圖變成了一個有向無環圖,連通塊內不會存在有向邊。

我們先將無向邊構成的圖用dfs進行連通塊染色(縮點),然後根據單向邊建立縮點後的有向無環圖(DAG),

從DAG中入度爲0的點加入隊列開始拓撲排序,對於每一個連通塊,我們將塊中的點放入優先隊列後用dijkstra算法跑出最短路,對於這些點中連出的單向邊,我們更新答案但是不放入優先隊列,同時將指向的連通塊入度減一,以此就可以高效正確的單源最短路。

注意本題有一個坑點:

連通塊的點\(now.to\)連出的單向邊,要當且僅當:\(dis[now.to] != inf\) 纔要去更新,

不然可能會本該no path的點,輸出了數值。

代碼:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 25000 + 10;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
struct node {
    int to;
    int val;
    node() {}
    node(int xx, int yy)
    {
        to = xx; val = yy;
    }
    bool operator < (const node &b)const
    {
        return val > b.val;
    }
};
priority_queue<node> dij_q;
std::vector<node> e[maxn];
int n, m1, m2, s;
int col[maxn], in[maxn], dis[maxn];
int vis[maxn];
vector<int> v[maxn];
int id = 0;
void dfs(int x)
{
    col[x] = id;
    for (auto y : e[x]) {
        if (!col[y.to]) {
            dfs(y.to);
        }
    }
}
void dij()
{
    queue<int> q;
    repd(i, 1, id) {
        if (!in[i]) {
            q.push(i);
        }
    }
    while (!q.empty()) {
        int temp = q.front();
        q.pop();
        for (auto x : v[temp]) {
            dij_q.push(node(x, dis[x]));
        }
        node now;
        while (!dij_q.empty()) {
            now = dij_q.top();
            dij_q.pop();
            if (vis[now.to]) { continue; }
            vis[now.to] = 1;
            for (auto x : e[now.to]) {
                if (col[x.to] == col[now.to]) {
                    if (dis[x.to] > dis[now.to] + x.val) {
                        dis[x.to] = dis[now.to] + x.val;
                        dij_q.push(node(x.to, dis[x.to]));
                    }
                }
                if (col[x.to] != col[now.to]) {
                    if (dis[now.to] != inf && dis[x.to] > dis[now.to] + x.val) {
                        dis[x.to] = dis[now.to] + x.val;
                    }
                    --in[col[x.to]];
                    if (!in[col[x.to]]) {
                        q.push(col[x.to]);
                    }
                }
            }
        }
    }
}
int main()
{
#if DEBUG_Switch
    freopen("D:\\code\\input.txt", "r", stdin);
#endif
    //freopen("D:\\code\\output.txt","w",stdout);
    n = readint();
    m1 = readint(); m2 = readint(); s = readint();
    repd(i, 1, m1) {
        int x = readint(); int y = readint();
        int w = readint();
        e[x].pb(node(y, w));
        e[y].pb(node(x, w));
    }
    repd(i, 1, n) {
        if (!col[i]) {
            id++;
            dfs(i);
        }
        dis[i] = inf;
    }
    dis[s] = 0;
    repd(i, 1, n) {
        v[col[i]].pb(i);
    }
    repd(i, 1, m2) {
        int x = readint(); int y = readint();
        int w = readint();
        e[x].pb(node(y, w));
        in[col[y]]++;
    }
    dij();
    repd(i, 1, n) {
        if (dis[i] == inf) {
            printf("NO PATH\n");
        } else {
            printf("%d\n", dis[i]);
        }
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章