HDU - 5385 The path

HDU - 5385

先把1加入点集进行扩展, 然后两头分别从2和n开始往里加元素, 使得随着时间dis[ i ] 递增即可。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 1e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, m, d[N], U[N], V[N], ans[N];
bool vis[N];
vector<PII> G[N];
set<PII> Set;

void init() {
    Set.clear();
    for(int i = 1; i <= n; i++) {
        G[i].clear();
        vis[i] = false;
    }
}
int main() {
    int T; scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &n, &m);
        init();
        for(int i = 1; i <= m; i++) {
            int u, v;
            scanf("%d%d", &u, &v);
            U[i] = u; V[i] = v;
            G[u].push_back(mk(i, v));
            ans[i] = n;
        }
        d[1] = 0;
        vis[1] = true;
        for(auto &e : G[1]) {
            if(vis[e.se]) continue;
            vis[e.se] = true;
            Set.insert(mk(e.se, e.fi));
        }
        int tim = 1, big = n, sml = 2;
        while(SZ(Set)) {
            if(Set.begin()->fi == sml) {
                int eid = Set.begin()->se;
                Set.erase(*Set.begin());
                d[sml] = tim;
                ans[eid] = d[sml] - d[U[eid]];
                for(auto &e : G[sml]) {
                    if(vis[e.se]) continue;
                    vis[e.se] = true;
                    Set.insert(mk(e.se, e.fi));
                }
                sml++;
            }
            else if(Set.rbegin()->fi == big) {
                int eid = Set.rbegin()->se;
                Set.erase(*Set.rbegin());
                d[big] = tim;
                ans[eid] = d[big] - d[U[eid]];
                for(auto &e : G[big]) {
                    if(vis[e.se]) continue;
                    vis[e.se] = true;
                    Set.insert(mk(e.se, e.fi));
                }
                big--;
            } else {
                assert(0);
            }
            tim++;
        }
        for(int i = 1; i <= m; i++) {
            printf("%d\n", ans[i]);
        }
    }
    return 0;
}

/*
*/

 

posted @ 2019-07-13 20:24  NotNight  阅读(142)  评论(0编辑  收藏  举报