//765MS 492K #include<cstdio>#include<cstring>#include<string>#include<iostream>#include<map>using namespace std;#define MAX 3#define INF 1<<30-1int n,m,k;int w[MAX][MAX];int lx[MAX],ly[MAX],visx[MAX],visy[MAX],slack[MAX],link[MAX];bool dfs(int x){visx[x] = 1;for(int y = 0; y < m; y ++){if(visy[y]) continue;int t = lx[x] + ly[y] - w[x][y];if(t == 0){visy[y] = 1;if(link[y] == -1 || dfs(link[y])){link[y] = x; return true;}}else if(slack[y] > t)slack[y] = t;}return false;}int KM(){memset(link,-1,sizeof(link));memset(ly,0,sizeof(ly));for(int i = 0; i < n; i ++){lx[i] = -INF;for(int j = 0 ; j < m; j ++)if(lx[i] < w[i][j])lx[i] = w[i][j];}for(int x = 0; x < n; x ++){for(int k = 0; k < m; k ++) slack[k] = INF;while(1){memset(visx,0,sizeof(visx));memset(visy,0,sizeof(visy));if(dfs(x)) break;int d = INF;for(int i = 0; i < m; i ++)if(!visy[i] && slack[i] < d) d = slack[i];for(int j = 0; j < m; j ++){if(visx[j]) lx[j] -= d;if(visy[j]) ly[j] += d;else slack[j] -= d;}}}int ans = 0;for(int k = 0; k < m; k ++){if(link[k] != -1)ans += w[link[k]][k];}return ans;}int main(){string str1,str2;int t;//人名和傷害值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;}map<string,int>m1,m2;map<string,int>::iterator it;int sum1 = 0,sum2 = 0;//對呂布。曹操人的編號,從0開始while(k --){int a,b;cin>>str1>>str2>>t;it = m1.find(str1);if(it == m1.end()){a = sum1++;m1[str1] = a;}elsea = (*it).second;it = m2.find(str2);if(it == m2.end()){b = sum2++;m2[str2] = b;}elseb = (*it).second;w[a][b] = -t; //}printf("%d\n",-KM());}return 0;}
個人愚昧觀點,歡迎指正與討論