HDU - 2883 kebab (最大流)

                                                kebab

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


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”.
 

Sample Input
 
 
2 10 1 10 6 3 2 10 4 2 2 10 1 10 5 3 2 10 4 2
 

Sample Output
 
 
Yes No
 

一、原题地址

          传送门


二、大致题意

    有N个客人来店里面吃烤肉,现店里面有一台烤肉能力为M的烤肉架。

    每个客人有到来的时间Si,需求的每串烤肉需要烤的时间ni,离开的时间Ei,需求的烤肉数量。

    每串肉是可以分开来烤的,现在询问能否在客人限定的时间内满足他们的烤肉要求。


三、大致思路

    hdu 3572 的升级版。同样是将时间看作是最大流上的点,但是这里的时间范围很大,所以需要将他们先处理为时间段,这样的话最多两百个客人,就是最多只有400-1个时间段了。

    建图。

    关于每个时间段,容量就是这个时间段的烤肉能力,也就是这段(时间的长度)乘以(店里烤肉架的工作能力)。

    关于每个顾客,源点与他们的边的容量,就是这个客人需求的烤肉数量,和每个烤肉需要的时间相乘。即ni*ti。而与每个时间段是否建边,应该取决于这个客人被服务的时间是否被这个这个时间段包含,若包含则容量就是这个客人的烤肉总需求量。


四、代码

    

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int inf = 0x3f3f3f3f;
#define LL long long int 
long long  gcd(long long  a, long long  b) { return a == 0 ? b : gcd(b % a, a); }

const int maxn = 610;
int n, m;
int sum;
int t_num;
set<int>use_for_time;
struct Node
{
	int s, num, e, nedtim;
}cust[205];
struct Time
{
	int s, e;
}time[405];
void read()
{
	sum = 0;
	for (int i = 1; i <= n; i++)
	{
		scanf("%d %d %d %d", &cust[i].s, &cust[i].num, &cust[i].e, &cust[i].nedtim);
		sum += cust[i].nedtim*cust[i].num;
		use_for_time.insert(cust[i].s);
		use_for_time.insert(cust[i].e);
	}
}
void Hash()
{
	t_num = 0;
	set<int>::iterator it = use_for_time.begin(),end=use_for_time.end();
	int fa = *it;
	for (it++; it != end; it++)
	{
		time[t_num].s = fa;
		time[t_num++].e = *it;
		fa = *it;
	}
}
struct Edge
{
	Edge() {}
	Edge(int from, int to, int cap, int flow) :from(from), to(to), cap(cap), flow(flow) {}
	int from, to, cap, flow;
};
struct Dinic
{
	int n, m, s, t;            //结点数,边数(包括反向弧),源点与汇点编号
	vector<Edge> edges;     //边表 edges[e]和edges[e^1]互为反向弧
	vector<int> G[maxn];    //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
	bool vis[maxn];         //BFS使用,标记一个节点是否被遍历过
	int d[maxn];            //从起点到i点的距离
	int cur[maxn];          //当前弧下标

	void init(int n, int s, int t)
	{
		this->n = n, this->s = s, this->t = t;
		for (int i = 0; i <= n; i++) G[i].clear();
		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<int> Q;//用来保存节点编号的
		Q.push(s);
		d[s] = 0;
		vis[s] = true;
		while (!Q.empty())
		{
			int x = Q.front(); Q.pop();
			for (int i = 0; i<G[x].size(); i++)
			{
				Edge& e = edges[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;//flow用来记录从x到t的最小残量
		for (int& i = cur[x]; i<G[x].size(); i++)
		{
			Edge& e = edges[G[x][i]];
			if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow)))>0)
			{
				e.flow += f;
				edges[G[x][i] ^ 1].flow -= f;
				flow += f;
				a -= f;
				if (a == 0) break;
			}
		}
		return flow;
	}

	int Maxflow()
	{
		int flow = 0;
		while (BFS())
		{
			memset(cur, 0, sizeof(cur));
			flow += DFS(s, inf);
		}
		return flow;
	}
}DC;
void build()
{
	int vt = 1 + n + t_num;
	DC.init(2 + n + t_num, 0, vt);
	for (int i = 1; i <= n; i++)
	{
		int w = cust[i].num*cust[i].nedtim;
		DC.AddEdge(0, i, w);
		for (int j = 0; j < t_num; j++)
		{
			if (cust[i].s <= time[j].s&&time[j].e <= cust[i].e)
				DC.AddEdge(i, n + j + 1, (time[j].e - time[j].s )*m);
		}
	}
	for (int i = 0; i < t_num; i++)
	{
		DC.AddEdge(n + i + 1, vt, (time[i].e - time[i].s )*m);
	}
}
void init()
{
	use_for_time.clear();
	read();
	Hash();
	build();
}
int main()
{
	while (scanf("%d%d", &n, &m) != EOF)
	{
		init();
		int tt=DC.Maxflow();
		if (tt == sum)printf("Yes\n");
		else printf("No\n");
	}
	getchar();
	getchar();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值