結果

問題 No.1341 真ん中を入れ替えて門松列
ユーザー leaf_1415leaf_1415
提出日時 2021-01-15 22:40:07
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,837 bytes
コンパイル時間 985 ms
コンパイル使用メモリ 94,136 KB
実行使用メモリ 488,576 KB
最終ジャッジ日時 2024-05-05 00:41:16
合計ジャッジ時間 4,615 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(),(x).end()
#define inf 1e18

using namespace std;

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



struct MinCostFlow{
	struct edge{
		int to, cap, rev;
		ll cost;
		edge(){}
		edge(llint a, llint b, llint c, llint d){
			to = a, cap = b, cost = c, rev = d;
		}
	};
	int n;
	vector<vector<edge> > G;
	vector<llint> dist;
	vector<int> prevv, preve;
	
	MinCostFlow(){}
	MinCostFlow(int n){
		this->n = n;
		G.resize(n+1);
		dist.resize(n+1);
		prevv.resize(n+1);
		preve.resize(n+1);
	}
	void BellmanFord(int s)
	{
		for(int i = 0; i <= n; i++) dist[i] = inf;
		dist[s] = 0, prevv[s] = -1;
		
		bool update = true;
		while(update){
			update = false;
			for(int i = 0; i <= n; i++){
				for(int j = 0; j < G[i].size(); j++){
					if(G[i][j].cap == 0) continue;
					if(dist[G[i][j].to] > dist[i] + G[i][j].cost){
						dist[G[i][j].to] = dist[i] + G[i][j].cost;
						prevv[G[i][j].to] = i;
						preve[G[i][j].to] = j;
						update = true;
					}
				}
			}
		}
	}
	void add_edge(int from, int to, int cap, llint cost)
	{
		G[from].push_back( edge(to, cap, cost, G[to].size()) );
		G[to].push_back( edge(from, 0, -cost, G[from].size()-1) );
	}
	llint calc(int s, int t, llint f, bool &flag)
	{
		llint ret = 0;
		while(f > 0){
			BellmanFord(s);
			if(dist[t] >= inf) break;
			
			llint p = t, flow = f;
			while(prevv[p] != -1){
				flow = min(flow, (ll)G[prevv[p]][preve[p]].cap);
				p = prevv[p];
			}
			
			p = t;
			while(prevv[p] != -1){
				G[prevv[p]][preve[p]].cap -= flow;
				G[p][G[prevv[p]][preve[p]].rev].cap += flow;
				p = prevv[p];
			}
			f -= flow;
			ret += dist[t] * flow;
		}
		if(f > 0) flag = true;
		return ret;
	}
};

ll n, m;
ll l[3005], r[3005], a[3005];
MinCostFlow mcf(6005);

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> m;
	rep(i, 1, n){
		cin >> l[i] >> a[i] >> r[i];
		if(l[i] > r[i]) swap(l[i], r[i]);
	}
	
	rep(i, 1, n){
		rep(j, 1, n){
			if(r[i] < a[j]) mcf.add_edge(i, n+j, 1, -a[j]);
			if(l[i] > a[j]) mcf.add_edge(i, n+j, 1, -r[i]);
		}
	}
	ll S = 2*n+1, T = 2*n+2;
	rep(i, 1, n) mcf.add_edge(S, i, 1, 0), mcf.add_edge(n+i, T, 1, 0);
	
	bool flag = false;
	ll res = -mcf.calc(S, T, n, flag);
	if(flag) cout << "NO" << endl;
	else{
		cout << "YES" << endl;
		if(res >= m) cout << "KADOMATSU!" << endl;
		else cout << "NO" << endl;
	}
	
	return 0;
}
0