結果
| 問題 |
No.580 旅館の予約計画
|
| ユーザー |
anta
|
| 提出日時 | 2017-10-22 23:52:30 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 78 ms / 2,000 ms |
| コード長 | 3,946 bytes |
| コンパイル時間 | 2,138 ms |
| コンパイル使用メモリ | 187,624 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-21 14:36:53 |
| 合計ジャッジ時間 | 4,087 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 36 |
ソースコード
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if (y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if (x < y) x = y; }
struct MinimumCostMaximumFlow {
typedef int Index; typedef int Flow; typedef int Cost;
static const Flow InfCapacity = INF; static const Cost InfCost = INF;
struct Edge {
Index to; Index rev;
Flow capacity; Cost cost;
};
vector<vector<Edge> > g;
void init(Index n) { g.assign(n, vector<Edge>()); }
void add(Index i, Index j, Flow capacity = InfCapacity, Cost cost = Cost()) {
Edge e, f; e.to = j, f.to = i; e.capacity = capacity, f.capacity = 0; e.cost = cost, f.cost = -cost;
g[i].push_back(e); g[j].push_back(f);
g[i].back().rev = (Index)g[j].size() - 1; g[j].back().rev = (Index)g[i].size() - 1;
}
void addB(Index i, Index j, Flow capacity = InfCapacity, Cost cost = Cost()) {
add(i, j, capacity, cost);
add(j, i, capacity, cost);
}
pair<Cost, Flow> minimumCostMaximumFlow(Index s, Index t, Flow f = InfCapacity, bool useSPFA = false) {
int n = g.size();
vector<Cost> dist(n); vector<Index> prev(n); vector<Index> prevEdge(n);
pair<Cost, Flow> total = make_pair(0, 0);
vector<Cost> potential(n);
while (f > 0) {
fill(dist.begin(), dist.end(), InfCost);
if (useSPFA || total.second == 0) {
deque<Index> q;
q.push_back(s); dist[s] = 0; vector<bool> inqueue(n);
while (!q.empty()) {
Index i = q.front(); q.pop_front(); inqueue[i] = false;
for (Index ei = 0; ei < (Index)g[i].size(); ei ++) {
const Edge &e = g[i][ei]; Index j = e.to; Cost d = dist[i] + e.cost;
if (e.capacity > 0 && d < dist[j]) {
if (!inqueue[j]) {
inqueue[j] = true;
q.push_back(j);
}
dist[j] = d; prev[j] = i; prevEdge[j] = ei;
}
}
}
} else {
vector<bool> vis(n);
priority_queue<pair<Cost, Index> > q;
q.push(make_pair(-0, s)); dist[s] = 0;
while (!q.empty()) {
Index i = q.top().second; q.pop();
if (vis[i]) continue;
vis[i] = true;
for (Index ei = 0; ei < (Index)g[i].size(); ei ++) {
const Edge &e = g[i][ei];
if (e.capacity <= 0) continue;
Index j = e.to; Cost d = dist[i] + e.cost + potential[i] - potential[j];
if (dist[j] > d) {
dist[j] = d; prev[j] = i; prevEdge[j] = ei;
q.push(make_pair(-d, j));
}
}
}
}
if (dist[t] == InfCost) break;
if (!useSPFA) for (Index i = 0; i < n; i ++) potential[i] += dist[i];
Flow d = f; Cost distt = 0;
for (Index v = t; v != s; ) {
Index u = prev[v]; const Edge &e = g[u][prevEdge[v]];
d = min(d, e.capacity); distt += e.cost; v = u;
}
f -= d; total.first += d * distt; total.second += d;
for (Index v = t; v != s; v = prev[v]) {
Edge &e = g[prev[v]][prevEdge[v]];
e.capacity -= d; g[e.to][e.rev].capacity += d;
}
}
return total;
}
};
int main() {
int N; int M;
while (~scanf("%d%d", &N, &M)) {
MinimumCostMaximumFlow mcmf;
const int X = 7 * 60 * 24 + 1;
const int src = X, dst = src + 1;
mcmf.init(dst + 1);
rep(i, src - 1)
mcmf.add(i, i + 1, INF, 0);
mcmf.add(src, 0, N);
mcmf.add(X - 1, dst, N);
rep(i, M) {
pair<int, int> p;
rep(k, 2) {
int d, h, m;
scanf("%d %d:%d", &d, &h, &m);
d -= 2;
int x = (d * 24 + h) * 60 + m;
if (k == 0)
p.first = x;
else
p.second = x + 1;
}
mcmf.add(p.first, p.second, 1, -1);
}
int ans = -mcmf.minimumCostMaximumFlow(src, dst, N).first;
printf("%d\n", ans);
}
return 0;
}
anta