hud2883--kebab

kebab

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2495    Accepted Submission(s): 1141


 

Problem Description
Almost everyone likes kebabs nowadays (Here a kebab means pieces of meat grilled on a long thin stick). Have you, however, considered about the hardship of a kebab roaster while enjoying the delicious food? Well, here's a chance for you to help the poor roaster make sure whether he can deal with the following orders without dissatisfying the customers.

Now N customers is coming. Customer i will arrive at time si (which means the roaster cannot serve customer i until time si). He/She will order ni kebabs, each one of which requires a total amount of ti unit time to get it well-roasted, and want to get them before time ei(Just at exactly time ei is also OK). The roaster has a big grill which can hold an unlimited amount of kebabs (Unbelievable huh? Trust me, it’s real!). But he has so little charcoal that at most M kebabs can be roasted at the same time. He is skillful enough to take no time changing the kebabs being roasted. Can you help him determine if he can meet all the customers’ demand?

Oh, I forgot to say that the roaster needs not to roast a single kebab in a successive period of time. That means he can divide the whole ti unit time into k (1<=k<=ti) parts such that any two adjacent parts don’t have to be successive in time. He can also divide a single kebab into k (1<=k<=ti) parts and roast them simultaneously. The time needed to roast one part of the kebab well is linear to the amount of meat it contains. So if a kebab needs 10 unit time to roast well, he can divide it into 10 parts and roast them simultaneously just one unit time. Remember, however, a single unit time is indivisible and the kebab can only be divided into such parts that each needs an integral unit time to roast well.
 

 

Input
There are multiple test cases. The first line of each case contains two positive integers N and M. N is the number of customers and M is the maximum kebabs the grill can roast at the same time. Then follow N lines each describing one customer, containing four integers: si (arrival time), ni (demand for kebabs), ei (deadline) and ti (time needed for roasting one kebab well). 

There is a blank line after each input block.

Restriction:
1 <= N <= 200, 1 <= M <= 1,000
1 <= ni, ti <= 50
1 <= si < ei <= 1,000,000
 

 

Output
If the roaster can satisfy all the customers, output “Yes” (without quotes). Otherwise, output “No”.
 

hud2883--kebab_第1张图片

首先读入所有任务的开始时间s[i]和结束时间e[i],然后对这些时间点排序,去重,得到cnt个时间点,然后我们就能得到cnt-1个半开半闭的子时间区间(前后两个子区间边界不重叠,且所有区间连起来正好覆盖了原来的整个大时间区间,该大时间区间也是半开,半闭的).  建图: 源点s编号0,  n个任务编号1到n,  cnt-1个区间编号n+1到n+cnt,  汇点t编号n+cnt+1. 源点到每个任务i有边(s,i,ni*ti)每个时间区间j到汇点有边(j,t, 该区间覆盖的单位时间点数)如果任务i包含时间区间j,那么有边(i,j,INF)求最大流,看最大流 是否== 所有任务需要的单位时间之和即可.

主要是要用离散的方式来存数据!然后套网络流的dinic模板就好了!

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define mem memset
#define sc scanf
#define pr printf
typedef long long ll;
using namespace std;
//inline int read(){
//	int X = 0, w = 0; char ch = 0;
//	while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
//	while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
//	return w ? -X : X;
//}
//inline double dbread(){
//	double X = 0, Y = 1.0; int w = 0; char ch = 0;
//	while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
//	while (isdigit(ch)) X = X * 10 + (ch ^ 48), ch = getchar();
//	ch = getchar();//读入小数点
//	while (isdigit(ch)) X += (Y /= 10) * (ch ^ 48), ch = getchar();
//	return w ? -X : X;
//}
//inline void write(int x){
//	if (x < 0) putchar('-'), x = -x;
//	if (x > 9) write(x / 10);
//	putchar(x % 10 + '0');
//}
#define maxn 605
struct Edge {
	int from, to, cap, flow;
	Edge(){}
	Edge(int f, int t, int c, int fl) :from(f), to(t), cap(c), flow(fl) {}
};
// 建图: 源点s编号0,  n个任务编号1到n,  cnt-1个区间编号n+1到n+cnt,  汇点t编号n+cnt+1.
struct Dinic {
	int n, m, s, t;
	vector edge;
	vector G[maxn];
	int d[maxn];
	int cur[maxn];
	bool vis[maxn];
	void init(int n, int s, int t) {
		this->n = n;
		this->s = s;
		this->t = t;
		edge.clear();
		for (int i = 0; i < n; ++i)
			G[i].clear();
	}
	void addedge(int from, int to, int cap) {
		edge.push_back(Edge(from, to, cap, 0));//初始流量为0
		edge.push_back(Edge(to, from, 0, 0));//反向
		m = edge.size();
		G[from].push_back(m - 2);
		G[to].push_back(m - 1);
	}
	bool BFS() {
		mem(vis, 0, sizeof(vis));
		queue Q;
		Q.push(s);
		vis[s] = true;
		d[s] = 0;
		Q.push(s);
		while (!Q.empty()) {
			int x = Q.front();
			Q.pop();
			for (int i = 0; i < G[x].size(); ++i) {
				Edge& e = edge[G[x][i]];
				if (!vis[e.to] && e.cap > e.flow) {
					vis[e.to] = true;
					d[e.to] = d[x] + 1;
					Q.push(e.to);
				}
			}
		}
		return vis[t];
	}
	int DFS(int x, int a) {
		if (x == t || a == 0)
			return a;
		int flow = 0, f;
		for (int& i = cur[x]; i < G[x].size(); ++i) {
			Edge& e = edge[G[x][i]];
			if (d[e.to] == d[x] + 1 && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
				e.flow += f;
				edge[G[x][i] ^ 1].flow -= f;
				flow += f;
				a -= f;
				if (a == 0)
					break;
			}
		}
		return flow;
	}
	int max_flow() {
		int ans = 0;
		while (BFS()) {
			mem(cur, 0, sizeof(cur));
			ans += DFS(s, INF);
		}
		return ans;
	}
}DC;
int N, M;
int s[maxn], n[maxn], e[maxn], t[maxn];
int tm[maxn];
int full_flow;
int main(){
	//ios::sync_with_stdio(false);
	while (sc("%d%d", &N, &M) == 2) {
		full_flow = 0;
		int cnt = 0;
		for (int i = 1; i <= N; ++i) {// 读入所有任务的开始时间s[i]和结束时间e[i]
			sc("%d%d%d%d", &s[i], &n[i], &e[i], &t[i]);
			tm[cnt++] = s[i];
			tm[cnt++] = e[i];
			full_flow += n[i] * t[i];
		}
		//对这些时间点排序,去重,得到cnt个时间点
		sort(tm, tm + cnt);
		cnt = unique(tm, tm + cnt) - tm;//去重
		int src = 0, dst = N + cnt + 1;
		DC.init(N + cnt + 2, src, dst);
		for (int i = 1; i <= N; ++i)
			DC.addedge(src, i, n[i] * t[i]);
		for (int i = 1; i <= cnt - 1; ++i) {
			DC.addedge(N + i, dst, (tm[i] - tm[i - 1]) * M);
			for (int j = 1; j <= N; ++j)
				if (s[j] <= tm[i - 1] && tm[i] <= e[j])
					DC.addedge(j, N + i, INF);
		}
		pr("%s\n", DC.max_flow() == full_flow ? "Yes" : "No");
	}
	return 0;
}

 

你可能感兴趣的:(网络流)