HDOJ 1281 – 棋盤遊戲 快兩年沒寫二分圖匹配了~回顧下匈牙利

來源:互聯網
上載者:User

                題解:

                         真相是被splay和動態樹虐跪了...刷寫水題調整下鬱悶得不行的心態.... 匈牙利演算法我早忘了..二分圖嘛~~不管是最大匹配還是最大匹配最小代價~什麼匈牙利啊~KM啊都可以閃..有網路流就夠了....話是什麼說..但是匈牙利演算法寫起來還是比最簡單的網路流的寫法方便...效率也更高吧~~回顧下感覺不錯~

                         本題..(x,y)配對~經典構圖..找出最大匹配後..枚舉去掉某個點.看是否最大匹配減少了...

Program:

#include<iostream>#include<stdio.h>#include<algorithm>#include<cmath>#include<stack>#include<queue>#define ll long long#define MAXN 105using namespace std;int n,m,line[MAXN*MAXN][2],match[MAXN];bool arc[MAXN][MAXN],used[MAXN];bool dfs(int x){       int i;       for (i=1;i<=m;i++)          if (arc[x][i] && !used[i])          {                 used[i]=true;                 if (!match[i] || dfs(match[i]))                 {                       match[i]=x;                       return true;                 }          }              return false; }int getmax(){       int sum=0;       memset(match,0,sizeof(match));       for (int i=1;i<=n;i++)       {               memset(used,false,sizeof(used));               sum+=dfs(i);       }       return sum;}int main(){       int i,k,cases,ans1,ans2;        cases=0;       while (~scanf("%d%d%d",&n,&m,&k))       {               memset(arc,false,sizeof(arc));               for (i=1;i<=k;i++)                 {                       int x,y;                       scanf("%d%d",&x,&y);                       arc[x][y]=true,line[i][0]=x,line[i][1]=y;               }               ans2=getmax(),ans1=0;               for (i=1;i<=k;i++)               {                       int x=line[i][0],y=line[i][1];                       arc[x][y]=false;                       int temp=getmax();                       arc[x][y]=true;                       if (temp<ans2) ans1++;               }               printf("Board %d have %d important blanks for %d chessmen.\n",++cases,ans1,ans2);       }       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.