結果
問題 | No.1320 Two Type Min Cost Cycle |
ユーザー |
|
提出日時 | 2020-12-17 00:19:59 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 684 ms / 2,000 ms |
コード長 | 7,040 bytes |
コンパイル時間 | 2,873 ms |
コンパイル使用メモリ | 211,192 KB |
最終ジャッジ日時 | 2025-01-17 02:08:58 |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 57 |
ソースコード
#include <bits/stdc++.h>using ll = long long;using uint = unsigned int;using ull = unsigned long long;using ld = long double;template<typename T> using max_heap = std::priority_queue<T>;template<typename T> using min_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;constexpr int popcount(const ull v) { return v ? __builtin_popcountll(v) : 0; }constexpr int log2p1(const ull v) { return v ? 64 - __builtin_clzll(v) : 0; }constexpr int lsbp1(const ull v) { return __builtin_ffsll(v); }constexpr int clog(const ull v) { return v ? log2p1(v - 1) : 0; }constexpr ull ceil2(const ull v) { return 1ULL << clog(v); }constexpr ull floor2(const ull v) { return v ? (1ULL << (log2p1(v) - 1)) : 0ULL; }constexpr bool btest(const ull mask, const int ind) { return (mask >> ind) & 1ULL; }template<typename T> void bset(T& mask, const int ind) { mask |= ((T)1 << ind); }template<typename T> void breset(T& mask, const int ind) { mask &= ~((T)1 << ind); }template<typename T> void bflip(T& mask, const int ind) { mask ^= ((T)1 << ind); }template<typename T> void bset(T& mask, const int ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); }template<typename T> bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); }template<typename T> bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); }template<typename T> constexpr T inf_v = std::numeric_limits<T>::max() / 4;template<typename Real> constexpr Real pi_v = Real{3.141592653589793238462643383279502884};template<typename T> constexpr T TEN(const int n) { return n == 0 ? T{1} : TEN<T>(n - 1) * T{10}; }template<typename F> struct fix : F{fix(F&& f) : F{std::forward<F>(f)} {}template<typename... Args> auto operator()(Args&&... args) const { return F::operator()(*this, std::forward<Args>(args)...); }};template<typename T, int n, int i = 0>auto nd_array(int const (&szs)[n], const T x = T{}){if constexpr (i == n) {return x;} else {return std::vector(szs[i], nd_array<T, n, i + 1>(szs, x));}}class printer{public:printer(std::ostream& os_ = std::cout) : m_os{os_} { m_os << std::fixed << std::setprecision(15); }template<typename... Args> int ln(const Args&... args) { return dump(args...), m_os << '\n', 0; }template<typename... Args> int el(const Args&... args) { return dump(args...), m_os << std::endl, 0; }private:template<typename T> void dump(const T& v) { m_os << v; }template<typename T> void dump(const std::vector<T>& vs){for (int i = 0; i < (int)vs.size(); i++) { m_os << (i ? " " : ""), dump(vs[i]); }}template<typename T> void dump(const std::vector<std::vector<T>>& vss){for (int i = 0; i < (int)vss.size(); i++) { m_os << (0 <= i or i + 1 < (int)vss.size() ? "\n" : ""), dump(vss[i]); }}template<typename T, typename... Args> int dump(const T& v, const Args&... args) { return dump(v), m_os << ' ', dump(args...), 0; }std::ostream& m_os;};printer out;class scanner{public:scanner(std::istream& is_ = std::cin) : m_is{is_} { m_is.tie(nullptr), std::ios::sync_with_stdio(false); }template<typename T> T val(){T v;return m_is >> v, v;}template<typename T> T val(const T offset) { return val<T>() - offset; }template<typename T> std::vector<T> vec(const int n){return make_v<T>(n, [this]() { return val<T>(); });}template<typename T> std::vector<T> vec(const int n, const T offset){return make_v<T>(n, [this, offset]() { return val<T>(offset); });}template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1){return make_v<std::vector<T>>(n0, [this, n1]() { return vec<T>(n1); });}template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1, const T offset){return make_v<std::vector<T>>(n0, [this, n1, offset]() { return vec<T>(n1, offset); });}template<typename... Args> auto tup() { return std::tuple<std::decay_t<Args>...>{val<Args>()...}; }template<typename... Args> auto tup(const Args&... offsets) { return std::tuple<std::decay_t<Args>...>{val<Args>(offsets)...}; }private:template<typename T, typename F>std::vector<T> make_v(const int n, F f){std::vector<T> ans;for (int i = 0; i < n; i++) { ans.push_back(f()); }return ans;}std::istream& m_is;};scanner in;template<typename T = int>class graph{public:graph(const int size) : m_size{size}, m_eis(m_size) {}void add_edge(const int u, const int v, const T& c, const bool bi = false){const int ei = (int)m_us.size();m_eis[u].push_back(ei);if (bi) { m_eis[v].push_back(ei); }m_us.push_back(u), m_vs.push_back(v), m_cs.push_back(c);}void add_edge(const int u, const int v, const bool bi = false) { add_edge(u, v, 1, bi); }const std::vector<int>& operator[](const int u) const { return m_eis[u]; }std::vector<int>& operator[](const int u) { return m_eis[u]; }std::tuple<int, int, T> edge(const int u, const int i) const { return std::make_tuple(u, m_us[i] ^ m_vs[i] ^ u, m_cs[i]); }int size() const { return m_size; }friend std::ostream& operator<<(std::ostream& os, const graph& g){for (int u = 0; u < g.size(); u++) {for (const int ei : g[u]) {const auto& [from, to, cost] = g.edge(u, ei);os << "[" << ei << "]: " << from << "->" << to << "(" << cost << ")\n";}}return os;}private:int m_size;std::vector<std::vector<int>> m_eis;std::vector<int> m_us, m_vs;std::vector<T> m_cs;};template<typename T>std::vector<T> dijkstra(const graph<T>& g, const int s){std::vector<T> ds(g.size(), inf_v<T>);using P = std::pair<T, int>;std::priority_queue<P, std::vector<P>, std::greater<P>> Q;ds[s] = 0, Q.push({0, s});while (not Q.empty()) {const auto [cost, u] = Q.top();Q.pop();if (ds[u] < cost) { continue; }for (const int ei : g[u]) {[[maybe_unused]] const auto& [from, to, cost] = g.edge(u, ei);if (ds[to] <= ds[from] + cost) { continue; }ds[to] = ds[from] + cost, Q.push({ds[to], to});}}return ds;}int main(){const auto T = in.val<int>();const auto [N, M] = in.tup<int, int>();std::vector<int> us(M), vs(M);std::vector<ll> cs(M);for (int i = 0; i < M; i++) { std::tie(us[i], vs[i], cs[i]) = in.tup<int, int, ll>(1, 1, 0); }ll ans = inf_v<ll>;for (int i = 0; i < M; i++) {const int u = us[i], v = vs[i];const ll c = cs[i];graph<ll> g(N);for (int j = 0; j < M; j++) {if (i == j) { continue; }g.add_edge(us[j], vs[j], cs[j], T == 0);}chmin(ans, c + dijkstra(g, v)[u]);}out.ln(ans == inf_v<ll> ? -1 : ans);return 0;}