結果

問題 No.654 Air E869120
ユーザー f1b_maxbl00df1b_maxbl00d
提出日時 2020-09-19 13:12:09
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 4,252 bytes
コンパイル時間 1,952 ms
コンパイル使用メモリ 153,704 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-05 17:01:22
合計ジャッジ時間 4,411 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 20 ms
4,380 KB
testcase_11 AC 14 ms
4,376 KB
testcase_12 AC 15 ms
4,380 KB
testcase_13 AC 18 ms
4,376 KB
testcase_14 AC 13 ms
4,376 KB
testcase_15 AC 14 ms
4,376 KB
testcase_16 AC 12 ms
4,380 KB
testcase_17 AC 14 ms
4,380 KB
testcase_18 AC 12 ms
4,376 KB
testcase_19 AC 12 ms
4,376 KB
testcase_20 AC 5 ms
4,380 KB
testcase_21 AC 5 ms
4,376 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 5 ms
4,376 KB
testcase_24 AC 5 ms
4,380 KB
testcase_25 AC 4 ms
4,380 KB
testcase_26 AC 4 ms
4,376 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 4 ms
4,376 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 3 ms
4,380 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 3 ms
4,380 KB
testcase_33 AC 3 ms
4,380 KB
testcase_34 AC 3 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,384 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#pragma warning(disable:4996)
//#include <Windows.h>
#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <random>
#include <functional>
#include <stack>
#include <iomanip>
#include <cassert>
//#include <boost/multiprecision/cpp_int.hpp>
#include <complex>
#include <cstdio>
#include <list>
#include <bitset>
//#include <stdio.h>

//< in.txt > out.txt
using namespace std;
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
typedef long long LL;
typedef long double LD;
//typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PI;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef unsigned long long ULL;
template<class T>
using pqueue = priority_queue<T, vector<T>, function<bool(T, T)>>;

template<class T>
inline void chmin(T& a, T b) {
	a = min(a, b);
}

template<class T>
inline void chmax(T& a, T b) {
	a = max(a, b);
}

void input();
void solve();

void daminput();
void naive();

void outputinput();

int main() {
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	input();
	//daminput();
	solve();
	//naive();
	//outputinput();
	return 0;
}

//////////////////////////////////////////////////
//////////////////////////////////////////////////

int N, M, d;
VI U, V, P, Q, W;

void input() {
	cin >> N >> M >> d;
	U.resize(M);
	V.resize(M);
	P.resize(M);
	Q.resize(M);
	W.resize(M);
	for (int m = 0; m < M; m++) {
		cin >> U[m] >> V[m] >> P[m] >> Q[m] >> W[m];
	}
}

void daminput() {
}

template<class F>
class MaxFEdge {
public:
	int to;
	F cap;
	int rev;
	MaxFEdge(int _to, F _cap, int _rev) :to(_to), cap(_cap), rev(_rev) {}
};

template<class F>
class Dinic {
public:
	int V;
	vector<vector<MaxFEdge<F>>> G;
	VI itr, level;
	Dinic(int _v) :V(_v) {
		G.resize(V);
	}
	void AddEdge(int src, int to, F cap) {
		G[src].push_back(MaxFEdge<F>(to, cap, G[to].size()));
		G[to].push_back(MaxFEdge<F>(src, 0, G[src].size() - 1));
	}
	void bfs(int s) {
		level.assign(V, -1);
		queue<int> q;
		level[s] = 0;
		q.push(s);
		while (!q.empty()) {
			int v = q.front();
			q.pop();
			for (auto& e : G[v]) {
				if (e.cap > 0 && level[e.to] < 0) {
					level[e.to] = level[v] + 1;
					q.push(e.to);
				}
			}
		}
	}
	F dfs(int v, int t, F f) {
		if (v == t)return f;
		F res = 0;
		for (int& i = itr[v]; i < (int)G[v].size(); i++) {
			MaxFEdge<F>& e = G[v][i];
			if (e.cap > 0 && level[v] < level[e.to]) {
				F d = dfs(e.to, t, min(f, e.cap));
				if (d > 0) {
					e.cap -= d;
					G[e.to][e.rev].cap += d;
					res += d;
					f -= d;
				}
			}
		}
		return res;
	}
	F MaxFlow(int s, int t) {
		F res = 0;
		F f = 0;
		F inf = numeric_limits<F>::max();
		while (true) {
			bfs(s);
			if (level[t] < 0)return res;
			itr.assign(V, 0);
			while (true) {
				f = dfs(s, t, inf);
				if (f <= 0)break;
				res += f;
			}
		}
	}
};

void solve() {
	vector<PLL> vers;
	{
		set<PLL> versset;
		versset.insert(PLL(1, 0));
		versset.insert(PLL(N, (LL)1e9+(LL)d));
		for (int m = 0; m < M; m++) {
			versset.insert(PI(U[m], P[m]));
			versset.insert(PI(V[m], Q[m]+d));
		}
		for (PLL pll : versset) {
			vers.push_back(pll);
		}
		sort(vers.begin(), vers.end(), [](PLL a, PLL b) {
			if (a.first != b.first)return a.first < b.first;
			else return a.second < b.second;
		});
	}
	auto conv = [&](PLL a) {
		int s = -1, e = vers.size();
		while (e - s > 1) {
			int m = (e + s) / 2;
			if (vers[m].first < a.first)s = m;
			else if (vers[m].first > a.first)e = m;
			else if (vers[m].second <= a.second)s = m;
			else e = m;
		}
		return s;
	};
	Dinic<LL> D(vers.size());
	for (int m = 0; m < M; m++) {
		int s = conv(PLL(U[m], P[m]));
		int e = conv(PLL(V[m], Q[m]+d));
		D.AddEdge(s, e, W[m]);
	}
	for (int n = 0; n < vers.size()-1; n++) {
		if (vers[n].first == vers[n + 1].first) {
			D.AddEdge(n, n + 1, 1e15);
		}
	}
	LL maxflow = D.MaxFlow(0, vers.size() - 1);
	cout << maxflow << "\n";
}

void naive() {
}

void outputinput() {
}
0