結果
| 問題 |
No.807 umg tours
|
| コンテスト | |
| ユーザー |
Tiramister
|
| 提出日時 | 2019-03-22 23:02:55 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 597 ms / 4,000 ms |
| コード長 | 2,041 bytes |
| コンパイル時間 | 1,152 ms |
| コンパイル使用メモリ | 90,860 KB |
| 実行使用メモリ | 44,456 KB |
| 最終ジャッジ日時 | 2024-11-23 19:07:50 |
| 合計ジャッジ時間 | 8,329 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include <iostream>
#include <queue>
#include <vector>
#include <tuple>
#include <limits>
using ll = long long;
template <class T>
struct Edge {
int from, to;
T cost;
Edge(int from = -1, int to = -1, T cost = 1) : from(from), to(to), cost(cost){};
bool operator<(const Edge<T>& e) const { return this->cost < e.cost; }
bool operator>(const Edge<T>& e) const { return this->cost > e.cost; }
};
template <class T = int>
class Graph {
public:
explicit Graph(int N = 0) : size(N) { path.resize(size); }
void span(int u, int v, T cost = 1) { path[u].push_back(Edge<T>(u, v, cost)); }
std::vector<Edge<T>> operator[](int v) const { return path[v]; }
int size;
std::vector<std::vector<Edge<T>>> path;
};
const ll INF = std::numeric_limits<ll>::max();
template <class T>
std::vector<T> dijkstra(const Graph<T>& graph, std::vector<int> ss) {
std::vector<T> dist(graph.size, INF);
std::priority_queue<std::pair<int, T>, std::vector<std::pair<int, T>>, std::greater<std::pair<int, T>>> que;
for (auto s : ss) {
dist[s] = 0;
que.emplace(0, s);
}
while (!que.empty()) {
int v;
T d;
std::tie(d, v) = que.top();
que.pop();
if (d > dist[v]) continue;
for (const auto& e : graph[v]) {
if (dist[e.to] <= dist[v] + e.cost) continue;
dist[e.to] = dist[v] + e.cost;
que.emplace(dist[e.to], e.to);
}
}
return dist;
}
int main() {
int N, M;
std::cin >> N >> M;
Graph<ll> graph(N * 2);
for (int i = 0; i < M; ++i) {
int u, v;
ll c;
std::cin >> u >> v >> c;
--u, --v;
graph.span(u, v, c);
graph.span(v, u, c);
graph.span(u, v + N, 0);
graph.span(v, u + N, 0);
graph.span(u + N, v + N, c);
graph.span(v + N, u + N, c);
}
auto dist = dijkstra<ll>(graph, {0, N});
for (int v = 0; v < N; ++v) {
std::cout << dist[v] + dist[v + N] << std::endl;
}
return 0;
}
Tiramister