結果

問題 No.3111 Toll Optimization
ユーザー tomo8
提出日時 2025-04-19 01:31:06
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 262 ms / 5,000 ms
コード長 5,418 bytes
コンパイル時間 3,498 ms
コンパイル使用メモリ 294,940 KB
実行使用メモリ 25,236 KB
最終ジャッジ日時 2025-04-19 01:31:20
合計ジャッジ時間 11,836 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 70
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef MAIN
#define MAIN
#include __FILE__

void _main(){
  cint(n, m, x);
  vector<vector<xy>> v(n, vector<xy>(0));
  dsu d(n);
  {
  cvec(m, C);
  cvec(m, A, B);
  rep(i, m) {
    int a = A[i], b = B[i], c = C[i];
    --a; --b;
    d.merge(a, b);
    v[a] += {b, c};
    v[b] += {a, c};
  }
  }
  if (!d.same(0, n-1)) {
    cout << -1 << endl;
    return;
  }
  vector<vector<int>> dist(n, vector<int>(x+1, inf));
  dist[0][x] = 0;
  // dijkstra
  rpq<xyz> q;
  q.push({0, 0, x});
  while (!q.empty()) {
    auto [d, f, a] = q.top(); q.pop();
    dist[f][a] = d;
    // dont use
    for (auto [k, c] : v[f]) {
      if (dist[k][a] > d + c) {
        q.push({d + c, k, a});
      }
    }
    // use
    if (a) {
      for (auto [k, _] : v[f]) {
        if (dist[k][a-1] > d) {
          q.push({d, k, a-1});
        }
      }
    }
    while (!q.empty() && dist[q.top()[1]][q.top()[2]] != inf) q.pop();
  }
  // owari
  cout << *min_element(all(dist[n-1])) << endl;
}

#else
#define MAIN

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using strvec = vector<string>;

constexpr ll mod10 = 1000000007;
constexpr ll mod9 = 998244353;
constexpr ll inf = 10000000000000000;

#define int ll
#define double ld
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repf(i, s, n) for (int i = s; i < (int)(n); i++)
template <class... Args>
void __y_input(Args &...arg)
{
    (cin >> ... >> arg);
}

template <class... Args>
void __y_input_vec_1(int p, Args &...args)
{
    (cin >> ... >> args[p]);
}
template <class Arg>
void __y_input_vec_resize(int size, Arg &arg)
{
    arg.resize(size);
}
template <class... Args>
void __y_input_vec(int n, Args &...args)
{
    (__y_input_vec_resize(n, args), ...);
    rep(i, n)
    {
        (__y_input_vec_1(i, args), ...);
    }
}
#define cint(...)  \
    int __VA_ARGS__; \
    __y_input(__VA_ARGS__)

#define cdbl(...)     \
    double __VA_ARGS__; \
    __y_input(__VA_ARGS__)

#define cstr(...)     \
    string __VA_ARGS__; \
    __y_input(__VA_ARGS__)

#define cvec(n, ...)       \
    vector<int> __VA_ARGS__; \
    __y_input_vec(n, __VA_ARGS__)

#define cvect(t, n, ...) \
    vector<t> __VA_ARGS__; \
    __y_input_vec(n, __VA_ARGS__)

#define last cout << endl
#define yn(bl) (bl ? "Yes" : "No")
#define all(v) v.begin(), v.end()
#define acc(v) accumulate(v.begin(), v.end(), 0LL)
#define nxp(v) next_permutation(v.begin(), v.end())
ostream &yesno(bool bl)
{
    cout << yn(bl);
    return cout;
}
void cyn(bool bl){
    cout << (bl ? "Yes" : "No") << endl;
}
typedef array<int, 2> xy;
typedef array<int, 3> xyz;

template <typename T>
inline void sort(T &vec)
{
    return sort(vec.begin(), vec.end());
}
template <typename T>
inline void rsort(T &vec)
{
    return sort(vec.rbegin(), vec.rend());
}
void _main();

template <typename T>
void prefix_sum(vector<T>& p, vector<T>& s){
  s.emplace_back(p[0]);
  for (size_t i = 1; i < p.size(); i++){
    s.emplace_back(p[i] + s.back());
  }
  return;
}

template <typename T>
void compress(T a, T b){
  vector m(a, b);
  sort(all(m));
  m.erase(unique(all(m)), m.end());
  for (auto itr = a; itr != b; itr++){
    *itr = (lower_bound(all(m), *itr) - m.begin());
  }
}

template <typename T>
using rpq = priority_queue<T, vector<T>, greater<T>>;

template <class T>
void operator+=(vector<T>& v, const T t) {
  v.emplace_back(t);
}

template <class S, S (*op)(S, S), S (*e)()>
struct Segtree {
    std::vector<S> Tree;
    int n;
    int depth = 1, width = 1;
    Segtree(int N = 0) : n(N) {
        while (width < n) depth++, width <<= 1;
        Tree.resize(width * 2);
        for (int i = width; i < width * 2; i++) Tree[i] = e();
        for (int i = width-1; i >= 0; i--) Tree[i] = op(Tree[i<<1], Tree[i<<1|1]);
    }
    Segtree(std::vector<S> v) {
        n = v.size();
        while (width < n) depth++, width <<= 1;
        Tree.resize(width * 2);
        for (int i = width; i < width * 2; i++) Tree[i] = (i < width + n ? v[i - width] : e());
        for (int i = width-1; i >= 0; i--) Tree[i] = op(Tree[i<<1], Tree[i<<1|1]);
    }
    void set(int pos, S val) {
        pos += width;
        Tree[pos] = val;
        for (int c = (pos >> 1); c > 0; c >>= 1) Tree[c] = op(Tree[c<<1], Tree[c<<1|1]);
        return;
    }
    S get(int pos) { return Tree[width + pos]; }
    S all_prod() { return Tree[1]; }
    S prod(int l, int r) {
        S x = e(), y = e();
        l += width, r += width;
        for (; l < r; l >>= 1, r >>= 1) {
            if (l & 1) x = op(x, Tree[l++]);
            if (r & 1) y = op(Tree[--r], y);
        }
        return op(x, y);
    }
};

struct dsu {
  vector<int> parent;
  dsu(int n){
    parent = vector<int>(n, -1);
  }
  int leader(int i){
    if (parent[i] < 0) return i;
    else return parent[i] = leader(parent[i]);
  }
  bool same(int a, int b){
    return (leader(a) == leader(b));
  }
  int size(int i){
    return -parent[leader(i)];
  }
  int merge(int a, int b){
    int la = leader(a), lb = leader(b);
    if (la == lb) return la;
    if (size(la) > size(lb)){
      parent[lb] += parent[la];
      parent[la] = lb;
      return lb;
    }
    else {
      parent[la] += parent[lb];
      parent[lb] = la;
      return la;
    }
  }
};

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

#endif
0