Poj 3894 System Engineer (maximum matching of bipartite graphs-Hungary algorithm)

Source: Internet
Author: User

System Engineer
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submissions: 507 Accepted: 217

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 participant 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

 

Assignment: assign work. One job corresponds to one person.

Code:

 

# Include <cstdio> # include <cstring> # include <iostream> using namespace std; struct edge {int to, next;} e [100000]; int index; bool visit [10010]; // records whether a vertex in v2 has been searched for int match [10010]; // record the number of the point that matches the vertices in V2 int head [10010]; int m, cnt; // Add an edge to the graph, note that a successor node with a directed edge // u is v ------> uvoid addedge (int u, int v) {e [index]. to = v; e [index]. next = head [u]; head [u] = index; index ++;} // Hungarian algorithm (adjacent table Storage graph) bool dfs (int u) {int I, v; for (I = head [u]; I = e [I]. next) {v = e [I]. to; if (! Visit [v]) // If the node u is adjacent to v and has not been searched {visit [v] = true; // Mark v if (match [v] =-1 | dfs (match [v]). // if v is not in the previous M match, or v is in M matching, but {// there can be an augmented path match [v] = u starting from the node adjacent to v; // The record is found successfully, update matching means "" return true; // return that the search is successful. }}} Return false;} void init () {int aa, k, bb, I; memset (head, 0, sizeof (head )); // remember to initialize memset (e, 0, sizeof (e); index = 1; for (I = 1; I <= m; I ++) {scanf ("% d: (% d)", & aa, & k); // printf ("OK: % d \ n", aa, k ); while (k --) {scanf ("% d", & bb); bb = bb-cnt + 1; addedge (bb, aa + 1 );}}} int main () {int I; while (scanf ("% d", & cnt )! = EOF) {m = cnt; init (); int ans = 0; memset (match,-1, sizeof (match); for (I = 1; I <= cnt; I ++) {memset (visit, 0, sizeof (visit); // clear the flag if (dfs (I) for the last search )) // try to extend ans ++;} printf ("% d \ n", ans);} return 0 ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.