結果

問題 No.1301 Strange Graph Shortest Path
ユーザー PachicobuePachicobue
提出日時 2020-11-27 23:12:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 222 ms / 3,000 ms
コード長 13,665 bytes
コンパイル時間 2,779 ms
コンパイル使用メモリ 218,048 KB
実行使用メモリ 43,272 KB
最終ジャッジ日時 2023-10-09 22:02:14
合計ジャッジ時間 11,223 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 171 ms
40,792 KB
testcase_03 AC 139 ms
36,632 KB
testcase_04 AC 217 ms
38,664 KB
testcase_05 AC 143 ms
41,020 KB
testcase_06 AC 201 ms
36,072 KB
testcase_07 AC 185 ms
37,980 KB
testcase_08 AC 145 ms
37,120 KB
testcase_09 AC 187 ms
34,244 KB
testcase_10 AC 146 ms
36,576 KB
testcase_11 AC 193 ms
37,332 KB
testcase_12 AC 199 ms
36,996 KB
testcase_13 AC 177 ms
40,956 KB
testcase_14 AC 178 ms
34,448 KB
testcase_15 AC 178 ms
35,676 KB
testcase_16 AC 221 ms
38,432 KB
testcase_17 AC 190 ms
41,156 KB
testcase_18 AC 168 ms
37,568 KB
testcase_19 AC 205 ms
36,156 KB
testcase_20 AC 210 ms
34,964 KB
testcase_21 AC 189 ms
39,572 KB
testcase_22 AC 216 ms
35,908 KB
testcase_23 AC 185 ms
40,932 KB
testcase_24 AC 209 ms
35,612 KB
testcase_25 AC 216 ms
38,768 KB
testcase_26 AC 186 ms
37,448 KB
testcase_27 AC 197 ms
37,484 KB
testcase_28 AC 150 ms
40,740 KB
testcase_29 AC 222 ms
37,660 KB
testcase_30 AC 203 ms
38,688 KB
testcase_31 AC 208 ms
38,228 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 92 ms
31,508 KB
testcase_34 AC 214 ms
43,272 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In instantiation of ‘void cost_flow<Cap, Cost>::add_edge(int, int, Cap, Cost) [with Cap = int; Cost = long long int]’:
main.cpp:326:19:   required from here
main.cpp:21:164: 警告: narrowing conversion of ‘(&((cost_flow<int, long long int>*)this)->cost_flow<int, long long int>::edges.std::vector<std::vector<cost_flow<int, long long int>::edge, std::allocator<cost_flow<int, long long int>::edge> >, std::allocator<std::vector<cost_flow<int, long long int>::edge, std::allocator<cost_flow<int, long long int>::edge> > > >::operator[](((std::vector<std::vector<cost_flow<int, long long int>::edge, std::allocator<cost_flow<int, long long int>::edge> >, std::allocator<std::vector<cost_flow<int, long long int>::edge, std::allocator<cost_flow<int, long long int>::edge> > > >::size_type)((int)to))))->std::vector<cost_flow<int, long long int>::edge, std::allocator<cost_flow<int, long long int>::edge> >::size()’ from ‘std::vector<cost_flow<int, long long int>::edge, std::allocator<cost_flow<int, long long int>::edge> >::size_type’ {aka ‘long unsigned int’} to ‘int’ [-Wnarrowing]
   21 |     void add_edge(const int from, const int to, const Cap cap, const Cost cost) { assert(from < sz), assert(to < sz), edges[from].push_back(edge{to, edges[to].size(), cap, cost, false}), edges[to].push_back(edge{from, static_cast<int>(edges[from].size() - 1), 0, -cost, true}); }
      |                                                                                                                                                      ~~~~~~~~~~~~~~^~

ソースコード

diff #

#include <bits/stdc++.h>
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 Cap, typename Cost>
struct cost_flow
{
private:
    struct edge
    {
        int to, rev;
        Cap cap;
        Cost cost;
        const bool is_rev;
    };
    const int sz;
    std::vector<std::vector<edge>> edges;
    std::vector<Cost> pot;
public:
    cost_flow(const int v) : sz(v), edges(v), pot(v, 0) {}
    void add_edge(const int from, const int to, const Cap cap, const Cost cost) { assert(from < sz), assert(to < sz), edges[from].push_back(edge{to, edges[to].size(), cap, cost, false}), edges[to].push_back(edge{from, static_cast<int>(edges[from].size() - 1), 0, -cost, true}); }
    const std::vector<edge>& operator[](const int i) const { return assert(i < sz), edges[i]; }
    std::vector<edge>& operator[](const int i) { return assert(i < sz), edges[i]; }
    void calc_potential(const int s, const bool is_dag = false)
    {
        if (is_dag) {
            std::vector<int> ord;
            std::vector<bool> used(sz, 0);
            auto dfs = [&](auto&& self, const int s) -> void {
                if (not used[s]) {
                    used[s] = true;
                    for (const auto& e : edges[s]) {
                        if (e.cap == 0) { continue; }
                        self(self, e.to);
                    }
                    ord.push_back(s);
                }
            };
            for (int i = 0; i < sz; i++) { dfs(dfs, i); }
            std::reverse(ord.begin(), ord.end());
            std::fill(pot.begin(), pot.end(), inf_v<Cost>);
            for (int i = 0; i < sz; i++) {
                if (ord[i] == s) { pot[s] = 0; }
                if (pot[ord[i]] == inf_v<Cost>) { continue; }
                for (const auto& e : edges[ord[i]]) {
                    if (e.cap == 0) { continue; }
                    pot[e.to] = std::min(pot[e.to], pot[ord[i]] + e.cost);
                }
            }
        } else {
            std::fill(pot.begin(), pot.end(), inf_v<Cost>);
            pot[s] = 0;
            for (int i = 0; i < sz; i++) {
                for (int v = 0; v < sz; v++) {
                    if (pot[v] == inf_v<Cost>) { continue; }
                    for (const auto& e : edges[v]) {
                        if (e.cap == 0) { continue; }
                        if (pot[e.to] <= pot[v] + e.cost) { continue; }
                        pot[e.to] = pot[v] + e.cost;
                        if (i + 1 == sz) { pot[e.to] = -inf_v<Cost>; }
                    }
                }
            }
        }
    }
    template<typename Result>
    std::pair<bool, Result> primal_dual(const int s, const int t, Cap f)
    {
        std::vector<Cost> dist(sz, inf_v<Cost>);
        using P = std::pair<Result, int>;
        std::priority_queue<P, std::vector<P>, std::greater<P>> q;
        std::vector<int> prev_v(sz), prev_e(sz);
        Result ans = 0;
        while (f > 0) {
            std::fill(dist.begin(), dist.end(), inf_v<Cost>);
            dist[s] = 0, q.push({0, s});
            while (not q.empty()) {
                const Result cost = q.top().first;
                const int v = q.top().second;
                q.pop();
                if (dist[v] < cost) { continue; }
                for (int i = 0; i < edges[v].size(); i++) {
                    const auto& e = edges[v][i];
                    const Cost pd = pot[v] - pot[e.to];
                    if (e.cap == 0 or dist[e.to] <= dist[v] + e.cost + pd) { continue; }
                    dist[e.to] = dist[v] + e.cost + pd, prev_v[e.to] = v, prev_e[e.to] = i;
                    q.push({dist[e.to], e.to});
                }
            }
            if (dist[t] == inf_v<Cost>) { return {false, ans}; }
            for (int v = 0; v < sz; v++) { pot[v] += dist[v]; }
            Cap d = f;
            for (int v = t; v != s; v = prev_v[v]) {
                const auto& e = edges[prev_v[v]][prev_e[v]];
                d = std::min(d, e.cap);
            }
            f -= d, ans += static_cast<Result>(d) * static_cast<Result>(pot[t]);
            for (int v = t; v != s; v = prev_v[v]) {
                auto& e = edges[prev_v[v]][prev_e[v]];
                e.cap -= d, edges[v][e.rev].cap += d;
            }
        }
        return {true, ans};
    }
    friend std::ostream& operator<<(std::ostream& os, const cost_flow& cf)
    {
        os << "[\n";
        for (int i = 0; i < cf.size(); i++) {
            for (const auto& e : cf[i]) {
                if (not e.is_rev) { os << i << "->" << e.to << ":"
                                       << "<Cap=" << e.cap << ",Cost=" << e.cost << ">\n"; }
            }
        }
        return (os << "]\n");
    }
};
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 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));
    }
}
template<typename T, std::size_t N>
std::ostream& operator<<(std::ostream& os, const std::array<T, N>& v)
{
    os << "[";
    for (const auto& e : v) { os << e << ","; }
    return (os << "]" << std::endl);
}
template<typename T, typename A>
std::ostream& operator<<(std::ostream& os, const std::deque<T, A>& v)
{
    os << "[";
    for (const auto& e : v) { os << e << ","; }
    return (os << "]" << std::endl);
}
template<typename K, typename T, typename C, typename A>
std::ostream& operator<<(std::ostream& os, const std::map<K, T, C, A>& v)
{
    os << "[";
    for (const auto& e : v) { os << "<" << e.first << ": " << e.second << ">,"; }
    return (os << "]" << std::endl);
}
template<typename K, typename T, typename C, typename A>
std::ostream& operator<<(std::ostream& os, const std::multimap<K, T, C, A>& v)
{
    os << "[";
    for (const auto& e : v) { os << "<" << e.first << ": " << e.second << ">,"; }
    return (os << "]" << std::endl);
}
template<typename T, typename C, typename A>
std::ostream& operator<<(std::ostream& os, const std::multiset<T, C, A>& v)
{
    os << "[";
    for (const auto& e : v) { os << e << ","; }
    return (os << "]" << std::endl);
}
template<typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& v) { return (os << "<" << v.first << "," << v.second << ">"); }
template<typename T1, typename T2, typename T3>
std::ostream& operator<<(std::ostream& os, const std::priority_queue<T1, T2, T3>& v)
{
    auto q = v;
    os << "[";
    while (not q.empty()) { os << q.top() << ",", q.pop(); }
    return os << "]\n";
}
template<typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::queue<T1>& v)
{
    auto q = v;
    os << "[";
    while (not q.empty()) { os << q.front() << ",", q.pop(); }
    return os << "]\n";
}
template<typename T, typename C, typename A>
std::ostream& operator<<(std::ostream& os, const std::set<T, C, A>& v)
{
    os << "[";
    for (const auto& e : v) { os << e << ","; }
    return (os << "]" << std::endl);
}
template<typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::stack<T1>& v)
{
    auto q = v;
    os << "[";
    while (not q.empty()) { os << q.top() << ",", q.pop(); }
    return os << "]\n";
}
template<typename TupType, size_t... I>
std::ostream& print(std::ostream& os, const TupType& _tup, std::index_sequence<I...>) { return os << "(", (..., (os << (I == 0 ? "" : ", ") << std::get<I>(_tup))), os << ")\n"; }
template<typename... T>
std::ostream& operator<<(std::ostream& os, const std::tuple<T...>& _tup) { return print(os, _tup, std::make_index_sequence<sizeof...(T)>()); }
template<typename K, typename T, typename H, typename P, typename A>
std::ostream& operator<<(std::ostream& os, const std::unordered_map<K, T, H, P, A>& v)
{
    os << "[";
    for (const auto& e : v) { os << "<" << e.first << ": " << e.second << ">,"; }
    return (os << "]" << std::endl);
}
template<typename K, typename T, typename H, typename P, typename A>
std::ostream& operator<<(std::ostream& os, const std::unordered_multimap<K, T, H, P, A>& v)
{
    os << "[";
    for (const auto& e : v) { os << "<" << e.first << ": " << e.second << ">,"; }
    return (os << "]" << std::endl);
}
template<typename T, typename H, typename P, typename A>
std::ostream& operator<<(std::ostream& os, const std::unordered_multiset<T, H, P, A>& v)
{
    os << "[";
    for (const auto& e : v) { os << e << ","; }
    return (os << "]" << std::endl);
}
template<typename T, typename H, typename P, typename A>
std::ostream& operator<<(std::ostream& os, const std::unordered_set<T, H, P, A>& v)
{
    os << "[";
    for (const auto& e : v) { os << e << ","; }
    return (os << "]" << std::endl);
}
template<typename T, typename A>
std::ostream& operator<<(std::ostream& os, const std::vector<T, A>& v)
{
    os << "[";
    for (const auto& e : v) { os << e << ","; }
    return (os << "]" << std::endl);
}
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>
void HogeHogeSansuu(T x) { std::cerr << x; }
template<typename T, typename... Args>
void HogeHogeSansuu(T x, Args... args) { std::cerr << x << ",", HogeHogeSansuu(args...); }
int main()
{
    const auto [N, M] = in.tup<int, int>();
    cost_flow<int, ll> f(N);
    for (int i = 0; i < M; i++) {
        const auto [u, v, c, d] = in.tup<int, int, ll, ll>(1, 1, 0, 0);
        f.add_edge(u, v, 1, c), f.add_edge(v, u, 1, c);
        f.add_edge(u, v, 1, d), f.add_edge(v, u, 1, d);
    }
    const ll ans = f.primal_dual<ll>(0, N - 1, 2).second;
    out.ln(ans);
    return 0;
}
0