POJ 3686 The Windy's & HDU 3718 Similarity

72 篇文章 0 订阅

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值