結果

問題 No.807 umg tours
ユーザー goodbaton
提出日時 2019-03-22 23:25:11
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 631 ms / 4,000 ms
コード長 2,302 bytes
コンパイル時間 1,211 ms
コンパイル使用メモリ 112,048 KB
実行使用メモリ 45,656 KB
最終ジャッジ日時 2024-11-23 19:09:59
合計ジャッジ時間 8,186 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

/* Dijkstra O(NlogM)*/

template <typename Type = int>
struct Dijkstra{
  int V;
  vector<vector<pair<int,Type>>> G;
  vector<Type> cost;

  Dijkstra(int n):
    V(n), G(n, vector<pair<int,Type>>()) {}

  void add_edge(int u, int v, Type c){
    G[u].push_back({v, c});
  }

  Type solve(int s, int g = -1){
    cost.assign(V, -1);
    priority_queue<pair<Type,int>> pq;
    Type max_cost = 0;

    pq.push({0, s});

    while(pq.size()){
      Type now_cost = pq.top().first;
      int now = pq.top().second;
      pq.pop();

      if(cost[now] >= 0) continue;

      cost[now] = -now_cost;
      max_cost = max(max_cost, -now_cost);

      if(now == g) return -now_cost;

      for(int i=0; i<(int)G[now].size(); i++)
        pq.push({now_cost - G[now][i].second, G[now][i].first});
    }

    return max_cost;
  }
};

int main(){
  int n, m;

  scanf("%d%d", &n, &m);

  Dijkstra<ll> dijk(n*2);

  for(int i=0;i<m;i++){
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--; b--;

    dijk.add_edge(a, b, c);
    dijk.add_edge(b, a, c);
    dijk.add_edge(a+n, b+n, c);
    dijk.add_edge(b+n, a+n, c);
    dijk.add_edge(a, b+n, 0);
    dijk.add_edge(b, a+n, 0);
  }

  dijk.add_edge(0, n, 0);


  dijk.solve(0);

  for(int i=0;i<n;i++){
    printf("%lld\n", dijk.cost[i] + dijk.cost[i+n]);
  }

  return 0;
}
0