题意:有几场战斗。怎么安排,使受到的伤害最小
思路:把字符串编码,KM匹配即可
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<vector>
#include<cstdlib>
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define cl(a,b) memset(a,b,sizeof(a));
#define LL long long
#define P pair<int,int>
#define X first
#define Y second
#define pb push_back
#define fread(zcc) freopen(zcc,"r",stdin)
#define fwrite(zcc) freopen(zcc,"w",stdout)
using namespace std;
const int maxn=205;
const int inf=9999999;
int w[maxn][maxn];
int linker[maxn],lx[maxn],ly[maxn],slack[maxn];
bool visx[maxn],visy[maxn];
int nx,ny;
bool dfs(int x){
visx[x]=true;
for(int i=0;i<ny;i++){
if(visy[i])continue;
int tmp=lx[x]+ly[i]-w[x][i];
if(tmp==0){
visy[i]=true;
if(linker[i]==-1||dfs(linker[i])){
linker[i]=x;
return true;
}
}else if(slack[i]>tmp){
slack[i]=tmp;
}
}
return false;
}
int km(){
cl(linker,-1);
cl(ly,0);
for(int i=0;i<nx;i++){
lx[i]=-inf;
for(int j=0;j<ny;j++)if(w[i][j]>lx[i]){lx[i]=w[i][j];}
}
for(int x=0;x<nx;x++){
fill(slack,slack+ny+1,inf);
while(true){
cl(visx,false);
cl(visy,false);
if(dfs(x))break;
int d=inf;
for(int i=0;i<ny;i++)if(!visy[i]&&slack[i]<d){
d=slack[i];
}
for(int i=0;i<nx;i++)if(visx[i])lx[i]-=d;
for(int i=0;i<ny;i++)if(visy[i])ly[i]+=d;
else slack[i]-=d;
}
}
int ans=0;
for(int i=0;i<ny;i++)if(linker[i]!=-1){
ans+=w[linker[i]][i];
}
return ans;
}
map<string,int> mpX;
int cntX;
int getidX(string s){
if(mpX.count(s)){
return mpX[s];
}
else {
cntX++;
mpX[s]=cntX;
return cntX;
}
}
map<string,int> mpY;
int cntY;
int getidY(string s){
if(mpY.count(s)){
return mpY[s];
}
else {
cntY++;
mpY[s]=cntY;
return cntY;
}
}
char name[2][40];
int main(){
int n,m,k;
while(~scanf("%d%d%d",&n,&m,&k)){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
w[i][j]=-inf;
}
}
cntX=-1;
mpX.clear();
cntY=-1;
mpY.clear();
while(k--){
int v;
scanf("%s%s%d",name[0],name[1],&v);
int x,y;
x=getidX(name[0]);
y=getidY(name[1]);
w[x][y]=-v;
//printf("====%d %d %d\n",x,y,v);
}
nx=n;ny=m;
printf("%d\n",-km());
}
return 0;
}