洛谷P3953 逛公園(dp 拓撲排序)

自為風月馬前卒發表於2018-10-31

題意

題目連結

Sol

去年考NOIP的時候我好像連最短路計數都不會啊qwq。。

首先不難想到一個思路,(f[i][j])表示到第(i)個節點,與最短路之差長度為(j)的路徑的方案數

首先把每個節點的最短路求出來

轉移的時候按拓撲序(也就是按距離從小到大排序)轉移一下

然而有(0)邊的時候會掛掉,原因是會有dis相同的時候,這時候單按dis排序會無法判斷轉移方向

一種方案是直接把所有(0)邊加入到新圖中,拓撲排序一遍。得到第二關鍵字

同時判斷一下(0)

#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP make_pair
#define fi first
#define se second 
using namespace std;
const int MAXN = 100001, INF = 1e9 + 7;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < `0` || c > `9`) {if(c == `-`) f = -1; c = getchar();}
    while(c >= `0` && c <= `9`) x = x * 10 + c - `0`, c = getchar();
    return x * f;
}
int T, N, M, K, mod, f[MAXN][51], dis1[MAXN], disn[MAXN], vis[MAXN], inder[MAXN], id[MAXN];
vector<Pair> v[MAXN], rv[MAXN];
vector<pair<pair<int, int>, int> > P;
vector<int> E[MAXN];
void init() {
    memset(f, 0, sizeof(f));
    memset(inder, 0, sizeof(inder));
    P.clear();
    f[1][0] = 1;
    N = read(); M = read(); K = read(); mod = read();
    for(int i = 1; i <= N; i++) v[i].clear(), E[i].clear();
    for(int i = 1; i <= M; i++) {
        int x = read(), y = read(), z = read();
        v[x].push_back(MP(y, z)); rv[y].push_back(MP(x, z));
        if(!z) E[x].push_back(y), inder[y]++;
    }
}
void add(int &x, int y) {
    if(x + y < 0) x = (x + y + mod);
    else x = (x + y >= mod ? x + y - mod : x + y);
}
int Topsort() {
    int cnt = 0; queue<int> q;
    for(int i = 1; i <= N; i++) if(!inder[i]) id[i] = ++cnt, q.push(i);
    while(!q.empty()) {
        int p = q.front(); q.pop();
        for(int i = 0, to; i < E[p].size(); i++) 
            if(!(--inder[to = E[p][i]])) q.push(to), id[to] = ++cnt;
    }
    for(int i = 1; i <= N; i++) if(inder[i] && (dis1[i] + disn[i] <= dis1[N] + K)) return -1;
    return 0;
}
void Dij(int bg, int *dis) {
    priority_queue<Pair> q; q.push(MP(0, bg)); 
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= N; i++) dis[i] = INF + 1; dis[bg] = 0;
    while(!q.empty()) {
        if(vis[q.top().se]) {q.pop(); continue;}
        int p = q.top().se; q.pop(); vis[p] = 1;
        if(bg == 1) {
            for(int i = 0, to; i < v[p].size(); i++) 
                if(dis[to = v[p][i].fi] > dis[p] + v[p][i].se) 
                    dis[to] = dis[p] + v[p][i].se, q.push(MP(-dis[to], to));            
        } else {
            for(int i = 0, to; i < rv[p].size(); i++) 
                if(dis[to = rv[p][i].fi] > dis[p] + rv[p][i].se) 
                    dis[to] = dis[p] + rv[p][i].se, q.push(MP(-dis[to], to));           
        }
    }   
}
int solve() {
    Dij(1, dis1);
    Dij(N, disn);
    if(Topsort() == -1) return -1;
    for(int i = 1; i <= N; i++) P.push_back(MP(MP(dis1[i], id[i]), i));
    sort(P.begin(), P.end());
    f[1][0] = 1;
    for(int j = 0; j <= K; j++) {
        for(int i = 0; i < N; i++) {
            int x = P[i].se;
            for(int k = 0; k < v[x].size(); k++) {
                int to = v[x][k].fi, w = v[x][k].se, ps = dis1[x] + j + w - dis1[to];
                if(ps <= K) add(f[to][ps], f[x][j]);
            }
        }
    }
    int ans = 0;
    for(int i = 0; i <= K; i++) add(ans, f[N][i]);
    return ans;
}
int main() {
    T = read(); 
    while(T--) {
        init();
        printf("%d
", solve());            
    }
    return 0;
}
/*
*/

相關文章