結果

問題 No.119 旅行のツアーの問題
ユーザー niiminiimi
提出日時 2018-09-15 01:11:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,540 bytes
コンパイル時間 1,758 ms
コンパイル使用メモリ 147,144 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 01:55:30
合計ジャッジ時間 3,199 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
typedef pair<int, int> P;

#define For(i, a, b) for(int i = (a); i < (b); i++)
#define Rep(i, n) For(i, 0, (n))
#define Rrep(i, n) for(int i = (n - 1); i >= 0; i--)
#define Itr(i, c) for(typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define pb push_back

const int inf = 999999999;
const int mod = 1000000007;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

typedef struct{
	int to, cap, rev;
}edge;

vector<edge> G[82];
bool used[82];

void add_edge(int from, int to, int cap){
	int p = G[to].size(), q = G[from].size();
	G[from].pb((edge){to, cap, p});
	G[to].pb((edge){from, 0, q});
}

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

int max_flow(int s, int t){
	int flow = 0;
	while(1){
		Rep(i, sizeof(used)/sizeof(*used)){
			used[i] = false;
		}
		int f = dfs(s, t, inf);
		if(f == 0) return flow;
		flow += f;
	}
}

int main(){
	int n, m, sum = 0; cin >> n;
	Rep(i, n){
		int b, c; cin >> b >> c;
		sum += max(b, c);
		add_edge(80, i * 2, max(b, c) - c);
		add_edge(i * 2 + 1, 81, max(b, c) - b);
		add_edge(i * 2, i * 2 + 1, max(b, c));
	}
	cin >> m;
	Rep(i, m){
		int d, e;
		cin >> d >> e;
		add_edge(d * 2 + 1, e * 2, inf);
	}
	int f = max_flow(80, 81);
	cout << sum - f << endl;
	return 0;
}
0