POJ 3686 The Windy's KM Algorithm

Source: Internet
Author: User

The creation of this question is really amazing.

 


If a machine processes k Toys, there are two kinds of time for these toys, one is the real processing time, and the other is the waiting time, the waiting time is the time for all the toys to be processed,

Assume that the k Toys are processed at a1, a2, a3... ak. The actual processing time and waiting time are respectively

A1, a1 + a2, a1 + a2 + a3...... a1 + a2 +... ak

After summation, a1 * k + a2 * (k-1) + a3 * (k-2)... + ak

In this case, we find that the actual time between each toy can be separated and then summed up.

Because each machine can process up to n toys, it can be split into n points, 1 ~ N represents the last number of processed toys on this machine.

Therefore, we connect each vertex k split in each toy I and machine j to an edge of the z [I] [j] * k weight.


Then obtain the minimum weight match.

 

 

[Cpp]
# Include <iostream>
# Include <algorithm>
# Include <cstring>
# Include <string>
# Include <cstdio>
# Include <cmath>
# Include <queue>
# Include <map>
# Include <set>
# Define eps 1e-5
# Define MAXN 55
# Define MAXM 55555
# Define INF 100000007
Using namespace std;
Int n, m, ny, nx;
Int w [MAXN] [2555];
Int lx [MAXN], ly [2555];
Int linky [2555];
Int visx [MAXN], visy [2555];
Int slack [2555];
Bool find (int x)
{
Visx [x] = 1;
For (int y = 1; y <= ny; y ++)
{
If (visy [y]) continue;
Int t = lx [x] + ly [y]-w [x] [y];
If (t = 0)
{
Visy [y] = 1;
If (linky [y] =-1 | find (linky [y])
{
Linky [y] = x;
Return true;
}
}
Else if (slack [y]> t) slack [y] = t;
}
Return false;
}
Int KM ()
{
Memset (linky,-1, sizeof (linky ));
For (int I = 1; I <= nx; I ++) lx [I] =-INF;
Memset (ly, 0, sizeof (ly ));
For (int I = 1; I <= nx; I ++)
For (int j = 1; j <= ny; j ++)
If (w [I] [j]> lx [I]) lx [I] = w [I] [j];
For (int x = 1; x <= nx; x ++)
{
For (int I = 1; I <= ny; I ++) slack [I] = INF;
While (true)
{
Memset (visx, 0, sizeof (visx ));
Memset (visy, 0, sizeof (visy ));
If (find (x) break;
Int d = INF;
For (int I = 1; I <= ny; I ++)
If (! Visy [I]) d = min (d, slack [I]);
If (d = INF) return-1;
For (int I = 1; I <= nx; I ++)
If (visx [I]) lx [I]-= d;
For (int I = 1; I <= ny; I ++)
If (visy [I]) ly [I] + = d;
Else slack [I]-= d;
}
}
Int tp = 0;
For (int I = 1; I <= ny; I ++)
If (linky [I]! =-1) tp + = w [linky [I] [I]-5000000;
Return-tp;
}
Int a [MAXN] [MAXN];
Int main ()
{
Int T;
Scanf ("% d", & T );
While (T --)
{
Memset (w, 0, sizeof (w ));
Scanf ("% d", & n, & m );
For (int I = 1; I <= n; I ++)
For (int j = 1; j <= m; j ++)
Scanf ("% d", & a [I] [j]);
For (int I = 1; I <= n; I ++)
For (int j = 1; j <= m; j ++)
For (int k = 1; k <= n; k ++)
W [I] [(j-1) * n + k] = 5000000-a [I] [j] * k;
Nx = n;
Ny = n * m;
Double ans = 1.0 * KM ()/n;
Printf ("% f \ n", ans );
}
Return 0;
}

# Include <iostream>
# Include <algorithm>
# Include <cstring>
# Include <string>
# Include <cstdio>
# Include <cmath>
# Include <queue>
# Include <map>
# Include <set>
# Define eps 1e-5
# Define MAXN 55
# Define MAXM 55555
# Define INF 100000007
Using namespace std;
Int n, m, ny, nx;
Int w [MAXN] [2555];
Int lx [MAXN], ly [2555];
Int linky [2555];
Int visx [MAXN], visy [2555];
Int slack [2555];
Bool find (int x)
{
Visx [x] = 1;
For (int y = 1; y <= ny; y ++)
{
If (visy [y]) continue;
Int t = lx [x] + ly [y]-w [x] [y];
If (t = 0)
{
Visy [y] = 1;
If (linky [y] =-1 | find (linky [y])
{
Linky [y] = x;
Return true;
}
}
Else if (slack [y]> t) slack [y] = t;
}
Return false;
}
Int KM ()
{
Memset (linky,-1, sizeof (linky ));
For (int I = 1; I <= nx; I ++) lx [I] =-INF;
Memset (ly, 0, sizeof (ly ));
For (int I = 1; I <= nx; I ++)
For (int j = 1; j <= ny; j ++)
If (w [I] [j]> lx [I]) lx [I] = w [I] [j];
For (int x = 1; x <= nx; x ++)
{
For (int I = 1; I <= ny; I ++) slack [I] = INF;
While (true)
{
Memset (visx, 0, sizeof (visx ));
Memset (visy, 0, sizeof (visy ));
If (find (x) break;
Int d = INF;
For (int I = 1; I <= ny; I ++)
If (! Visy [I]) d = min (d, slack [I]);
If (d = INF) return-1;
For (int I = 1; I <= nx; I ++)
If (visx [I]) lx [I]-= d;
For (int I = 1; I <= ny; I ++)
If (visy [I]) ly [I] + = d;
Else slack [I]-= d;
}
}
Int tp = 0;
For (int I = 1; I <= ny; I ++)
If (linky [I]! =-1) tp + = w [linky [I] [I]-5000000;
Return-tp;
}
Int a [MAXN] [MAXN];
Int main ()
{
Int T;
Scanf ("% d", & T );
While (T --)
{
Memset (w, 0, sizeof (w ));
Scanf ("% d", & n, & m );
For (int I = 1; I <= n; I ++)
For (int j = 1; j <= m; j ++)
Scanf ("% d", & a [I] [j]);
For (int I = 1; I <= n; I ++)
For (int j = 1; j <= m; j ++)
For (int k = 1; k <= n; k ++)
W [I] [(j-1) * n + k] = 5000000-a [I] [j] * k;
Nx = n; www.2cto.com
Ny = n * m;
Double ans = 1.0 * KM ()/n;
Printf ("% f \ n", ans );
}
Return 0;
}


 


Author: sdj222555

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.