結果

問題 No.1473 おでぶなおばけさん
ユーザー homesentinelhomesentinel
提出日時 2021-04-09 22:19:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 4,653 bytes
コンパイル時間 1,530 ms
コンパイル使用メモリ 138,660 KB
実行使用メモリ 13,364 KB
最終ジャッジ日時 2023-09-07 11:43:48
合計ジャッジ時間 6,022 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 74 ms
12,532 KB
testcase_03 AC 52 ms
10,420 KB
testcase_04 AC 39 ms
8,480 KB
testcase_05 AC 15 ms
4,884 KB
testcase_06 AC 58 ms
10,548 KB
testcase_07 AC 75 ms
13,336 KB
testcase_08 AC 89 ms
13,348 KB
testcase_09 AC 86 ms
13,252 KB
testcase_10 AC 36 ms
4,564 KB
testcase_11 AC 36 ms
4,804 KB
testcase_12 AC 36 ms
4,624 KB
testcase_13 AC 23 ms
4,380 KB
testcase_14 AC 17 ms
4,380 KB
testcase_15 AC 32 ms
4,380 KB
testcase_16 AC 34 ms
4,556 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 5 ms
4,380 KB
testcase_19 AC 29 ms
5,680 KB
testcase_20 AC 42 ms
9,644 KB
testcase_21 AC 45 ms
7,540 KB
testcase_22 AC 63 ms
11,868 KB
testcase_23 AC 54 ms
10,688 KB
testcase_24 AC 52 ms
10,396 KB
testcase_25 AC 66 ms
11,616 KB
testcase_26 AC 75 ms
11,596 KB
testcase_27 AC 23 ms
6,184 KB
testcase_28 AC 66 ms
13,084 KB
testcase_29 AC 38 ms
7,512 KB
testcase_30 AC 44 ms
8,192 KB
testcase_31 AC 77 ms
13,364 KB
testcase_32 AC 47 ms
10,324 KB
testcase_33 AC 43 ms
9,488 KB
testcase_34 AC 26 ms
6,712 KB
testcase_35 AC 23 ms
5,868 KB
testcase_36 AC 44 ms
8,820 KB
testcase_37 AC 55 ms
10,440 KB
testcase_38 AC 10 ms
4,480 KB
testcase_39 AC 35 ms
4,520 KB
testcase_40 AC 35 ms
4,540 KB
testcase_41 AC 33 ms
4,540 KB
testcase_42 AC 32 ms
4,512 KB
testcase_43 AC 43 ms
9,448 KB
testcase_44 AC 43 ms
9,652 KB
testcase_45 AC 43 ms
9,620 KB
testcase_46 AC 46 ms
9,940 KB
testcase_47 AC 59 ms
11,504 KB
testcase_48 AC 53 ms
10,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>


using namespace std;
//using namespace atcoder;

#define REP(i, n) for (int i=0; i<(n); ++i)
#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    return os << "(" << p.first << " " << p.second << ")";
}

template<class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;


// edit

struct UnionFind {
    vector<int> par, sz;

    UnionFind(int n) : par(n), sz(n, 1) {
        for (int i = 0; i < n; ++i) par[i] = i;
    }

    int root(int x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void merge(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return;
        if (sz[x] < sz[y]) swap(x, y);
        par[y] = x;
        sz[x] += sz[y];
        sz[y] = 0;
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    int size(int x) {
        return sz[root(x)];
    }
};


template<typename T>
class graph {
    struct edge {
        int src, to;
        T cost;
    };

public:
    int n;
    vector<vector<edge>> edges;

    graph(int n_) : n(n_), edges(n_) {
    }

    void add_edge(int src, int to, T cost) {
        edges[src].push_back({src, to, cost});
    }
};

template<typename T>
pair<vector<T>, vector<int>> dijkstra(const graph<T> &g, int s, T inf) {
    vector<T> d(g.edges.size(), inf);
    vector<int> p(g.edges.size());

    using Pi = pair<T, int>;
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;

    que.emplace(0, s);
    d[s] = 0;
    p[s] = -1;

    while (!que.empty()) {
        T cost;
        int v;
        tie(cost, v) = que.top();
        que.pop();

        if (d[v] < cost) continue;
        for (const auto &e : g.edges[v]) {
            T ncost = cost + e.cost;
            int nv = e.to;
            if (ncost < d[nv]) {
                d[nv] = ncost;
                p[nv] = v;
                que.emplace(ncost, nv);
            }
        }
    }

    return make_pair(d, p);
}

void solve() {
    int n, m;
    cin >> n >> m;

    vector<tuple<int, int, ll>> E(m);
    REP(i, m) {
        int s, t;
        ll d;
        cin >> s >> t >> d;
        s--, t--;
        E[i] = make_tuple(s, t, d);
    }

    sort(ALL(E), [](auto l, auto r) {
        return get<2>(l) > get<2>(r);
    });

    UnionFind uf(n);
    graph<int> g(n);

    int max_weight = 0;
    for (int i = 0; i < m; ++i) {
        if (uf.issame(0, n - 1) && get<2>(E[i - 1]) != get<2>(E[i])) {
            break;
        }
        max_weight = get<2>(E[i]);
        int s, t;
        ll d;
        tie(s, t, d) = E[i];
        uf.merge(s, t);
        g.add_edge(s, t, 1);
        g.add_edge(t, s, 1);
    }

    auto dist = dijkstra(g, 0, INF).first;
    int ans = dist[n - 1];

    cout << max_weight << " " << ans << endl;


}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
//    std::ifstream in("input.txt");
//    std::cin.rdbuf(in.rdbuf());

    solve();


    return 0;
}
0