結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー 👑 emthrmemthrm
提出日時 2020-12-19 15:23:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,531 bytes
コンパイル時間 2,663 ms
コンパイル使用メモリ 221,868 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-21 10:20:07
合計ジャッジ時間 4,969 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
5,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 14 ms
5,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 12 ms
5,376 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 3 ms
5,376 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 43 ms
5,376 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 30 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 6 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 63 ms
5,376 KB
testcase_34 AC 29 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 3 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 AC 47 ms
5,376 KB
testcase_49 AC 4 ms
5,376 KB
testcase_50 AC 2 ms
5,376 KB
testcase_51 AC 2 ms
5,376 KB
testcase_52 AC 10 ms
5,376 KB
testcase_53 AC 6 ms
5,376 KB
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename CostType>
struct Edge {
  int src, dst; CostType cost;
  Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
  inline bool operator<(const Edge &x) const {
    return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src;
  }
  inline bool operator<=(const Edge &x) const { return !(x < *this); }
  inline bool operator>(const Edge &x) const { return x < *this; }
  inline bool operator>=(const Edge &x) const { return !(*this < x); }
};

template <typename CostType>
CostType girth_in_directed_graph(const std::vector<std::vector<Edge<CostType>>> &graph, const CostType CINF) {
  int n = graph.size();
  CostType res = CINF;
  std::vector<CostType> dist(n);
  using Pci = std::pair<CostType, int>;
  std::priority_queue<Pci, std::vector<Pci>, std::greater<Pci>> que;
  for (int root = 0; root < n; ++root) {
    std::fill(dist.begin(), dist.end(), CINF);
    dist[root] = 0;
    que.emplace(0, root);
    while (!que.empty()) {
      CostType cost; int ver; std::tie(cost, ver) = que.top(); que.pop();
      if (dist[ver] < cost) continue;
      for (const Edge<CostType> &e : graph[ver]) {
        CostType cost = dist[ver] + e.cost;
        if (cost < dist[e.dst]) {
          dist[e.dst] = cost;
          que.emplace(cost, e.dst);
        } else if (e.dst == root) {
          if (cost < res) res = cost;
        }
      }
    }
  }
  return res;
}

template <typename CostType>
CostType girth_in_undirected_graph(int n, const std::vector<Edge<CostType>> &edges, const CostType CINF) {
  int m = edges.size();
  std::vector<std::vector<int>> graph(n);
  for (int i = 0; i < m; ++i) {
    graph[edges[i].src].emplace_back(i);
    graph[edges[i].dst].emplace_back(i);
  }
  std::vector<bool> used(m, false);
  std::vector<CostType> dist(n);
  std::vector<int> label(n);
  using Pci = std::pair<CostType, int>;
  std::priority_queue<Pci, std::vector<Pci>, std::greater<Pci>> que;
  CostType res = CINF;
  for (int root = 0; root < n; ++root) {
    std::fill(used.begin(), used.end(), false);
    std::fill(dist.begin(), dist.end(), CINF);
    dist[root] = 0;
    std::fill(label.begin(), label.end(), -2);
    label[root] = -1;
    que.emplace(0, root);
    while (!que.empty()) {
      CostType cost; int ver; std::tie(cost, ver) = que.top(); que.pop();
      if (dist[ver] < cost) continue;
      for (int id : graph[ver]) {
        int dst = edges[id].dst;
        CostType cost = dist[ver] + edges[id].cost;
        if (cost < dist[dst]) {
          used[id] = true;
          dist[dst] = cost;
          label[dst] = label[ver] == -1 ? dst : label[ver];
          que.emplace(cost, dst);
        }
      }
    }
    for (int i = 0; i < m; ++i) {
      int src = edges[i].src, dst = edges[i].dst;
      CostType cost = edges[i].cost;
      if (!used[i] && label[src] != -2 && label[dst] != -2) {
        if (label[src] != label[dst]) {
          CostType loop = dist[src] + dist[dst] + cost;
          if (loop < res) res = loop;
        } else if (label[src] == -1) {
          if (cost < res) res = cost;
        }
      }
    }
  }
  return res;
}

int main() {
  int t, n, m; cin >> t >> n >> m;
  if (t == 0) {
    vector<Edge<ll>> edges;
    while (m--) {
      int u, v, w; cin >> u >> v >> w; --u; --v;
      edges.emplace_back(u, v, w);
    }
    cout << girth_in_undirected_graph(n, edges, LINF) << '\n';
  } else if (t == 1) {
    vector<vector<Edge<ll>>> graph(n);
    while (m--) {
      int u, v, w; cin >> u >> v >> w; --u; --v;
      graph[u].emplace_back(u, v, w);
    }
    cout << girth_in_directed_graph(graph, LINF) << '\n';
  }
  return 0;
}
0