結果
| 問題 |
No.1301 Strange Graph Shortest Path
|
| コンテスト | |
| ユーザー |
scol_kp
|
| 提出日時 | 2020-11-28 00:37:10 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 165 ms / 3,000 ms |
| コード長 | 7,346 bytes |
| コンパイル時間 | 2,068 ms |
| コンパイル使用メモリ | 213,544 KB |
| 最終ジャッジ日時 | 2025-01-16 08:55:05 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 33 |
ソースコード
#include <bits/stdc++.h>
#define FASTIO
using namespace std;
using ll = long long;
using Vi = std::vector<int>;
using Vl = std::vector<ll>;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;
constexpr int I_INF = std::numeric_limits<int>::max();
constexpr ll L_INF = std::numeric_limits<ll>::max();
template <typename T1, typename T2>
inline bool chmin(T1& a, const T2& b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, const T2& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <ostream& os = std::cout>
class Prints {
private:
class __Prints {
public:
__Prints(const char* sep, const char* term) : sep(sep), term(term) {}
template <class... Args>
auto operator()(const Args&... args) const -> decltype((os << ... << std::declval<Args>()), void()) { print(args...); }
template <typename T>
auto pvec(const T& vec, size_t sz) const -> decltype(os << std::declval<decltype(std::declval<T>()[0])>(), void()) {
for (size_t i = 0; i < sz; i++)
os << vec[i] << (i == sz - 1 ? term : sep);
}
template <typename T>
auto pmat(const T& mat, size_t h, size_t w) -> decltype(os << std::declval<decltype(std::declval<T>()[0][0])>(), void()) {
for (size_t i = 0; i < h; i++)
for (size_t j = 0; j < w; j++)
os << mat[i][j] << (j == w - 1 ? term : sep);
}
private:
const char *sep, *term;
void print() const { os << term; }
void print_rest() const { os << term; }
template <class T, class... Tail>
void print(const T& head, const Tail&... tail) const { os << head, print_rest(tail...); }
template <class T, class... Tail>
void print_rest(const T& head, const Tail&... tail) const { os << sep << head, print_rest(tail...); }
};
public:
Prints() {}
__Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); }
};
Prints<> prints;
Prints<std::cerr> prints_err;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
template <typename Cap, typename Cost>
class MinCostFlowGraph {
private:
struct _Edge {
int to, rev;
Cap cap;
Cost cost;
_Edge() noexcept {}
_Edge(int to, Cap cap, Cost cost, int rev) noexcept
: to(to), rev(rev), cap(cap), cost(cost) {}
};
std::vector<std::vector<_Edge>> g;
std::vector<std::pair<int, int>> pos;
public:
MinCostFlowGraph(int n) noexcept : g(n) {}
int add_edge(int from, int to, Cap cap, Cost cost) noexcept {
const int m = static_cast<int>(pos.size());
const int from_id = static_cast<int>(g[from].size());
int to_id = static_cast<int>(g[to].size());
pos.emplace_back(from, static_cast<int>(g[from].size()));
if (from == to) ++to_id;
g[from].emplace_back(to, cap, cost, to_id);
g[to].emplace_back(from, 0, -cost, from_id);
return m;
}
struct Edge {
int from, to;
Cap cap, flow;
Cost cost;
Edge() noexcept {}
Edge(int from, int to, Cap cap, Cap flow, Cost cost) noexcept
: from(from), to(to), cap(cap), flow(flow), cost(cost) {}
};
Edge get_edge(int i) const noexcept {
const auto e = g[pos[i].first][pos[i].second];
const auto re = g[e.to][e.rev];
return Edge(pos[i].first, e.to, e.cap + re.cap, re.cap, e.cost);
}
std::vector<Edge> edges() const noexcept {
const int m = static_cast<int>(pos.size());
std::vector<Edge> res;
res.reserve(m);
for (int i = 0; i < m; i++) res.emplace_back(get_edge(i));
return res;
}
std::pair<Cap, Cost> flow(int s, int t) noexcept {
return flow(s, t, std::numeric_limits<Cap>::max());
}
std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) noexcept {
return slope(s, t, flow_limit).back();
}
std::vector<std::pair<Cap, Cost>> slope(int s, int t) noexcept {
return slope(s, t, std::numeric_limits<Cap>::max());
}
std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) noexcept {
constexpr Cost C_INF = std::numeric_limits<Cost>::max();
const int n = static_cast<int>(g.size());
std::vector<Cost> h(n), dist(n);
std::vector<int> prevv(n), preve(n);
std::vector<std::pair<Cap, Cost>> res;
struct Q {
Cost key;
int to;
Q(Cost key, int to)
noexcept : key(key), to(to) {}
bool operator>(const Q& r) const noexcept { return key > r.key; }
};
Cap flow = 0;
Cost cost = 0, prev_cost = -1;
res.emplace_back(flow, cost);
while (flow < flow_limit) {
std::priority_queue<Q, std::vector<Q>, std::greater<Q>> que;
dist.assign(n, C_INF);
dist[s] = 0;
que.emplace(0, s);
while (!que.empty()) {
auto [d, v] = que.top();
que.pop();
if (dist[v] < d) continue;
const int m = static_cast<int>(g[v].size());
for (int i = 0; i < m; i++) {
const _Edge& e = g[v][i];
Cost cn = dist[v] + e.cost + h[v] - h[e.to];
if (e.cap > 0 && dist[e.to] > cn) {
dist[e.to] = cn;
prevv[e.to] = v;
preve[e.to] = i;
que.emplace(cn, e.to);
}
}
}
if (dist[t] == C_INF) break;
for (int i = 0; i < n; i++) h[i] += dist[i];
Cap f = flow_limit - flow;
for (int i = t; i != s; i = prevv[i]) {
f = std::min(f, g[prevv[i]][preve[i]].cap);
}
for (int i = t; i != s; i = prevv[i]) {
_Edge& e = g[prevv[i]][preve[i]];
e.cap -= f;
g[i][e.rev].cap += f;
}
Cost d = h[t];
flow += f;
cost += f * d;
if (prev_cost == d) res.pop_back();
res.emplace_back(flow, cost);
prev_cost = d;
}
return res;
}
};
void solve() {
int N, M;
cin >> N >> M;
MinCostFlowGraph<ll, ll> mcf(N);
for (ll i = 0; i < M; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
--a, --b;
mcf.add_edge(a, b, 1, c);
mcf.add_edge(a, b, 1, d);
mcf.add_edge(b, a, 1, c);
mcf.add_edge(b, a, 1, d);
}
auto flw = mcf.flow(0, N - 1, 2);
prints()(flw.second);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int main() {
#ifdef FASTIO
std::cin.tie(nullptr), std::cout.tie(nullptr);
std::ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
std::ifstream ifs("./in_out/input.txt");
std::cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
std::ofstream ofs("./in_out/output.txt");
std::cout.rdbuf(ofs.rdbuf());
#endif
std::cout << std::setprecision(18) << std::fixed;
solve();
std::cout << std::flush;
return 0;
}
scol_kp