zoukankan      html  css  js  c++  java
  • HDU

    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
    ", ans[i]);
            }
        }
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    将光标定位于输入框最右侧的实现方式
    Canvas学习笔记
    CSS3 颜色模式
    CSS ^ $选择器
    jQuery.Validator Sample
    让网页的title动起来
    转:线程间操作无效: 从不是创建控件“”的线程访问它~~~的解决方法~
    winform 表单正则表达式验证 示例(ValidationRule)
    详解用Navicat工具将Excel中的数据导入Mysql中
    PHP面试题之实现输出100以内的质数
  • 原文地址:https://www.cnblogs.com/CJLHY/p/11181935.html
Copyright © 2011-2022 走看看