結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー opt
提出日時 2020-12-17 09:46:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 726 ms / 2,000 ms
コード長 1,898 bytes
コンパイル時間 2,273 ms
コンパイル使用メモリ 209,628 KB
最終ジャッジ日時 2025-01-17 02:23:00
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
using ll = long long;
template<class T> inline bool chmin(T &a, const T b) { if (a > b) { a = b; return true; } return false; }
template<class T, size_t n> istream &operator>>(istream &is, array<T, n> &v) { for (auto &e : v) is >> e; return is; }
template<class T> istream &operator>>(istream &is, vector<T> &v) { for (auto &e : v) is >> e; return is; }
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
const ll INFll = (1ll<<60) - 1;


template<typename T>
struct edge {
  int v; T w;
  edge(int v, T w) : v(v), w(w) {}
};
template<typename T>
using Graph = vector<vector<edge<T>>>;

template<typename T>
vector<T> dijkstra(const Graph<T> &G, int s) {
  int n = G.size();
  const auto INF_T = numeric_limits<T>::max() / 2 - 1;
  vector<T> dist(n, INF_T);
  using PTi = pair<T, int>;
  priority_queue<PTi, vector<PTi>, function<bool(PTi, PTi)>> pq(
    [] (auto x, auto y) { return x.first > y.first; }
  );

  dist[s] = 0;
  pq.emplace(dist[s], s);
  
  while (!pq.empty()) {
    auto [d, u] = pq.top(); pq.pop();
    if (dist[u] < d) continue;
    for (auto &[v, w] : G[u]) if (chmin(dist[v], d + w)) pq.emplace(dist[v], v);
  }
  return dist;
}


int main() {
  int T, n, m; cin >> T >> n >> m;
  vector<array<ll, 3>> E(m); cin >> E;
  for (auto &[u, v, w] : E) --u, --v;

  ll ans = INFll;
  rep(i, m) {
    Graph<ll> G(n);
    int s = -1, t = -1, wst = -1;
    rep(j, m) {
      auto [u, v, w] = E[j];
      if (j == i) {
        s = v, t = u, wst = w;
        continue;
      }
      G[u].emplace_back(v, w);
      if (T == 0) G[v].emplace_back(u, w);
    }
    auto dist = dijkstra(G, s);
    chmin(ans, dist[t] + wst);
  }

  if (ans == INFll) ans = -1;
  cout << ans << '\n';
}
0