博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LA 3231 - Fair Share
阅读量:6910 次
发布时间:2019-06-27

本文共 4787 字,大约阅读时间需要 15 分钟。

 

You are given N processors and M jobs to be processed. Two processors are specified to each job. To process the job, the job should be allocated to and executed on one of the two processors for one unit of time. If K jobs are allocated to a processor, then it takes K units of time for the processor to complete the jobs. To complete all the jobs as early as possible, you should allocate the M jobs to the N processors as fair as possible. Precisely speaking, you should minimize the maximum number of jobs allocated to each processor over all processors. The quantity, minimum number of jobs, is called fair share.

 

For example, you are given 5 processors and 6 jobs. Each job can be allocated to one of the two processors as shown in the table below. Job 1 can be allocated to processors 1 or 2, and job 2 can be allocated to processors 2 or 3, etc. If you allocate job 1 to processor 1, job 2 to processor 2, job 3 to processor 3, job 4 to processor 4, job 5 to processor 5, and job 6 to processor 1, then you have at most two jobs allocated to each processor. Since there are more jobs than processors in this example, some processors necessarily have at least two jobs, and thus the fair share is two.

Given N processors, M jobs, and the sets of two processors to which the jobs can be allocated, you are to write a program that finds the fair share. Processors are numbered from 1 to N and jobs are numbered from 1 to M . It is assumed that the sets of two processors to which the jobs can be allocated are distinct over all jobs.

That is, if a job J1 can be allocated to processors P1 or P2, and a job J2 which is different from J1 can be allocated to processors P3 or P4, then {

P1P2}$ \ne${
P3P4}.

 

Input 

The input consists of T test cases. The number of test cases T is given in the first line of the input file. Each test case begins with a line containing an integer N1$ \le$N$ \le$1, 000, that represents the number of processors in the test case. It is followed by a line containing an integer M1$ \le$M$ \le$10, 000, that represents the number of jobs. In the following M lines, K-th line contains two distinct integers representing processors to which job K can be allocated, 1$ \le$K$ \le$M. The integers given in a line are separated by a space. After that, the remaining test cases are listed in the same manner as the above.

 

Output 

Print exactly one line for each test case. The line should contain the fair share for that test case.

The following shows sample input and output for three test cases.

 

Sample Input 

 

3                                    5                                    6                                    1 2 2 3  3 4  4 5  5 1  1 33 2 3 2 1 2 6 6 1 2 3 4 4 6 6 5 5 3 6 3

 

Sample Output 

 

2  1 2

题意:m个任务n个处理器,一个任务只能在可供运行的两个机器中的其中一个上运行,问怎样分配使得任务最多的机器尽量少

思路:把任务和可以选择的机器连边,容量为1,建立源点汇点s, t,s到每个任务的容量为1,对于机器到汇点的容量二分答案

#include
#include
#include
#define m(s) memset(s,0,sizeof s)#define R register#define inf 0x3f3f3f3fusing namespace std;int read(){ R int x=0;bool f=1; R char ch=getchar(); while(ch<'0'||ch>'9'){
if(ch=='-')f=0;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} return f?x:-x;}const int N=2e4+10;int cas,S,T,n,m;int g[N][2],head[N],cur[N],dis[N],q[N*2];struct node{ int v,next,cap;}e[N<<2];int tot=1;void add(int x,int y,int z){ e[++tot].v=y;e[tot].cap=z;e[tot].next=head[x];head[x]=tot; e[++tot].v=x;e[tot].cap=0;e[tot].next=head[y];head[y]=tot;}bool bfs(){ for(int i=S;i<=T;i++) dis[i]=inf; int h=0,t=1;dis[S]=0;q[t]=S; while(h!=t){ int x=q[++h]; for(int i=head[x],v;i;i=e[i].next){ if(e[i].cap&&dis[v=e[i].v]>dis[x]+1){ dis[v]=dis[x]+1; if(v==T) return 1; q[++t]=v; } } } return 0;}int dfs(int x,int f){ if(x==T) return f; int used=0,t; for(int i=head[x],v;i;i=e[i].next){ if(e[i].cap&&dis[v=e[i].v]==dis[x]+1){ t=dfs(v,min(f,e[i].cap)); e[i].cap-=t;e[i^1].cap+=t; used+=t;f-=t; if(!f) return used; } } if(!used) dis[x]=0; return used;}bool check(int mid){ tot=1;m(head);int res=0; for(int i=1;i<=m;i++) add(S,i,1); for(int i=1;i<=m;i++) add(i,g[i][0]+m,1),add(i,g[i][1]+m,1); for(int i=1;i<=n;i++) add(i+m,T,mid); while(bfs()){ res+=dfs(S,inf); } return res>=m;}int main(){ for(cas=read();cas--;){ n=read();m=read();S=0;T=n+m+1; for(int i=1;i<=m;i++) g[i][0]=read(),g[i][1]=read(); int mid,ans=0,l=1,r=m; while(l<=r){ mid=(l+r)>>1; if(check(mid)) ans=mid,r=mid-1; else l=mid+1; } printf("%d\n",ans); } return 0;}

 

转载于:https://www.cnblogs.com/shenben/p/6257439.html

你可能感兴趣的文章
silverlight多国语言研究
查看>>
开发--省级三联动,简单的代码,但是功能不差
查看>>
赋值法
查看>>
单词积累(Unity)
查看>>
P4769 [NOI2018]冒泡排序(dp)
查看>>
[BZOJ5407]girls
查看>>
API接口 Http和Socket 优劣比较 使用场景选择
查看>>
js 邮政编码验证
查看>>
iOS开发之窥探UICollectionViewController(二) --详解CollectionView各种回调
查看>>
HDU 4532 湫秋系列故事——安排座位(组合)
查看>>
BZOJ 3672 [Noi2014]购票 (熟练剖分+凸壳维护)
查看>>
LINQ扩展实现去重复
查看>>
Linq to entity优化---MSDN
查看>>
多种方式实现依赖注入
查看>>
20150625_Andriod_01_ListView1_条目显示
查看>>
jmeter线程组之间传递参数
查看>>
monkey测试===Monkey测试策略(系列二)转
查看>>
安全测试===如何查看浏览器保存的密码
查看>>
POJ3177 Redundant Paths【双连通分量】
查看>>
El表达式的用法个人总结
查看>>