System Engineer
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 236 |
|
Accepted: 98 |
Description
Bob is a skilled system engineer. He is always facing challenging problems, and now he must solve a new one. He has to handle a set of servers with differing capabilities available to process job requests from persistent sources - jobs that need to be processed over a long or indefinite period of time. A sequence of persistent job requests arrives revealing a subset of servers capable of servicing their request. A job is processed on a single server and a server processes only one job. Bob has to schedule the maximum number of jobs on the servers. For example, if there are 2 jobs j1, j2 and 2 servers s1, s2, job j1 requiring the server s1, and job j2 requiring also the server s1. In this case Bob can schedule only one job. Can you help him?
In the general case there are n jobs numbered from 0 to n-1, n servers numbered from n to 2*n-1, and a sequence of job requests. The problem asks to find the maximum number of jobs that can be processed.
Input
The program input is at most 1 MB. Each data set in the file stands for a particular set of jobs. A data set starts with the number n (n <= 10000) of jobs, followed by the list of required servers for each job, in the format:
jobnumber: (nr_servers) s1 ... snr_servers The program prints the maximum number of jobs that can be processed.
White spaces can occur freely in the input. The input data are correct and terminate with an end of file.
Output
For each set of data the program prints the result to the standard output from the beginning of a line.
Sample Input
2
0: (1) 2
1: (1) 2
1
0: (1) 1
Sample Output
1
1
Hint
There are two data sets. In the first case, the number of jobs n is 2, numbered 0 and 1. The sequence of requests for job 0 is: 0: (1) 2, meaning that job 0 requires 1 sever, the server numbered 2. The sequence of requests for job 1 is: 1: (1) 2, meaning that job 1 requires 1 sever, the server numbered 2. The result for the data set is the length of the maximum number of scheduled jobs, 1.
Source
Southeastern European Regional Programming Contest 2009
二分图匹配。
n最大10000,m未知。
使用普通的匈牙利算法应该会超时。
Hopcroft_Carp算法时间复杂度是O(n^0.5*m)。
用Dinic算法求最大流比匹配更快。
Hopcroft_Carp代码:
#include
#include
#include
#include
using namespace std;
const int N=10005;
const int INF=1<<28;
int Mx[N],My[N],Nx,Ny;
int dx[N],dy[N],dis;
vector adj[N];
bool vst[N];
bool searchP()
{
queue q;
dis=INF;
memset(dx,-1,sizeof(dx));
memset(dy,-1,sizeof(dy));
for(int i=0;idis)
break;
for(int i=0;i
Dinic代码:
#include
#include
#define N 20005
#define M 200005
#define inf 999999999
#define min(a,b) ((a)<(b)?(a):(b))
int n,m,s,t,num,adj[N],dis[N],q[N];
struct edge
{
int v,w,pre;
}e[M];
void insert(int u,int v,int w)
{
e[num]=(edge){v,w,adj[u]};
adj[u]=num++;
e[num]=(edge){u,0,adj[v]};//有向图
adj[v]=num++;
}
int bfs()
{
int i,x,v,head=0,tail=0;
memset(dis,0,sizeof(dis));
dis[s]=1;
q[++tail]=s;
while(head!=tail)
{
x=q[head=(head+1)%N];
for(i=adj[x];~i;i=e[i].pre)
if(e[i].w&&!dis[v=e[i].v])
{
dis[v]=dis[x]+1;
if(v==t)
return 1;
q[tail=(tail+1)%N]=v;
}
}
return 0;
}
int dfs(int x,int limit)
{
if(x==t)
return limit;
int i,v,tmp,cost=0;
for(i=adj[x];~i&&cost