题目:http://acm.hdu.edu.cn/showproblem.php?pid=2813
题目挺有意思的,也很明了,稍微有点坑爹的地方就是名字转换成数字的过程,刚开始用map<string, int>去映射,超时了,然后直接暴力搜,结果过了,于是搜了一下别人的代码,发现基本都是用映射,发现我映射部分写搓了,于是回去改了一下,过了,但是效率并没有直接搜效率高。。。
直接搜:
#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <vector>
#include <map>
using namespace std;
const int N = 210;
const int INF = 0x3f3f3f3f;
int nx, ny;
int lx[N], ly[N], match[N], slack[N];
bool visx[N], visy[N];
int s[N][N];
bool hungary(int v)
{
visx[v] = true;
for(int i = 0; i < ny; i++)
{
if(visy[i]) continue;
if(lx[v] + ly[i] == s[v][i])
{
visy[i] = true;
if(match[i] == -1 || hungary(match[i]))
{
match[i] = v;
return true;
}
}
else slack[i] = min(slack[i], lx[v] + ly[i] - s[v][i]);
}
return false;
}
void km()
{
memset(match, -1, sizeof match);
memset(ly, 0, sizeof ly);
for(int i = 0; i < nx; i++)
lx[i] = -INF;
for(int i = 0; i < nx; i++)
for(int j = 0; j < ny; j++)
lx[i] = max(lx[i], s[i][j]);
for(int i = 0; i < nx; i++)
{
memset(slack, 0x3f, sizeof slack);
while(true)
{
memset(visx, 0, sizeof visx);
memset(visy, 0, sizeof visy);
if(hungary(i)) break;
else
{
int tmp = INF;
for(int j = 0; j < ny; j++)
if(!visy[j]) tmp = min(tmp, slack[j]);
for(int j = 0; j < nx; j++)
if(visx[j]) lx[j] -= tmp;
for(int j = 0; j < ny; j++)
if(visy[j]) ly[j] += tmp;
else slack[j] -= tmp;
}
}
}
}
int main()
{
int n, m, k, a;
char s1[50], s2[50];
char sss1[220][50], sss2[220][50];
while(~ scanf("%d%d%d", &n, &m, &k))
{
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
s[i][j] = -INF;
int cnt1 = 0, cnt2 = 0;
for(int i = 0; i < k; i++)
{
scanf(" %s%s %d", s1, s2, &a);
int t1, t2;
bool f = false;
for(int j = 0; j < cnt1; j++)
if(strcmp(s1, sss1[j]) == 0)
{
f = true; t1 = j; break;
}
if(f == false) t1 = cnt1, strcpy(sss1[cnt1], s1), cnt1++;
f = false;
for(int j = 0; j < cnt2; j++)
if(strcmp(s2, sss2[j]) == 0)
{
f = true; t2 = j; break;
}
if(f == false) t2 = cnt2, strcpy(sss2[cnt2], s2), cnt2++;
s[t1][t2] = -a;
}
nx = n, ny = m;
km();
int res = 0;
for(int i = 0; i < ny; i++)
if(match[i] != -1) res += s[match[i]][i];
printf("%d\n", -res);
}
return 0;
}
映射:
#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <vector>
#include <map>
using namespace std;
const int N = 240;
const int INF = 0x3f3f3f3f;
int nx, ny;
int lx[N], ly[N], match[N], slack[N];
bool visx[N], visy[N];
int s[N][N];
bool hungary(int v)
{
visx[v] = true;
for(int i = 0; i < ny; i++)
{
if(visy[i]) continue;
if(lx[v] + ly[i] == s[v][i])
{
visy[i] = true;
if(match[i] == -1 || hungary(match[i]))
{
match[i] = v;
return true;
}
}
else slack[i] = min(slack[i], lx[v] + ly[i] - s[v][i]);
}
return false;
}
void km()
{
memset(match, -1, sizeof match);
memset(ly, 0, sizeof ly);
for(int i = 0; i < nx; i++)
lx[i] = -INF;
for(int i = 0; i < nx; i++)
for(int j = 0; j < ny; j++)
lx[i] = max(lx[i], s[i][j]);
for(int i = 0; i < nx; i++)
{
memset(slack, 0x3f, sizeof slack);
while(true)
{
memset(visx, 0, sizeof visx);
memset(visy, 0, sizeof visy);
if(hungary(i)) break;
else
{
int tmp = INF;
for(int j = 0; j < ny; j++)
if(!visy[j]) tmp = min(tmp, slack[j]);
for(int j = 0; j < nx; j++)
if(visx[j]) lx[j] -= tmp;
for(int j = 0; j < ny; j++)
if(visy[j]) ly[j] += tmp;
else slack[j] -= tmp;
}
}
}
}
int main()
{
int n, m, k, a;
char s1[50], s2[50];
while(~ scanf("%d%d%d", &n, &m, &k))
{
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
s[i][j] = -INF;
map<string, int> map1, map2;
int cnt1 = 0, cnt2 = 0;
for(int i = 0; i < k; i++)
{
scanf(" %s%s%d", s1, s2, &a);
if(map1.find(s1) == map1.end()) map1[s1] = cnt1++;
if(map2.find(s2) == map2.end()) map2[s2] = cnt2++;
s[map1[s1]][map2[s2]] = -a;
}
nx = n, ny = m;
km();
int res = 0;
for(int i = 0; i < ny; i++)
if(match[i] != -1) res += s[match[i]][i];
printf("%d\n", -res);
}
return 0;
}