題目大意:有一個烤肉老板,每個單位時間可以完成M的烤肉
現在有N位客人,給出每位客人來的時間,走的時間,烤肉的數量和每串烤肉所需的單位時間
問這個老板能否完成每位客人的需求
解題思路:這題和HDU 3572相似,但又不能像那題那樣做,因為這題時間長度有點大
所以將時間區間當成一個點,將該區間連向超級匯點,容量為區間長度*M
將所有客人連向超級源點,容量為烤肉數量*每串烤肉所需時間
接下來的難點就是怎麼將客人和時間區間連起來了
如果時間區間在客人來的時間和走的時間這段區間內,就表明這段時間可以用來幫客人烤肉,所以可以連接,容量為INF
這樣圖就建好了
附上大神的詳細題解
詳細題解
#include
#include
#include
#include
#include
using namespace std;
#define N 1010
#define INF 0x3f3f3f3f
struct Edge {
int from, to, cap, flow;
Edge() {}
Edge(int from, int to, int cap, int flow): from(from), to(to), cap(cap), flow(flow) {}
};
struct ISAP {
int p[N], num[N], cur[N], d[N];
int t, s, n, m;
bool vis[N];
vector G[N];
vector edges;
void init(int n) {
this->n = n;
for (int i = 0; i <= n; i++) {
G[i].clear();
d[i] = INF;
}
edges.clear();
}
void AddEdge(int from, int to, int cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool BFS() {
memset(vis, 0, sizeof(vis));
queue Q;
d[t] = 0;
vis[t] = 1;
Q.push(t);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int i = 0; i < G[u].size(); i++) {
Edge &e = edges[G[u][i] ^ 1];
if (!vis[e.from] && e.cap > e.flow) {
vis[e.from] = true;
d[e.from] = d[u] + 1;
Q.push(e.from);
}
}
}
return vis[s];
}
int Augment() {
int u = t, flow = INF;
while (u != s) {
Edge &e = edges[p[u]];
flow = min(flow, e.cap - e.flow);
u = edges[p[u]].from;
}
u = t;
while (u != s) {
edges[p[u]].flow += flow;
edges[p[u] ^ 1].flow -= flow;
u = edges[p[u]].from;
}
return flow;
}
int Maxflow(int s, int t) {
this->s = s; this->t = t;
int flow = 0;
BFS();
if (d[s] > n)
return 0;
memset(num, 0, sizeof(num));
memset(cur, 0, sizeof(cur));
for (int i = 0; i < n; i++)
if (d[i] < INF)
num[d[i]]++;
int u = s;
while (d[s] <= n) {
if (u == t) {
flow += Augment();
u = s;
}
bool ok = false;
for (int i = cur[u]; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (e.cap > e.flow && d[u] == d[e.to] + 1) {
ok = true;
p[e.to] = G[u][i];
cur[u] = i;
u = e.to;
break;
}
}
if (!ok) {
int Min = n;
for (int i = 0; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (e.cap > e.flow)
Min = min(Min, d[e.to]);
}
if (--num[d[u]] == 0)
break;
num[d[u] = Min + 1]++;
cur[u] = 0;
if (u != s)
u = edges[p[u]].from;
}
}
return flow;
}
};
ISAP isap;
int S[N], E[N], num[N], T[N], All[N];
int n, m;
void solve() {
int t, cnt = 0, s = 0, Sum = 0;
for (int i = 1; i <= n; i++) {
scanf(%d%d%d%d, &S[i], &num[i], &E[i], &T[i]);
All[cnt++] = S[i];
All[cnt++] = E[i];
Sum += num[i] * T[i];
}
sort(All, All + cnt);
cnt = unique(All, All + cnt) - All;
t = n + cnt + 1;
isap.init(t);
for (int i = 1; i <= n; i++)
isap.AddEdge(s, i, num[i] * T[i]);
for (int i = 1; i < cnt; i++) {
isap.AddEdge(i + n, t, (All[i] - All[i - 1]) * m);
for (int j = 1; j <= n; j++) {
if (S[j] <= All[i - 1] && E[j] >= All[i]) {
isap.AddEdge(j, i + n, INF);
}
}
}
if (Sum == isap.Maxflow(s, t))
printf(Yes
);
else
printf(No
);
}
int main() {
while (scanf(%d%d, &n, &m) != EOF) {
solve();
}
return 0;
}