結果

問題 No.1301 Strange Graph Shortest Path
ユーザー 👑 jupirojupiro
提出日時 2020-12-02 17:44:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 211 ms / 3,000 ms
コード長 3,249 bytes
コンパイル時間 2,937 ms
コンパイル使用メモリ 144,980 KB
実行使用メモリ 43,232 KB
最終ジャッジ日時 2023-10-11 11:50:26
合計ジャッジ時間 14,131 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 162 ms
40,880 KB
testcase_03 AC 131 ms
36,676 KB
testcase_04 AC 206 ms
38,704 KB
testcase_05 AC 137 ms
40,916 KB
testcase_06 AC 181 ms
36,036 KB
testcase_07 AC 168 ms
38,064 KB
testcase_08 AC 137 ms
37,184 KB
testcase_09 AC 174 ms
34,260 KB
testcase_10 AC 139 ms
36,632 KB
testcase_11 AC 185 ms
37,208 KB
testcase_12 AC 189 ms
37,080 KB
testcase_13 AC 168 ms
40,972 KB
testcase_14 AC 167 ms
34,416 KB
testcase_15 AC 168 ms
35,748 KB
testcase_16 AC 208 ms
38,552 KB
testcase_17 AC 175 ms
41,224 KB
testcase_18 AC 157 ms
37,672 KB
testcase_19 AC 181 ms
36,212 KB
testcase_20 AC 185 ms
34,792 KB
testcase_21 AC 178 ms
39,528 KB
testcase_22 AC 191 ms
35,876 KB
testcase_23 AC 172 ms
40,848 KB
testcase_24 AC 188 ms
35,652 KB
testcase_25 AC 208 ms
38,772 KB
testcase_26 AC 181 ms
37,588 KB
testcase_27 AC 188 ms
37,328 KB
testcase_28 AC 145 ms
40,844 KB
testcase_29 AC 211 ms
37,780 KB
testcase_30 AC 199 ms
38,664 KB
testcase_31 AC 204 ms
38,136 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 90 ms
31,616 KB
testcase_34 AC 200 ms
43,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <utility>
#include <fstream>

using namespace std;
typedef long long ll;
using ull = unsigned long long;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); }return a; }
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
//constexpr long long mod = 1000000007LL;
constexpr long long  mod = 998244353;

struct PrimalDual {
	struct edge {
		int to;
		ll cap;
		ll cost;
		int rev;
		bool isrev;

		edge(int _to, ll _cap, ll _cost, int _rev, bool _isrev) :to(_to), cap(_cap), cost(_cost), rev(_rev), isrev(_isrev) {}

	};
	vector<vector<edge>> graph;
	vector<ll> potential, min_cost;
	vector<int> prevv, preve;
	PrimalDual(int V) :graph(V) {}

	void add_edge(int from, int to, ll cap, ll cost) {
		graph[from].emplace_back(to, cap, cost, graph[to].size(), false);
		graph[to].emplace_back(from, 0, -cost, graph[from].size() - 1, true);
	}

	ll min_cost_flow(int s, int t, ll f) {
		int V = graph.size();
		ll res = 0;
		priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;
		potential.assign(V, 0);
		preve.assign(V, -1);
		prevv.assign(V, -1);

		while (f > 0) {
			min_cost.assign(V, INF);
			pq.emplace(0, s);
			min_cost[s] = 0;
			while (!pq.empty()) {
				auto cur = pq.top();
				pq.pop();
				if (min_cost[cur.second] < cur.first) continue;
				for (int i = 0; i < graph[cur.second].size(); i++) {
					edge& e = graph[cur.second][i];
					ll nextCost = min_cost[cur.second] + e.cost + potential[cur.second] - potential[e.to];
					if (e.cap > 0 and min_cost[e.to] > nextCost) {
						min_cost[e.to] = nextCost;
						prevv[e.to] = cur.second;
						preve[e.to] = i;
						pq.emplace(min_cost[e.to], e.to);
					}

				}
			}
			if (min_cost[t] == INF) return -1;
			for (int v = 0; v < V; v++) potential[v] += min_cost[v];
			ll add_flow = f;
			for (int v = t; v != s; v = prevv[v]) {
				chmin(add_flow, graph[prevv[v]][preve[v]].cap);
			}
			f -= add_flow;
			res += add_flow * potential[t];
			for (int v = t; v != s; v = prevv[v]) {
				edge& e = graph[prevv[v]][preve[v]];
				e.cap -= add_flow;
				graph[v][e.rev].cap += add_flow;
			}
		}
		return res;
	}
};
int main()
{

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int n, m; cin >> n >> m;
	PrimalDual g(n);
	for (int i = 0; i < m; i++) {
		int u, v, c, d; cin >> u >> v >> c >> d;
		u--; v--;
		g.add_edge(u, v, 1, c);
		g.add_edge(u, v, 1, d);
		g.add_edge(v, u, 1, c);
		g.add_edge(v, u, 1, d);
	}
	cout << g.min_cost_flow(0, n - 1, 2) << endl;
}
0