結果

問題 No.1341 真ん中を入れ替えて門松列
ユーザー SSRSSSRS
提出日時 2021-01-15 23:14:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,144 bytes
コンパイル時間 3,406 ms
コンパイル使用メモリ 205,316 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-05-05 01:06:33
合計ジャッジ時間 16,699 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1000000000;
const long long INF = 1000000000000000;
const long long INF2 = 10000;
template <typename Cap, typename Cost>
struct primal_dual{
	struct edge{
		int to, rev;
		Cap cap;
		Cost cost;
		edge(int to, int rev, Cap cap, Cost cost): to(to), rev(rev), cap(cap), cost(cost){
		}
	};
	int N;
	vector<vector<edge>> G;
	primal_dual(){
	}
	primal_dual(int N): N(N), G(N){
	}
	void add_edge(int from, int to, Cap cap, Cost cost){
		int id1 = G[from].size();
		int id2 = G[to].size();
		G[from].push_back(edge(to, id2, cap, cost));
		G[to].push_back(edge(from, id1, 0, - cost));
	}
	pair<Cap, Cost> min_cost_flow(int s, int t, Cap F){
		Cap flow = 0;
		Cost cost = 0;
		vector<Cost> h(N, 0);
		while (flow < F){
			vector<Cap> m(N, INF2);
			vector<Cost> d(N, INF);
			vector<int> pv(N, -1);
			vector<int> pe(N, -1);
			vector<bool> used(N, false);
			priority_queue<pair<Cost, int>, vector<pair<Cost, int>>, greater<pair<Cost, int>>> pq;
			pq.push(make_pair(0, s));
			d[s] = 0;
			while (!pq.empty()){
				int v = pq.top().second;
				pq.pop();
				if (!used[v]){
					used[v] = true;
					if (v == t){
						break;
					}
					int cnt = G[v].size();
					for (int i = 0; i < cnt; i++){
						int w = G[v][i].to;
						if (!used[w] && G[v][i].cap > 0){
							Cost tmp = G[v][i].cost - h[w] + h[v];
							if (d[w] > d[v] + tmp){
								d[w] = d[v] + tmp;
								m[w] = min(m[v], G[v][i].cap);
								pv[w] = v;
								pe[w] = i;
								pq.push(make_pair(d[w], w));
							}
						}
					}
				}
			}
			if (!used[t]){
				break;
			}
			for (int i = 0; i < N; i++){
				if (used[i]){
					h[i] -= d[t] - d[i];
				}
			}
			Cap c = min(m[t], F - flow);
			for (int i = t; i != s; i = pv[i]){
				G[pv[i]][pe[i]].cap -= c;
				G[i][G[pv[i]][pe[i]].rev].cap += c;
			}
			flow += c;
			cost += c * (- h[s]);
		}
		return make_pair(flow, cost);
	}
};
int main(){
  int N;
  long long M;
  cin >> N >> M;
  vector<int> A(N), B(N), C(N);
  for (int i = 0; i < N; i++){
    cin >> A[i] >> B[i] >> C[i];
  }
  sort(B.begin(), B.end());
  primal_dual<int, long long> G(N * 3 + 2);
  int s = N * 3, t = N * 3 + 1;
  for (int i = 0; i < N; i++){
    int L = lower_bound(B.begin(), B.end(), min(A[i], C[i])) - B.begin();
    int R = upper_bound(B.begin(), B.end(), max(A[i], C[i])) - B.begin();
    G.add_edge(s, i, 1, 0);
    if (L > 0){
      G.add_edge(i, N + L - 1, 1, MAX - C[i]);
    }
    if (R < N){
      G.add_edge(i, N * 2 + R, 1, 0);
    }
  }
  for (int i = 0; i < N; i++){
    G.add_edge(N + i, t, N, 0);
    G.add_edge(N * 2 + i, t, N, MAX - B[i]);
  }
  for (int i = 0; i < N - 1; i++){
    G.add_edge(N + i + 1, N + i, N, 0);
    G.add_edge(N * 2 + i, N * 2 + i + 1, N, 0);
  }
  pair<int, long long> ans = G.min_cost_flow(s, t, N);
  if (ans.first < N){
    cout << "NO" << endl;
  } else {
    cout << "YES" << endl;
    long long sum = MAX * N - ans.second;
    if (sum >= M){
      cout << "KADOMATSU!" << endl;
    } else {
      cout << "NO" << endl;
    }
  }
}
0