您的位置:首页 > 大数据 > 人工智能

UVALive 3231 Fair Share

2015-08-05 09:36 459 查看

Fair Share

Time Limit: 3000ms
Memory Limit: 131072KB
This problem will be judged on UVALive. Original ID: 3231
64-bit integer IO format: %lld Java class name: Main

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.

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 20010;
struct arc {
int to,flow,next;
arc(int x = 0,int y = 0,int z = -1) {
to = x;
flow = y;
next = z;
}
} e[1000010];
int head[maxn],d[maxn],cur[maxn],tot,S,T;
void add(int u,int v,int flow) {
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,0,head[v]);
head[v] = tot++;
}
bool bfs(){
queue<int>q;
q.push(T);
memset(d,-1,sizeof d);
d[T] = 1;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i^1].flow > 0 && d[e[i].to] == -1){
d[e[i].to] = d[u] + 1;
q.push(e[i].to);
}
}
}
return d[S] > -1;
}
int dfs(int u,int low){
if(u == T) return low;
int tmp = 0,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow > 0 && d[e[i].to]+1== d[u]&&(a=dfs(e[i].to,min(e[i].flow,low)))){
e[i].flow -= a;
low -= a;
e[i^1].flow += a;
tmp += a;
if(!low) break;
}
}
if(!tmp) d[u] = -1;
return tmp;
}
int dinic(){
int ret = 0;
while(bfs()){
memcpy(cur,head,sizeof head);
ret += dfs(S,INT_MAX);
}
return ret;
}
int u[maxn*10],v[maxn*10],n,m;
bool build(int mid) {
memset(head,-1,sizeof head);
tot = 0;
for(int i = 1; i <= n; ++i)
add(S,i,mid);
for(int i = 1; i <= m; ++i) {
add(n + i,T,1);
add(u[i],n+i,1);
add(v[i],n+i,1);
}
return dinic() >= m;
}
int main() {
int kase;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d",&n,&m);
for(int i = 1; i <= m; ++i)
scanf("%d%d",u+i,v+i);
S = 0;
T = n + m + 2;
int low = 0,high = m,ret;
while(low <= high){
int mid = (low + high)>>1;
if(build(mid)){
ret = mid;
high = mid - 1;
}else low = mid + 1;
}
printf("%d\n",ret);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: