結果

問題 No.119 旅行のツアーの問題
ユーザー krotonkroton
提出日時 2015-01-07 00:07:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,691 bytes
コンパイル時間 1,256 ms
コンパイル使用メモリ 152,668 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-23 09:11:36
合計ジャッジ時間 2,224 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In instantiation of ‘void Dinic<V, INF, MAXV>::add_edge(int, int, V) [with V = int; V INF = 33554432; int MAXV = 200]’:
main.cpp:88:28:   required from here
main.cpp:49:3: warning: narrowing conversion of ‘(&((Dinic<int, 33554432, 200>*)this)->Dinic<int, 33554432, 200>::G.std::array<std::vector<Dinic<int, 33554432, 200>::edge, std::allocator<Dinic<int, 33554432, 200>::edge> >, 200>::operator[](((std::array<std::vector<Dinic<int, 33554432, 200>::edge, std::allocator<Dinic<int, 33554432, 200>::edge> >, 200>::size_type)to)))->std::vector<Dinic<int, 33554432, 200>::edge, std::allocator<Dinic<int, 33554432, 200>::edge> >::size()’ from ‘std::vector<Dinic<int, 33554432, 200>::edge, std::allocator<Dinic<int, 33554432, 200>::edge> >::size_type’ {aka ‘long unsigned int’} to ‘int’ inside { } [-Wnarrowing]
   G[from].push_back((edge){to, G[to].size(), cap});
   ^
main.cpp:50:47: warning: narrowing conversion of ‘((&((Dinic<int, 33554432, 200>*)this)->Dinic<int, 33554432, 200>::G.std::array<std::vector<Dinic<int, 33554432, 200>::edge, std::allocator<Dinic<int, 33554432, 200>::edge> >, 200>::operator[](((std::array<std::vector<Dinic<int, 33554432, 200>::edge, std::allocator<Dinic<int, 33554432, 200>::edge> >, 200>::size_type)from)))->std::vector<Dinic<int, 33554432, 200>::edge, std::allocator<Dinic<int, 33554432, 200>::edge> >::size() - 1)’ from ‘std::vector<Dinic<int, 33554432, 200>::edge, std::allocator<Dinic<int, 33554432, 200>::edge> >::size_type’ {aka ‘long unsigned int’} to ‘int’ inside { } [-Wnarrowing]
   G[to].push_back((edge){from, G[from].size() - 1, 0});

ソースコード

diff #

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

template<class V, V INF, int MAXV>
class Dinic {
private:
	struct edge { int to, rev; V cap; };

	array<vector<edge>, MAXV> G;
	array<int, MAXV> level, iter;

	void bfs(int s){
		level.fill(-1);

		queue<int> Q;
		level[s] = 0;
		Q.push(s);

		while(!Q.empty()){
			int v = Q.front(); Q.pop();
			for(const auto &e : G[v]){
				if(e.cap > 0 && level[e.to] < 0){
					level[e.to] = level[v] + 1;
					Q.push(e.to);
				}
			}
		}
	}

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

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

	V max_flow(int s, int t){
		V flow = 0;
		for(;;){
			bfs(s);
			if(level[t] < 0)return flow;

			iter.fill(0);
			V f;

			while((f = dfs(s, t, INF)) > 0){
				flow += f;
			}
		}
	}
};

int N, B[100], C[100];
int M, D[1000], E[1000];

int main(){
	const int INF = 1 << 25;

	cin >> N;
	for(int i=0;i<N;i++)
		cin >> B[i] >> C[i];
	
	cin >> M;
	for(int i=0;i<M;i++)
		cin >> D[i] >> E[i];

	Dinic<int, INF, 200> dinic;
	int s = 2 * N, t = s + 1;
	int res = 0;

	for(int i=0;i<N;i++){
		dinic.add_edge(s, i, B[i]);
		dinic.add_edge(i, N+i, INF);
		dinic.add_edge(N+i, t, C[i]);
		
		res += B[i] + C[i];
	}

	for(int i=0;i<M;i++){
		dinic.add_edge(D[i], N+E[i], INF);
	}

	res -= dinic.max_flow(s, t);
	cout << res << endl;
	return 0;
}
0