結果
| 問題 |
No.1320 Two Type Min Cost Cycle
|
| コンテスト | |
| ユーザー |
hitonanode
|
| 提出日時 | 2020-12-17 00:55:01 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 280 ms / 2,000 ms |
| コード長 | 5,105 bytes |
| コンパイル時間 | 2,782 ms |
| コンパイル使用メモリ | 221,512 KB |
| 最終ジャッジ日時 | 2025-01-17 02:12:57 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 57 |
ソースコード
#include <cassert>
#include <limits>
#include <queue>
#include <utility>
#include <vector>
// Shortest cycle detection of UNDIRECTED SIMPLE graphs
// Verified: <https://yukicoder.me/problems/no/1320>
template <typename T> struct ShortestCycleOfUndirectedWeighted {
int V, E;
std::vector<std::vector<std::pair<int, T>>> to; // (nxt, weight)
const T INF;
ShortestCycleOfUndirectedWeighted() = default;
ShortestCycleOfUndirectedWeighted(int V) : V(V), E(0), to(V), INF(std::numeric_limits<T>::max() / 2) {}
void add_edge(int s, int t, T len) {
assert(0 <= s and s < V);
assert(0 <= t and t < V);
assert(len >= 0);
to[s].emplace_back(t, len);
to[t].emplace_back(s, len);
E++;
}
std::vector<T> dist;
std::vector<int> prev;
// Find minimum length simple cycle which passes vertex `v`
// - Complexity: O(E log V)
// - return: (LEN, (a, b))
// - LEN: length of the shortest cycles if exists, INF ( = numeric_limits<int>::max() / 2 ) otherwise.
// - the cycle consists of vertices [v, ..., prev[prev[a]], prev[a], a, b, prev[b], prev[prev[b]], ..., v]
std::pair<T, std::pair<int, int>> Solve(int v) {
assert(0 <= v and v < V);
dist.assign(V, INF), dist[v] = 0;
prev.assign(V, -1);
using P = std::pair<T, std::pair<int, int>>;
std::priority_queue<P, std::vector<P>, std::greater<P>> pq;
std::vector<std::pair<std::pair<int, int>, T>> add_edge;
pq.emplace(0, std::make_pair(v, -1));
while (!pq.empty()) {
const int now = pq.top().second.first, prv = pq.top().second.second;
pq.pop();
for (const auto &nxt : to[now])
if (nxt.first != prv) {
if (dist[nxt.first] == INF) {
dist[nxt.first] = dist[now] + nxt.second;
prev[nxt.first] = now;
pq.emplace(dist[nxt.first], std::make_pair(nxt.first, now));
} else {
add_edge.emplace_back(std::make_pair(now, nxt.first), nxt.second);
}
}
}
T minimum_cycle = INF;
int s = -1, t = -1;
for (auto edge : add_edge) {
int a = edge.first.first, b = edge.first.second;
T L = dist[a] + dist[b] + edge.second;
if (L < minimum_cycle) minimum_cycle = L, s = a, t = b;
}
return std::make_pair(minimum_cycle, std::make_pair(s, t));
}
};
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template <typename T> struct ShortestPath {
int V, E;
int INVALID = -1;
std::vector<std::vector<std::pair<int, T>>> to;
ShortestPath() = default;
ShortestPath(int V) : V(V), E(0), to(V) {}
void add_edge(int s, int t, T len) {
assert(0 <= s and s < V);
assert(0 <= t and t < V);
to[s].emplace_back(t, len);
E++;
}
std::vector<T> dist;
std::vector<int> prev;
// Dijkstra algorithm
// Complexity: O(E log E)
void Dijkstra(int s) {
assert(0 <= s and s < V);
dist.assign(V, std::numeric_limits<T>::max());
dist[s] = 0;
prev.assign(V, INVALID);
using P = std::pair<T, int>;
std::priority_queue<P, std::vector<P>, std::greater<P>> pq;
pq.emplace(0, s);
while (!pq.empty()) {
T d;
int v;
std::tie(d, v) = pq.top();
pq.pop();
if (dist[v] < d) continue;
for (auto nx : to[v]) {
T dnx = d + nx.second;
if (dist[nx.first] > dnx) {
dist[nx.first] = dnx, prev[nx.first] = v;
pq.emplace(dnx, nx.first);
}
}
}
}
};
int main()
{
int T, N, M;
cin >> T >> N >> M;
lint ret = 1e18;
if (T == 1) {
vector<vector<pair<int, int>>> to(N);
while (M--) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
to[u].emplace_back(v, w);
}
REP(s, N) {
ShortestPath<lint> graph(N + 1);
REP(i, N) for (auto [j, w] : to[i]) {
graph.add_edge(i, j, w);
if (j == s) graph.add_edge(i, N, w);
}
graph.Dijkstra(s);
chmin(ret, graph.dist[N]);
}
} else {
ShortestCycleOfUndirectedWeighted<lint> graph(N);
while (M--) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
graph.add_edge(u, v, w);
}
REP(i, N) chmin<lint>(ret, graph.Solve(i).first);
}
cout << (ret < 1e18 ? ret : -1) << '\n';
}
hitonanode