結果

問題 No.807 umg tours
ユーザー 👑 emthrmemthrm
提出日時 2019-03-22 21:59:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,825 bytes
コンパイル時間 1,617 ms
コンパイル使用メモリ 143,492 KB
実行使用メモリ 32,460 KB
最終ジャッジ日時 2023-10-19 09:09:24
合計ジャッジ時間 5,844 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 WA -
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 WA -
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 144 ms
19,184 KB
testcase_21 AC 148 ms
19,764 KB
testcase_22 AC 55 ms
10,532 KB
testcase_23 AC 39 ms
8,964 KB
testcase_24 AC 106 ms
25,032 KB
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
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()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = 1000000007; // 998244353
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
using CostType = long long;
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 &rhs) const {
    return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src;
  }
  inline bool operator<=(const Edge &rhs) const { return cost <= rhs.cost; }
  inline bool operator>(const Edge &rhs) const {
    return cost != rhs.cost ? cost > rhs.cost : dst != rhs.dst ? dst > rhs.dst : src > rhs.src;
  }
  inline bool operator>=(const Edge &rhs) const { return cost >= rhs.cost; }
};

struct Dijkstra {
  Dijkstra(const vector<vector<Edge> > &graph_, const CostType CINF_ = LINF) : graph(graph_), CINF(CINF_) {}

  vector<CostType> build(int s) {
    int sz = graph.size();
    vector<CostType> dist(sz, CINF);
    dist[s] = 0;
    prev.assign(sz, -1);
    using Pci = pair<CostType, int>;
    priority_queue<Pci, vector<Pci>, greater<Pci> > que;
    que.emplace(0, s);
    while (!que.empty()) {
      Pci pr = que.top(); que.pop();
      int ver = pr.second;
      if (dist[ver] < pr.first) continue;
      for (Edge e : graph[ver]) {
        if (dist[e.dst] > dist[ver] + e.cost) {
          dist[e.dst] = dist[ver] + e.cost;
          prev[e.dst] = ver;
          que.emplace(dist[e.dst], e.dst);
        }
      }
    }
    return dist;
  }

  vector<CostType> build2(int s) {
    int sz = graph.size();
    vector<CostType> dist(sz, CINF);
    dist[s] = 0;
    prev.assign(sz, -1);
    using P = pair<CostType, pair<CostType, int> >;
    priority_queue<P, vector<P>, greater<P> > que;
    que.push({0, {0, s}});
    while (!que.empty()) {
      P pr = que.top(); que.pop();
      CostType di = pr.first, hiku = pr.second.first;
      int ver = pr.second.second;
      if (dist[ver] < di) continue;
      for (Edge e : graph[ver]) {
        if (e.cost > hiku) {
          CostType nx = di + hiku;
          if (dist[e.dst] > nx) {
            dist[e.dst] = nx;
            que.push({nx, {e.cost, e.dst}});
          }
        } else {
          CostType nx = di + e.cost;
          if (dist[e.dst] > nx) {
            dist[e.dst] = nx;
            que.push({nx, {hiku, e.dst}});
          }
        }
      }
    }
    return dist;
  }

  vector<int> build_path(int t) {
    vector<int> res;
    for (; t != -1; t = prev[t]) res.emplace_back(t);
    reverse(ALL(res));
    return res;
  }

private:
  vector<vector<Edge> > graph;
  const CostType CINF;
  vector<int> prev;
};

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n, m; cin >> n >> m;
  vector<vector<Edge> > graph(n);
  while (m--) {
    int a, b, c; cin >> a >> b >> c; --a; --b;
    graph[a].emplace_back(Edge(a, b, c));
    graph[b].emplace_back(Edge(b, a, c));
  }
  Dijkstra dij(graph);
  vector<long long> iki = dij.build(0), kaeri = dij.build2(0);
  REP(i, n) cout << iki[i] + kaeri[i] << '\n';
  return 0;
}
0