結果

問題 No.654 Air E869120
ユーザー KKT89KKT89
提出日時 2020-03-13 13:33:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 2,018 bytes
コンパイル時間 1,049 ms
コンパイル使用メモリ 93,252 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-01 18:26:35
合計ジャッジ時間 2,348 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef long long int ll;

struct Edge{
	int to,rev; ll cap;
	Edge(int to,ll cap,int rev):to(to),cap(cap),rev(rev){}
};
// ここの値に注意!!
const int N=2350;
const ll INF=1e15;
vector<Edge> g[N];
int level[N]; 
int iter[N];
void add_edge(int s,int t,ll c){
	g[s].push_back(Edge(t,c,g[t].size()));
	g[t].push_back(Edge(s,0,g[s].size()-1));
}
void bfs(int s){
	memset(level,-1,sizeof(level));
	queue<int> q;
	level[s]=0;
	q.push(s);
	while(q.size()){
		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);
			}
		}
	}
}
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]){
			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;
}
ll max_flow(int s,int t){
	ll flow=0;
	while(1){
		bfs(s);
		if(level[t]<0)return flow;
		memset(iter,0,sizeof(iter));
		int f;
		while((f=dfs(s,t,INF))>0){
			flow+=f;
		}
	}
}

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n,m,d; cin >> n >> m >> d;
	vector<int> x(m),y(m),p(m),q(m),w(m);
	vector<pair<int,int>> v;
	for(int i=0;i<m;i++){
		cin >> x[i] >> y[i] >> p[i] >> q[i] >> w[i];
		q[i]+=d;
		v.push_back(make_pair(x[i],p[i]));
		v.push_back(make_pair(y[i],q[i]));
 	}
 	sort(v.begin(),v.end());
 	for(int i=0;i<m;i++){
 		int num1=lower_bound(v.begin(),v.end(),make_pair(x[i],p[i]))-v.begin();
 		int num2=lower_bound(v.begin(),v.end(),make_pair(y[i],q[i]))-v.begin();
 		add_edge(num1,num2,w[i]);
 	}
 	for(int i=1;i<v.size();i++){
 		if(v[i].first==v[i-1].first){
 			add_edge(i-1,i,1e10);
 		}
 	}
 	for(int i=0;i<v.size();i++){
 		if(v[i].first==1){
 			add_edge(v.size(),i,1e10);
 		}
 		if(v[i].first==n){
 			add_edge(i,v.size()+1,1e10);
 		}
 	}
 	cout << max_flow(v.size(),v.size()+1) << endl;
}

0