hdu2813解題報告

來源:互聯網
上載者:User

//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;}
個人愚昧觀點,歡迎指正與討論

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.