結果

問題 No.654 Air E869120
ユーザー ふっぴーふっぴー
提出日時 2018-02-24 00:46:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 3,346 bytes
コンパイル時間 2,569 ms
コンパイル使用メモリ 189,324 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 18:03:05
合計ジャッジ時間 3,400 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #




#include "bits/stdc++.h"
using namespace std;

#define DEBUG(x) cout<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cout<<#v<<":";for(int i=0;i<v.size();i++) cout<<" "<<v[i]; cout<<endl

typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
const int inf = 1000000001;
const ll INF = 2e18;
#define MOD 1000000007
#define mod 1000000009
#define pi 3.14159265358979323846
#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;
//int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
int dx[6] = { -1,0,1,1,0,-1 }, dy[6] = { 0, 1, 1, 0, -1, -1 };
int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };



// 辺を表す構造体 {行先、容量、逆辺}
struct edge { int to; ll cap; int rev; };
#define V 10010
vector< vector<edge> > G(V, vector<edge>()); //グラフの隣接リスト表現
vl level(V); //sからの距離
vi iter(V); //どこまで調べ終わったか

			//fromからtoへ向かう容量capの辺をグラフに追加する
void add_edge(int from, int to, ll cap) {
	edge a;
	a.to = to; a.cap = cap; a.rev = G[to].size();
	G[from].push_back(a);
	a.to = from; a.cap = 0; a.rev = G[from].size() - 1;
	G[to].push_back(a);
}

//sからの最短距離をBFSで計算する
void bfs(int s) {
	fill(level.begin(), level.end(), -1);
	queue<int> que;
	level[s] = 0;
	que.push(s);
	while (!que.empty()) {
		int v = que.front(); que.pop();
		for (int i = 0; i < G[v].size(); i++) {
			edge &e = G[v][i];
			if (e.cap > 0 && level[e.to] < 0) {
				level[e.to] = level[v] + 1;
				que.push(e.to);
			}
		}
	}
}

//増加パスをDFSで探す
ll dfs(int v, int t, ll f) {
	if (v == t) return f;
	for (int &i = iter[v]; i < G[v].size(); i++) {
		edge &e = G[v][i];
		if (e.cap > 0 && level[v] < level[e.to]) {
			ll d = dfs(e.to, t, min(f, e.cap));
			if (d > 0) {
				e.cap -= d;
				G[e.to][e.rev].cap += d;
				return d;
			}
		}
	}
	return 0;
}

//sからtへの最大流を調べる
ll max_flow(int s, int t) {
	ll flow = 0;
	for (;;) {
		bfs(s);
		if (level[t] < 0) return flow;
		fill(iter.begin(), iter.end(), 0);
		ll f;
		while ((f = dfs(s, t, inf)) > 0) {
			flow += f;
		}
	}
}

int main() {
	ll n, m, d;
	cin >> n >> m >> d;
	vl u(m), v(m), p(m), q(m), w(m);
	vll time(n);
	time[0].push_back(0);
	time[n - 1].push_back(INF);
	map<pll, int> mp;
	int cnt = 2;
	mp[pll(0, 0)] = 1;
	mp[pll(n - 1, INF)] = 2;
	rep(i, m) {
		cin >> u[i] >> v[i] >> p[i] >> q[i] >> w[i];
		u[i]--; v[i]--;
		q[i] += d;
		int x, y;
		if (mp[pll(u[i], p[i])]) {
			x = mp[pll(u[i], p[i])];
		}
		else {
			x = ++cnt;
			mp[pll(u[i], p[i])] = x;
		}
		if (mp[pll(v[i], q[i])]) {
			y = mp[pll(v[i], q[i])];
		}
		else {
			y = ++cnt;
			mp[pll(v[i], q[i])] = y;
		}
		add_edge(x, y, w[i]);
		time[u[i]].push_back(p[i]);
		time[v[i]].push_back(q[i]);
	}
	rep(i, n) {
		sort(time[i].begin(), time[i].end());
		rep(j, time[i].size() - 1) {
			int x = mp[pll(i, time[i][j])], y = mp[pll(i, time[i][j + 1])];
			add_edge(x, y, INF);
		}
	}
	cout << max_flow(1, 2) << endl;

	
}
0