結果

問題 No.872 All Tree Path
ユーザー uw_yu1rabbituw_yu1rabbit
提出日時 2020-08-31 19:10:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 3,406 bytes
コンパイル時間 1,834 ms
コンパイル使用メモリ 107,620 KB
実行使用メモリ 814,588 KB
最終ジャッジ日時 2024-04-28 08:23:58
合計ジャッジ時間 5,298 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using ll = i64;
#define rep(i, n) for (i32 i = 0; i < (i32)(n); i++)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
using P = pair<i64,i64>;

  ll INF = 1145141919810364;
  template <typename T>
  class edge
  {
  public:
      T cost, to, from;
      edge(T from, T to, T cost) : from(from), to(to), cost(cost) {}
  };
  template <typename T>
  class Graph
  {
  public:
      vector<vector<edge<T>>> graph;

      Graph(T n, vector<T> from, vector<T> to, bool no_direction, vector<T> cost)
      {
          graph.resize(n);
          for (int i = 0; i < from.size(); i++)
          {
              edge<T> params(from[i], to[i], cost[i]);
              graph[from[i]].emplace_back(params);
              if (no_direction)
              {
                  swap(params.from, params.to);
                  graph[to[i]].emplace_back(params);
              }
          }
      }
      vector<ll> shortest_path;
      void Dijkstra(ll start)
      {
          shortest_path.resize(graph.size(), INF);
          shortest_path[start] = 0;
          priority_queue<P, vector<P>, greater<P>> que;
          que.emplace(0, start);
          while (!que.empty())
          {
              P p = que.top();
              que.pop();
              ll v = p.second;
              if (shortest_path[v] < p.first)
                  continue;
              for (int i = 0; i < graph[v].size(); i++)
              {
                  ll k = graph[v][i].to;
                  ll l = graph[v][i].cost;
                  if (shortest_path[k] > shortest_path[v] + l)
                  {
                      shortest_path[k] = shortest_path[v] + l;
                      que.push(P(shortest_path[k], k));
                  }
              }
          }
      }
      vector<vector<T>> Shortest_path;
      void Warshall_Froyd()
      {
          Shortest_path.resize(graph.size(), vector<T>(graph.size(), INF));
          for (int i = 0; i < graph.size(); i++)
          {
              for (int j = 0; j < graph[i].size(); j++)
              {
                  ll To = graph[i][j].to;
                  ll Cost = graph[i][j].cost;
                  Shortest_path[i][To] = Cost;
              }
          }
          for (int k = 0; k < graph.size(); k++)
          {
              for (int i = 0; i < graph.size(); i++)
              {
                  for (int j = 0; j < graph.size(); j++)
                  {
                      Shortest_path[i][j] = min(Shortest_path[k][j] + Shortest_path[i][k], Shortest_path[i][j]);
                  }
              }
          }
      }
      vector<T> dpath(T t)
      {
          vector<T> path;
          for (; t != INF; t = shortest_path[t])
              path.emplace_back(t);
          reverse(path.begin(), path.end());
          return path;
      }
  };
int main(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
i64 n;
cin >> n;
vector<i64> u(n - 1),v(n - 1),w(n - 1);
rep(i,n - 1){
    cin >> u[i] >> v[i] >> w[i];
    u[i]--;
    v[i]--;
}
Graph g(n,u,v,true,w);
g.Warshall_Froyd();
i64 ans = 0;
rep(i,n){
    g.Dijkstra(i);
    rep(j,n){
        if(i != j)ans += g.shortest_path[j];
    }
}
cout << ans << endl;
}
0