首页 > 其他分享 >HDU 2883 kebab(离散化+最大流)

HDU 2883 kebab(离散化+最大流)

时间:2023-06-12 14:39:15浏览次数:27  
标签:2883 HDU kebab int flow maxn time 时间 kebabs


题意:给定n个顾客,第i号顾客在si到达,点了ni个羊肉串,每个羊肉串需要ti个时间烤好。顾客想要在ei得到,一个烤炉只烤m串。问你是否能满足所有顾客的要求?能的话输出“Yes”,否则输出“No”。

思路:转自网上

          其实本题的本质就是HDU3572的思想,每个顾客其实提出的是需要ni*ti个单位时间任务(甚至可以在1个时刻同时完成,因为一串羊肉串都可以在1个时刻烤完),但是你每个时间只能提供m个单位时间做任务. 但是这个题目的时间点覆盖1到100W,明显不能再把每个单独的时间看成一个点了,所以这题要把每个不重叠的子时间区间看成一个点.

          首先读入所有任务的开始时间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)

          求最大流,看最大流 是否== 所有任务需要的单位时间之和即可.



#include <cstdio>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
#define maxn 1550
#define INF 1<<29
#define LL long long
int cas=1,T;
struct Edge
{
	int from,to,cap,flow;
	Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
int n,m;
struct Dinic
{
//	int n,m;
    int s,t;
	vector<Edge>edges;        //边数的两倍
	vector<int> G[maxn];      //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
	bool vis[maxn];           //BFS使用
	int d[maxn];              //从起点到i的距离
	int cur[maxn];            //当前弧下标
	void init()
	{
	   for (int i=0;i<=1200;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));        //反向弧
		int mm=edges.size();
		G[from].push_back(mm-2);
		G[to].push_back(mm-1);
	}
	bool BFS()
	{
		memset(vis,0,sizeof(vis));
		queue<int>q;
		q.push(s);
		d[s]=0;
		vis[s]=1;
		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]=1;
					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 = 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 s,int t)
	{
		this->s=s;
		this->t=t;
		int flow = 0;
		while (BFS())
		{
			memset(cur,0,sizeof(cur));
			flow+=DFS(s,INF);
		}
		return flow;
	}
}dc;

int nn[maxn];
int s[maxn];
int e[maxn];
int t[maxn];
int time[maxn];
int main()
{
    while (scanf("%d%d",&n,&m)!=EOF)
	{
		int sum = 0;
		int cnt = 0;
		for (int i = 1;i<=n;i++)
		{
			scanf("%d%d%d%d",&s[i],&nn[i],&e[i],&t[i]);
			time[cnt++]=s[i];
			time[cnt++]=e[i];
			sum+=nn[i]*t[i];
		}
		sort(time,time+cnt);
		cnt = unique(time,time+cnt)-time;
		dc.init();
		for (int i = 1;i<=n;i++)
		{
			dc.AddEdge(0,i,nn[i]*t[i]);
		}
		for (int i = 1;i<cnt;i++)
		{
			dc.AddEdge(n+i,n+cnt+1,(time[i]-time[i-1])*m);
			for (int j = 1;j<=n;j++)
			{
				if (s[j]<=time[i-1] && time[i]<=e[j])
					dc.AddEdge(j,n+i,INF);
			}
		}
		printf("%s\n",dc.Maxflow(0,n+cnt+1)==sum?"Yes":"No");
	}
}




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



 






标签:2883,HDU,kebab,int,flow,maxn,time,时间,kebabs
From: https://blog.51cto.com/u_16156555/6462523

相关文章

  • hdu2044 一只小蜜蜂.
    思路:观察一下可以知道,比如走到7,首先要走到5或者6,要走到5,首先要先走到4或3...递推一下即可#include<iostream>#include<cstdio>usingnamespacestd;#defineLLlonglongLLf[60];intn;intmain(){intT;scanf("%d",&T);f[0]=1;f[1]=1;for(inti=2;......
  • hdu2086 A1 = ?
    思路:推公式题....#include<stdio.h>doublea[3005],c[3005];doublesum;intmain(){intn;while(scanf("%d",&n)!=EOF) { scanf("%lf%lf",&a[0],&a[n+1]); for(inti=1;i<=n;i++) scanf("%lf",&c......
  • HDU 2732 Leapin' Lizards(最大流)
    题意:给你一个网格,网格上的一些位置上有一只蜥蜴,所有蜥蜴的最大跳跃距离是d,如果一只蜥蜴能跳出网格边缘,那么它就安全了.且每个网格有一个最大跳出次数x,即最多有x只蜥蜴从这个网格跳出,这个网格就再也不能有蜥蜴进来了.问你最少有多少只蜥蜴跳不出网格.思路:和POJ3498差不多.........
  • hdu2068 RPG的错排(错排)
    思路:我们定义f(n)为n个人抽到的情况总数。对于第n个人,他要不抽中自己,即要抽中其他n-1个人,有n-1种可能,接下来讨论下,如果第n个人它抽中的人也抽中了第n个人,那么有f(n-2)种情况,如果第n个人抽中的人没有抽中第n个人,那么有f(n-1)可能,所以f(n)=(n-1)*(f(n-1)+f(n-2))。      ......
  • hdu2079选课时间(背包)
    思路:相当于一个裸的多重背包#include<iostream>#include<cstdio>usingnamespacestd;inta[20],num[20],dp[50];intmain(){ intT; scanf("%d",&T); while(T--) { intn,m; scanf("%d%d",&n,&m); for(inti=1;i<=m;i......
  • hdu2066 一个人的旅行(最短路)
    思路:简单最短路#include<stdio.h>#include<string.h>#include<algorithm>usingnamespacestd;constintinf=1<<30;intT,S,D,n;intmap[1111][1111];intvis[1111],cast[1111];ints[1111],e[1111];voidDijkstra(){inti,j,minn,p......
  • HDU 2838 Cow Sorting(树状数组)
    题意:首先每次只能交换相邻的两头牛,并且最后要求升序排列,所以最后整个序列的逆序是0,每次交换只可以消除1个逆序。(令a[i]的逆序是从1到i-1比它大的数的个数。)思路:对于某个数,要把它变成有序的,那么很容易可以推算出公式就是它自身的逆序数乘自身的值再加上它的逆序数的和,自己算算看看。......
  • HDU 1394 Minimum Inversion Number(树状数组)
    题意:有一个n个整数的排列,这n个整数就是0,1,2,3...n-1这n个数(但不一定按这个顺序给出)。现在先计算一下初始排列的逆序数,然后把第一个元素a1放到an后面去,形成新排列a2a3a4...ana1,然后再求这个排列的逆序数。继续执行类似操作(一共要执行n-1次)直到产生排列ana1a2...an-1为止。......
  • HDU 3743 Frosh Week(树状数组+离散化)
    题意和思路:和POJ2299几乎一样...离散化+树状数组#include<cstdio>#include<queue>#include<cstring>#include<iostream>#include<cstdlib>#include<algorithm>#include<vector>#include<map>#include<string>#includ......
  • POJ 2352 HDU1541 Stars(树状数组)
    题意:二维平面给定n个点的坐标,然后要你输出每个点的“等级“。每个点的等级是它的左下放的点个数(包括正下放和正左方的点)。即要你输出对于每个点(x,y)来说,有多少点的坐标(xi,yi)满足xi<=x且yi<=y。思路:题目给出的坐标中已经是按y升序排列,那么其实只用考虑x轴,那么显然就是在前面的......