結果
| 問題 |
No.1320 Two Type Min Cost Cycle
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2020-12-19 04:17:27 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,870 bytes |
| コンパイル時間 | 2,495 ms |
| コンパイル使用メモリ | 216,412 KB |
| 最終ジャッジ日時 | 2025-01-17 03:47:19 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 56 WA * 1 |
ソースコード
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
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()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
IOSetup() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << fixed << setprecision(20);
}
} iosetup;
template <typename CostType>
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 &x) const {
return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src;
}
inline bool operator<=(const Edge &x) const { return !(x < *this); }
inline bool operator>(const Edge &x) const { return x < *this; }
inline bool operator>=(const Edge &x) const { return !(*this < x); }
};
template <typename CostType>
CostType girth_in_directed_graph(const std::vector<std::vector<Edge<CostType>>> &graph) {
int n = graph.size();
CostType res = -1;
std::vector<CostType> dist(n);
std::priority_queue<Edge<CostType>, std::vector<Edge<CostType>>, std::greater<Edge<CostType>>> que;
for (int root = 0; root < n; ++root) {
std::fill(dist.begin(), dist.end(), -1);
dist[root] = 0;
for (const Edge<CostType> &e : graph[root]) {
if (e.dst == root) {
if (res == -1 || e.cost < res) res = e.cost;
} else {
que.emplace(e);
}
}
while (!que.empty()) {
Edge<CostType> edge = que.top(); que.pop();
if (dist[edge.dst] != -1) {
if (edge.dst == root && (res == -1 || dist[edge.src] + edge.cost < res)) res = dist[edge.src] + edge.cost;
continue;
}
dist[edge.dst] = dist[edge.src] + edge.cost;
for (const Edge<CostType> &e : graph[edge.dst]) {
if (dist[e.dst] != -1) {
if (e.dst == root && (res == -1 || dist[edge.dst] + e.cost < res)) res = dist[edge.dst] + e.cost;
} else {
que.emplace(e);
}
}
}
}
return res;
}
template <typename CostType>
CostType girth_in_undirected_graph(int n, const std::vector<Edge<CostType>> &edges) {
int m = edges.size();
std::vector<std::vector<int>> graph(n);
for (int i = 0; i < m; ++i) {
graph[edges[i].src].emplace_back(i);
graph[edges[i].dst].emplace_back(i);
}
std::vector<bool> used(m, false);
std::vector<CostType> dist(n);
using P = std::pair<int, int>;
std::priority_queue<P, std::vector<P>, std::function<bool(const P&, const P&)>> que(
[&](const P &a, const P &b) {
const Edge<CostType> &a_edge = edges[a.first], &b_edge = edges[b.first];
return a_edge.cost != b_edge.cost ? a_edge.cost > b_edge.cost : a_edge.dst != b_edge.dst ? a_edge.dst > b_edge.dst : a_edge.src > b_edge.src;
}
);
CostType res = -1;
for (int root = 0; root < n; ++root) {
std::fill(used.begin(), used.end(), false);
std::fill(dist.begin(), dist.end(), -1);
dist[root] = 0;
for (int id : graph[root]) {
int dst = edges[id].src == root ? edges[id].dst : edges[id].src;
if (dst != root) que.emplace(id, dst);
}
while (!que.empty()) {
int id, dst; std::tie(id, dst) = que.top(); que.pop();
if (dist[dst] != -1) continue;
int src = edges[id].dst == dst ? edges[id].src : edges[id].dst;
dist[dst] = dist[src] + edges[id].cost;
used[id] = true;
for (int e : graph[dst]) {
int nx = edges[e].src == dst ? edges[e].dst : edges[e].src;
if (dist[nx] == -1) que.emplace(e, nx);
}
}
for (int i = 0; i < m; ++i) {
if (!used[i] && dist[edges[i].src] != -1 && dist[edges[i].dst] != -1) {
CostType loop = dist[edges[i].src] + dist[edges[i].dst] + edges[i].cost;
if (res == -1 || loop < res) res = loop;
}
}
}
return res;
}
int main() {
int t, n, m; cin >> t >> n >> m;
if (t == 0) {
vector<Edge<ll>> edges;
while (m--) {
int u, v, w; cin >> u >> v >> w; --u; --v;
edges.emplace_back(u, v, w);
}
cout << girth_in_undirected_graph(n, edges) << '\n';
} else if (t == 1) {
vector<vector<Edge<ll>>> graph(n);
while (m--) {
int u, v, w; cin >> u >> v >> w; --u; --v;
graph[u].emplace_back(u, v, w);
}
cout << girth_in_directed_graph(graph) << '\n';
}
return 0;
}
emthrm