結果

問題 No.1473 おでぶなおばけさん
ユーザー CleyLCleyL
提出日時 2022-08-18 16:09:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 148 ms / 2,000 ms
コード長 2,768 bytes
コンパイル時間 1,130 ms
コンパイル使用メモリ 95,776 KB
実行使用メモリ 11,648 KB
最終ジャッジ日時 2024-04-16 00:32:45
合計ジャッジ時間 7,970 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 143 ms
10,880 KB
testcase_03 AC 112 ms
9,216 KB
testcase_04 AC 75 ms
7,552 KB
testcase_05 AC 28 ms
5,376 KB
testcase_06 AC 122 ms
9,216 KB
testcase_07 AC 143 ms
11,648 KB
testcase_08 AC 148 ms
11,648 KB
testcase_09 AC 143 ms
11,520 KB
testcase_10 AC 97 ms
5,376 KB
testcase_11 AC 97 ms
5,376 KB
testcase_12 AC 97 ms
5,376 KB
testcase_13 AC 60 ms
5,376 KB
testcase_14 AC 43 ms
5,376 KB
testcase_15 AC 83 ms
5,376 KB
testcase_16 AC 95 ms
5,376 KB
testcase_17 AC 8 ms
5,376 KB
testcase_18 AC 12 ms
5,376 KB
testcase_19 AC 71 ms
5,376 KB
testcase_20 AC 89 ms
8,704 KB
testcase_21 AC 102 ms
6,656 KB
testcase_22 AC 108 ms
10,112 KB
testcase_23 AC 91 ms
9,728 KB
testcase_24 AC 91 ms
9,216 KB
testcase_25 AC 131 ms
9,984 KB
testcase_26 AC 139 ms
10,112 KB
testcase_27 AC 55 ms
5,632 KB
testcase_28 AC 139 ms
11,264 KB
testcase_29 AC 103 ms
6,912 KB
testcase_30 AC 120 ms
7,296 KB
testcase_31 AC 125 ms
11,392 KB
testcase_32 AC 105 ms
9,216 KB
testcase_33 AC 89 ms
8,320 KB
testcase_34 AC 49 ms
6,144 KB
testcase_35 AC 53 ms
5,504 KB
testcase_36 AC 100 ms
7,680 KB
testcase_37 AC 101 ms
8,960 KB
testcase_38 AC 23 ms
5,376 KB
testcase_39 AC 97 ms
5,376 KB
testcase_40 AC 97 ms
5,376 KB
testcase_41 AC 84 ms
5,376 KB
testcase_42 AC 84 ms
5,376 KB
testcase_43 AC 102 ms
8,900 KB
testcase_44 AC 101 ms
9,152 KB
testcase_45 AC 104 ms
8,896 KB
testcase_46 AC 101 ms
8,704 KB
testcase_47 AC 124 ms
9,984 KB
testcase_48 AC 110 ms
9,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
struct edge{
    int s,t,d;
};
bool operator<(edge a, edge b){
    if(a.d == b.d){
        if(a.s == b.s){
            return a.t < b.t;
        }
       return a.s < b.s;
    }
    return a.d < b.d;
}
bool operator==(edge a, edge b){
    return a.s == b.s && a.t == b.t && a.d == b.d;
}
bool operator>(edge a,edge b){
    return !(a<b || a==b);
}
struct UnionFind {
  int n;
  vector<int> par;
  vector<int> size_;
  UnionFind(int n_) : n(n_), par((size_t)n_), size_((size_t)n_,1){
    for(int i = 0; n > i; i++)par[i] = i;
  }

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

  void unite(int a,int b){
    int ra = root(a);
    int rb = root(b);
    if(ra==rb)return;
    if(size(ra) > size(rb)) swap(ra,rb);
    par[ra] = rb;
    size_[rb] += size_[ra];
  }

  bool same(int a, int b){
    return root(a) == root(b);
  }

  int size(int a){
    return size_[root(a)];
  }
  void debug(){
    for(int i = 0; n > i; i++){
      cout << size_[root(i)] << " ";
    }
    cout << endl;

    return;
  }
};
template<typename T>
class dijkstra{
  struct vg{
    int t;
    T c;
  };
  vector<T> cost;
  void run(int x){
    auto comp = [](vg a,vg b){
      return a.c>b.c;
    };
    priority_queue<vg,vector<vg>,decltype(comp)> R{comp};
    for(int i = 0; n > i; i++){
      cost[i] = -1;
    }
    cost[x] = 0;
    R.push({x,0});
    while(R.size()){
      auto k = R.top();R.pop();
      if(cost[k.t] != k.c)continue;
      for(int i = 0; A[k.t].size() > i; i++){
        if(cost[A[k.t][i].t] != -1 && cost[A[k.t][i].t] <= k.c+A[k.t][i].c)continue;
        cost[A[k.t][i].t] = k.c+A[k.t][i].c;
        R.push({A[k.t][i].t,k.c+A[k.t][i].c});
      }
    }

  }

public:
  int n;
  vector<vector<vg>> A;
	dijkstra(int n_):n(n_),A(n_),cost(n_,-1){}

  //双方向
  void push(int s,int v){
    A[s].push_back({v,1});
    A[v].push_back({s,1});
  }
  void push(int s,int v,int c){
    A[s].push_back({v,c});
    A[v].push_back({s,c});
  }

  void push_side(int s,int v,int c){
    A[s].push_back({v,c});
  }

  void build(int x){
    run(x);
  }

  int operator[](int i){
    return cost[i];
  }
  
};int main(){
    int n,m;cin>>n>>m;
    vector<edge> A(m);
    for(int i = 0; m > i; i++){
        cin>>A[i].s>>A[i].t>>A[i].d;
        A[i].s--;A[i].t--;
    }
    sort(A.begin(),A.end(),greater<edge>());
    UnionFind B(n);
    dijkstra<int> C(n);
    int rew = -1;
    for(int i = 0; m > i; i++){
        if(rew > A[i].d)break;
        C.push(A[i].s,A[i].t);
        B.unite(A[i].s,A[i].t);
        if(B.same(0,n-1)){
            
            rew = A[i].d;
        }
    }
    C.build(0);
    cout << rew << " " << C[n-1] << endl;
}
0