KM两道。
POJ 3686 建图很巧妙,注意建图的方式。
/*POJ 3686*/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;
const int maxn = 60*60;
const int INF = 0x3f3f3f3f;
int n, m;
int W[maxn][maxn];
int Lx[maxn], Ly[maxn];
int Left[maxn];
bool S[maxn], T[maxn];
bool match(int i)
{
S[i] = 1;
for(int j = 1; j <= m; j++) if(Lx[i]+Ly[j] == W[i][j] && !T[j])
{
T[j] = 1;
if(!Left[j] || match(Left[j]))
{
Left[j] = i;
return 1;
}
}
return 0;
}
void update()
{
int a = INF;
for(int i = 1; i <= n; i++) if(S[i])
for(int j = 1; j <= m; j++) if(!T[j])
a = min(a, Lx[i]+Ly[j]-W[i][j]);
for(int i = 1; i <= n; i++)
{
if(S[i]) Lx[i] -= a;
}
for(int j = 1; j <= m; j++)
{
if(T[j]) Ly[j] += a;
}
}
void KM()
{
memset(Left, 0, sizeof(Left));
memset(Lx, 0, sizeof(Lx));
memset(Ly, 0, sizeof(Ly));
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
Lx[i] = max(Lx[i], W[i][j]);
}
for(int i = 1; i <= n; i++)
{
for(;;)
{
for(int j = 1; j <= m; j++) S[j] = T[j] = 0;
if(match(i)) break; else update();
}
}
}
inline void readint(int &x)
{
char c;
c = getchar();
while(!isdigit(c)) c = getchar();
x = 0;
while(isdigit(c)) x = x*10+c-'0', c = getchar();
}
inline void writeint(int x)
{
if(x > 9) writeint(x/10);
putchar(x%10+'0');
}
void read_case()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
int c;
scanf("%d", &c);
for(int k = 1; k <= n; k++)
W[i][(j-1)*n+k] = -c*k;
}
m *= n;
}
void solve()
{
read_case();
KM();
int ans = 0;
for(int i = 1; i <= m; i++) if(Left[i]) ans += W[Left[i]][i];
printf("%.6lf\n", -1.0*ans/n);
}
int main()
{
int T;
for(readint(T); T > 0; T--)
{
solve();
}
return 0;
}
/*HDU 3718*/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;
const int maxn = 110;
const int INF = 0x3f3f3f3f;
int n, m;
int W[maxn][maxn];
int Lx[maxn], Ly[maxn];
int Left[maxn];
bool S[maxn], T[maxn];
bool match(int i)
{
S[i] = 1;
for(int j = 1; j <= m; j++) if(Lx[i]+Ly[j] == W[i][j] && !T[j])
{
T[j] = 1;
if(!Left[j] || match(Left[j]))
{
Left[j] = i;
return 1;
}
}
return 0;
}
void update()
{
int a = INF;
for(int i = 1; i <= n; i++) if(S[i])
for(int j = 1; j <= m; j++) if(!T[j])
a = min(a, Lx[i]+Ly[j]-W[i][j]);
for(int i = 1; i <= n; i++)
{
if(S[i]) Lx[i] -= a;
}
for(int j = 1; j <= m; j++)
{
if(T[j]) Ly[j] += a;
}
}
void KM()
{
memset(Left, 0, sizeof(Left));
memset(Lx, 0, sizeof(Lx));
memset(Ly, 0, sizeof(Ly));
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
Lx[i] = max(Lx[i], W[i][j]);
}
for(int i = 1; i <= n; i++)
{
for(;;)
{
for(int j = 1; j <= m; j++) S[j] = T[j] = 0;
if(match(i)) break; else update();
}
}
}
inline void readint(int &x)
{
char c;
c = getchar();
while(!isdigit(c)) c = getchar();
x = 0;
while(isdigit(c)) x = x*10+c-'0', c = getchar();
}
inline void writeint(int x)
{
if(x > 9) writeint(x/10);
putchar(x%10+'0');
}
char str[4];
char STR[10010];
int K, N, M;
void read_case()
{
readint(N), readint(K), readint(M);
for(int i = 1; i <= N; i++)
{
scanf("%s", str);
STR[i] = str[0];
}
n = m = 27;
}
double cal()
{
KM();
int ans = 0;
for(int i = 1; i <= 30; i++) if(Left[i]) ans += W[Left[i]][i];
return (double)ans/N;
}
void solve()
{
read_case();
for(int i = 1; i <= M; i++)
{
memset(W, 0, sizeof(W));
for(int j = 1; j <= N; j++)
{
scanf("%s", str);
int u = str[0]-'A'+1, v = STR[j]-'A'+1;
W[u][v]++;
}
printf("%.4lf\n", cal());
}
}
int main()
{
int T;
for(readint(T); T > 0; T--)
{
solve();
}
return 0;
}