hdu Assignment 二分图匹配

Assignment

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 2
Problem Description
Last year a terrible earthquake attacked Sichuan province. About 300,000 PLA soldiers attended the rescue, also ALPCs. Our mission is to solve difficulty problems to optimization the assignment of troops. The assignment is measure by efficiency, which is an integer, and the larger the better. We have N companies of troops and M missions, M>=N. One company can get only one mission. One mission can be assigned to only one company. If company i takes mission j, we can get efficiency Eij. We have a assignment plan already, and now we want to change some companies’ missions to make the total efficiency larger. And also we want to change as less companies as possible.
 
Input
For each test case, the first line contains two numbers N and M. N lines follow. Each contains M integers, representing Eij. The next line contains N integers. The first one represents the mission number that company 1 takes, and so on. 1<=N<=M<=50, 1
 
Output
For each the case print two integers X and Y. X represents the number of companies whose mission had been changed. Y represents the maximum total efficiency can be increased after changing.
 
Sample Input
3 3 2 1 3 3 2 4 1 26 2 2 1 3 2 3 1 2 3 1 2 3 1 2
 
Sample Output
2 26 1 2
 
Source
2009 Multi-University Training Contest 5 - Host by NUDT
 题目的构图狠巧妙啊~ 网上抄的,哎

因为我们要变动最小,所以对在原计划中的边要有一些特殊照顾,使得最优匹配时,尽量优先使用原计划的边,这样变化才能是最小的且不会影响原匹配。

根据这个思想,我们可以把每条边的权值扩大k倍,k要大于n。然后对原计划的边都+1。精华全在这里。我们来详细说明一下。

全部边都扩大了k倍,而且k比n大,这样,我们求出的最优匹配就是k倍的最大权值,只要除以k就可以得到最大权值。实现原计划的边加1,这样,在每次选择边时,这些变就 有了优势,就会优先选择这些边。假如原计划的h条边被选入了最优匹配中,这样,最优权值就是k倍的最大权值+k(原计划的每条边都+1)。但是k大于n的用意何在呢?我们发现假如原计划的边全部在匹配中,只会增加n,又n

View Code
 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 using namespace std;
 6 #define maxn 51
 7 #define inf 1e9
 8 int map[maxn][maxn];
 9 int mx[maxn];
10 int n,m,mnum,sum;
11 int lx[maxn],ly[maxn];
12 int tlx[maxn],tly[maxn];
13 void init(){
14  int t;
15  sum=0;mnum=0;
16  for(int i=1;i<=n;i++)lx[i]=-inf;
17  for(int i=1;i<=m;i++)ly[i]=0;
18  for(int i=1;i<=n;i++){
19   for(int j=1;j<=m;j++){
20    scanf("%d",&map[i][j]);
21    map[i][j]*=100;
22   }
23  }
24  for(int i=1;i<=n;i++){
25   scanf("%d",&mx[i]);
26   sum+=map[i][mx[i]];
27   map[i][mx[i]]+=1;
28  }
29  for(int i=1;i<=n;i++){
30   for(int j=1;j<=m;j++)if(map[i][j]>lx[i]) lx[i]=map[i][j];
31  }
32 }
33 int my[maxn],slack;
34 bool visx[maxn],visy[maxn];
35 bool dfs(int u){
36  visx[u]=true;
37  for(int v=1;v<=m;v++)if(!visy[v]){
38   int t=lx[u]+ly[v]-map[u][v];
39   if(!t){
40    visy[v]=true;
41    if(my[v]==-1 || dfs(my[v])){
42     my[v]=u; return true;
43    }
44   }else if(tt;
45  }
46  return false;
47 }
48 int km(){
49  int ans=0;
50  memset(my,-1,sizeof(my));
51  for(int i=1;i<=n;i++){
52   while(1){
53    memset(visx,0,sizeof(visx));
54    memset(visy,0,sizeof(visy));
55    slack=inf;
56    if(dfs(i)) break;
57    for(int j=1;j<=n;j++)if(visx[j]) lx[j]-=slack;
58    for(int j=1;j<=m;j++)if(visy[j]) ly[j]+=slack;
59   }
60  }
61  for(int i=1;i<=m;i++){
62   if(my[i]!=-1){
63    ans+=ly[i]+lx[my[i]];
64   }
65  }
66  return ans;
67 }
68 int main()
69 {
70  freopen("in.txt","r",stdin);
71     while(scanf("%d%d",&n,&m)!=EOF){
72   init();
73   mnum=km();
74   printf("%d %d\n",n-mnum%100,mnum/100-sum/100);
75     }
76     return 0;
77 }

 

转载于:https://www.cnblogs.com/lmnx/archive/2012/04/27/2473962.html

你可能感兴趣的:(java)