結果

問題 No.1995 CHIKA Road
ユーザー Linaria2002Linaria2002
提出日時 2022-07-20 21:38:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 543 ms / 2,000 ms
コード長 3,994 bytes
コンパイル時間 2,704 ms
コンパイル使用メモリ 220,620 KB
実行使用メモリ 45,540 KB
最終ジャッジ日時 2023-09-15 10:17:07
合計ジャッジ時間 9,502 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 17 ms
5,776 KB
testcase_07 AC 73 ms
11,572 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 79 ms
12,544 KB
testcase_11 AC 543 ms
45,540 KB
testcase_12 AC 209 ms
26,040 KB
testcase_13 AC 126 ms
17,192 KB
testcase_14 AC 132 ms
18,080 KB
testcase_15 AC 437 ms
40,456 KB
testcase_16 AC 31 ms
7,148 KB
testcase_17 AC 175 ms
20,740 KB
testcase_18 AC 328 ms
31,812 KB
testcase_19 AC 340 ms
31,968 KB
testcase_20 AC 155 ms
18,932 KB
testcase_21 AC 142 ms
17,548 KB
testcase_22 AC 319 ms
30,584 KB
testcase_23 AC 78 ms
11,788 KB
testcase_24 AC 262 ms
27,008 KB
testcase_25 AC 37 ms
8,184 KB
testcase_26 AC 92 ms
13,516 KB
testcase_27 AC 256 ms
26,340 KB
testcase_28 AC 141 ms
17,392 KB
testcase_29 AC 311 ms
31,100 KB
testcase_30 AC 34 ms
8,040 KB
testcase_31 AC 16 ms
5,444 KB
testcase_32 AC 48 ms
9,588 KB
testcase_33 AC 239 ms
25,892 KB
testcase_34 AC 19 ms
5,856 KB
testcase_35 AC 83 ms
12,652 KB
testcase_36 AC 100 ms
14,396 KB
testcase_37 AC 295 ms
29,392 KB
testcase_38 AC 190 ms
22,224 KB
testcase_39 AC 15 ms
5,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define int ll
#define rep(i, N) for(int i = 0; i < (int)(N); ++i)
#define rep1(i, N) for(int i = 1; i <= (int)(N); ++i)
#define per(i, N) for(int i = (N)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define bit(n, k) ((n) >> (k))
#define pcnt(n) (__builtin_popcount(n))
#define YesNo(ans) ans ? cout << "Yes\n" : cout << "No\n"
#define endl '\n'
#define fi first
#define se second
#define mkpr make_pair
#define mktpl make_tuple
#define eb emplace_back

using namespace std;
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
//using point = struct{ ll x, y; };
using point = struct{ ld x, y; };
using State = string::iterator;
template<class T> using max_heap = priority_queue<T>;
template<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<class T> using vec = vector<T>;
template<class T> using vvec = vec<vec<T>>;
template<class T> using vvvec = vec<vvec<T>>;
template<class T> using vvvvec = vvec<vvec<T>>;

constexpr ld EPS = 1e-10;
ld Pi = acos(-1);
constexpr int INF = 1001001001;
constexpr ll LINF = 1001001001001001001ll;
constexpr ll MOD = (0) ? 998244353 : 1e9+7;
constexpr int NIL = -1;
constexpr int pm[2] = {1, -1};
constexpr int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
constexpr int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};

ll cel(ll a, ll b){ return (a + b - 1) / b;}
ll Gcd(ll a, ll b){ return b ? Gcd(b, a % b) : abs(a);}
ll Lcm(ll a, ll b){ return a / Gcd(a, b) * b;}
template<class T> T powi(T x, ll exp){
    return exp > 0 ? (exp & 1 ? x : 1) * powi(x * x, exp >> 1) : x / x;
}
ll modpow(ll x, ll exp, ll mod){
    x %= mod;
    return exp > 0 ? (exp & 1 ? x : 1) * modpow(x * x, exp >> 1, mod) % mod : 1;
}
template<class T> bool chmin(T &a, T b){ return a > b ? (a = b, true) : 0;}
template<class T> bool chmax(T &a, T b){ return a < b ? (a = b, true) : 0;}

using Pair = pair<ll, ll>;
using Tpl = tuple<int, int, int>;

class Dijkstra{
    using DS = pair<ll, int>;
    int N;
    // {distance, v}        
    priority_queue<DS, vector<DS>, greater<DS>> Que;
    // {weight, v}
    vector<vector<DS>> G;

    public:
    // {dist, from}
    vector<DS> dist;
    Dijkstra(int N) : N(N), G(N), dist(N) {}
    void add_edge(int from, int to, int cst){
        G[from].emplace_back(DS{cst, to});
    }
    void solve(int s){
        rep(i, N) dist[i] = {LINF, NIL};
        dist[s].fi = 0;
        Que.push(DS{0, s});
        while(!Que.empty()){
            DS p = Que.top();
            Que.pop();
            int u = p.se;
            if(dist[u].fi != p.fi) continue;
            for(auto v : G[u]){
                if(dist[v.se].fi > dist[u].fi + v.fi){
                    dist[v.se].fi = dist[u].fi + v.fi;
                    dist[v.se].se = u;
                    Que.push(DS{dist[v.se].fi, v.se});
                }
            }
        }
    }
};

void Main(){
    int N, M; cin >> N >> M;
    map<int, int> mp;
    mp[1] = 0, mp[N] = 0;
    vec<Tpl> edge;
    rep(i, M){
        int a, b; cin >> a >> b;
        mp[a] = 0, mp[b] = 0;
        edge.eb(mktpl(a, b, 2 * (b - a) - 1));
    }
    int n = 0, prev;
    Dijkstra di(mp.size());
    for(auto it : mp){
        if(n > 0){
            //cout << n << endl;
            di.add_edge(n - 1, n, 2 * (it.fi - prev));
            di.add_edge(n, n - 1, 2 * (it.fi - prev));
            //cout << it.fi - prev << endl;
        }
        prev = it.fi;
        mp[it.fi] = n;
        n++;
    }

    rep(i, M){
        Tpl t = edge[i];
        //cout << mp[get<0>(t)] << mp[get<1>(t)] << get<2>(t) << endl;
        di.add_edge(mp[get<0>(t)], mp[get<1>(t)], get<2>(t));
        di.add_edge(mp[get<1>(t)], mp[get<0>(t)], get<2>(t));
    }

    di.solve(0);
    rep(i, 0){
        cout << di.dist[i].fi << endl;
    }
    cout << di.dist[mp.size() - 1].fi << endl;
}

signed main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    Main();
    return 0;
}
0