結果

問題 No.654 Air E869120
ユーザー knshnbknshnb
提出日時 2018-11-05 23:04:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 4,547 bytes
コンパイル時間 2,734 ms
コンパイル使用メモリ 200,784 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-30 22:08:17
合計ジャッジ時間 3,987 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define ll long long
#define REP(i, n) for (ll i = 0, max_i = (n); i < max_i; i++)
#define REPI(i, a, b) for (ll i = (a), max_i = (b); i < max_i; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define fi first
#define se second
#define pb push_back
#define debug(x) cerr << #x << ": " << (x) << endl
#define debug2(x, y) cerr << #x << ": " << (x) << " " << #y << ": " << y << endl;
#define int long long
using namespace std;
using II = pair<int, int>;
using VII = vector<II>;
using VI = vector<int>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
template <class T = int> inline T in() { T x; cin >> x; return x; }
template <class T = int> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template <class T = int> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <class T> ostream& operator<<(ostream &s, const vector<T>& d) { int n = d.size(); REP (i, n) s << d[i] << " "; return s; }
template <class T> ostream& operator<<(ostream &s, const vector<vector<T>>& dd) { for (vector<T> d: dd) s << d << endl; return s; }
struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast;
const int MOD = 1e9 + 7;

// class Graph {
//   vector<bool> used;
//   int t; // ゴール
//   // f: 流量
//   int dfs(int v, int f) {
//     if (v == t) return f;
//     used[v] = true;
//     for (edge& e: G[v]) {
//       if (used[e.to] || e.cap <= 0) continue;
//       int res = dfs(e.to, min(f, e.cap));
//       if (res <= 0) continue;
//       e.cap -= res;
//       G[e.to][e.rev].cap += res;
//       return res;
//     }
//     return 0;
//   }
// public:
//   int V;
//   // G[e.to][e.rev]で逆辺にアクセスできるように
//   // 逆辺には"辺に流した流量"を入れる(はじめは全て0)
//   struct edge { int to, cap, rev; };
//   vector<vector<edge>> G;
//   Graph(int V) : V(V), G(V), used(V) {}
//   void add_edge(int from, int to, int cap) {
//     G[from].push_back({to, cap, (int)G[to].size()});
//     G[to].push_back({from, 0, (int)G[from].size() - 1});
//   }
//   int max_flow(int s, int t) {
//     this->t = t;
//     int ret = 0;
//     while (1) {
//       fill(ALL(used), false);
//       int f = dfs(s, 1e18);
//       if (f == 0) return ret;
//       ret += f;
//     }
//   }
// };
struct edge {
	long long to, cap, rev;
};

class Max_Flow {
public:
	vector<vector<edge>> G;
	vector<long long>level, iter;

	void init(long long size_) {
		G.resize(size_);
		level.resize(size_);
		iter.resize(size_);
	}

	void add_edge(long long from, long long to, long long cap) {
		G[from].push_back(edge{ to, cap, (long long)G[to].size() });
		G[to].push_back(edge{ from, 0, (long long)G[from].size() - 1 });
	}

	void bfs(long long s) {
		for (long long i = 0; i < level.size(); i++) level[i] = -1;
		queue<long long> que;
		level[s] = 0;
		que.push(s);
		while (!que.empty()) {
			long long v = que.front(); que.pop();
			for (long long 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);
				}
			}
		}
	}

	long long dfs(long long v, long long t, long long f) {
		if (v == t) return f;
		for (long long &i = iter[v]; i < G[v].size(); i++) {
			edge &e = G[v][i];
			if (e.cap > 0 && level[v] < level[e.to]) {
				long long 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;
	}

	long long max_flow(long long s, long long t) {
		long long flow = 0;
		for (;;) {
			bfs(s);
			if (level[t] < 0) return flow;
			for (int i = 0; i < iter.size(); i++) iter[i] = 0;
			long long f;
			while ((f = dfs(s, t, (1LL << 60))) > 0) {
				flow += f;
			}
		}
	}
};

struct Flight {
  int u, v, p, q, w;
};
signed main() {
  int N, M, d; cin >> N >> M >> d;
  map<II, int> trans;
  vector<Flight> flights(M);
  REP (i, M) {
    int u, v, p, q, w; cin >> u >> v >> p >> q >> w;
    q += d;
    flights[i] = {u, v, p, q, w};
    trans[{u, p}];
    trans[{v, q}];
  }
  int k = 0;
  for (auto& p: trans) {
    p.se = k++;
  }
  Max_Flow g; g.init(k);
  for (Flight f: flights) {
    g.add_edge(trans[{f.u, f.p}], trans[{f.v, f.q}], f.w);
  }
  for (auto it = trans.begin(); it != prev(trans.end()); it++) {
    if (next(it)->fi.fi == it->fi.fi) {
      g.add_edge(it->se, next(it)->se, 1e18);
    }
  }
  cout << g.max_flow(0, k - 1) << endl;
}
0