結果
問題 | No.1341 真ん中を入れ替えて門松列 |
ユーザー | leaf_1415 |
提出日時 | 2021-01-15 22:29:32 |
言語 | C++11 (gcc 11.4.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,595 bytes |
コンパイル時間 | 1,226 ms |
コンパイル使用メモリ | 100,596 KB |
実行使用メモリ | 634,624 KB |
最終ジャッジ日時 | 2024-05-05 00:30:34 |
合計ジャッジ時間 | 4,877 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | MLE | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
ソースコード
#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{ llint to, cap, cost, rev; 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; vector<llint> h; MinCostFlow(){} MinCostFlow(int n){ this->n = n; G.resize(n+1); dist.resize(n+1); prevv.resize(n+1); preve.resize(n+1); h.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 Dijkstra(int s) { for(int i = 0; i <= n; i++) dist[i] = inf; dist[s] = 0, prevv[s] = -1; priority_queue< P, vector<P>, greater<P> > Q; Q.push( make_pair(0, s) ); llint v, d; while(Q.size()){ d = Q.top().first; v = Q.top().second; Q.pop(); if(dist[v] < d) continue; for(int i = 0; i < G[v].size(); i++){ if(G[v][i].cap == 0) continue; llint u = G[v][i].to, c = h[v] - h[u] + G[v][i].cost; if(dist[u] > d + c){ dist[u] = d + c; prevv[u] = v; preve[u] = i; Q.push( P(dist[u], u) ); } } } } void add_edge(int from, int to, llint 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) { BellmanFord(s); for(int i = 0; i <= n; i++) h[i] = dist[i]; llint ret = 0; while(f > 0){ Dijkstra(s); if(dist[t] >= inf) break; llint p = t, flow = f; while(prevv[p] != -1){ flow = min(flow, 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] + h[t] - h[s]) * flow; for(int i = 0; i <= n; i++) h[i] += dist[i]; //オーバーフローに注意(?) } 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; 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; }