[ACM] HDU 3395 Special Fish (二分圖最大權匹配,KM演算法),hdu3395
Special Fish
Problem DescriptionThere is a kind of special fish in the East Lake where is closed to campus of Wuhan University. It’s hard to say which gender of those fish are, because every fish believes itself as a male, and it may attack one of some other fish who is believed to be female by it.
A fish will spawn after it has been attacked. Each fish can attack one other fish and can only be attacked once. No matter a fish is attacked or not, it can still try to attack another fish which is believed to be female by it.
There is a value we assigned to each fish and the spawns that two fish spawned also have a value which can be calculated by XOR operator through the value of its parents.
We want to know the maximum possibility of the sum of the spawns.
InputThe input consists of multiply test cases. The first line of each test case contains an integer n (0 < n <= 100), which is the number of the fish. The next line consists of n integers, indicating the value (0 < value <= 100) of each fish. The next n lines, each line contains n integers, represent a 01 matrix. The i-th fish believes the j-th fish is female if and only if the value in row i and column j if 1.
The last test case is followed by a zero, which means the end of the input.
OutputOutput the value for each test in a single line.
Sample Input
31 2 30111011100
Sample Output
6
Authormomodi@whu
SourceThe 5th Guangting Cup Central China Invitational Programming Contest
Recommendnotonlysuccess | We have carefully selected several similar problems for you: 1533 1853 2448 3388 3392
解題思路:
題意為有n條特殊的魚,每個魚都有一個價值,如果魚i ”認為“ 魚j 性別不同,那麼就攻擊它,繁殖的後代的價值為 v[i] ^ v[j], 每條魚只能攻擊或被攻擊一次,問最後繁殖的後代的最大價值為多少。
也是比較裸的二分圖最大權不匹配,邊i,j的權值等於 v[i] ^ v[j] 。
http://blog.csdn.net/sr_19930829/article/details/40650359
代碼:
#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;const int maxn=102;const int inf=0x3f3f3f;int nx,ny;//左右兩邊的點數int v[maxn];//每條魚的valueint g[maxn][maxn];//鄰接矩陣int linked[maxn];//右邊的點和左邊哪個點串連int lx[maxn],ly[maxn];//左右點的標號int slack[maxn];//slack[j]表示右邊的點j的所有不在匯出子圖的邊對應的lx[i]+ly[j]-w[i][j]的最小值bool visx[maxn],visy[maxn];bool DFS(int x)//hungary求增廣路{ visx[x]=true; for(int y=0;y<ny;y++) { if(visy[y]) continue; int tmp=lx[x]+ly[y]-g[x][y]; if(tmp==0) { visy[y]=true; if(linked[y]==-1||DFS(linked[y])) { linked[y]=x; return true; } } else if(slack[y]>tmp) slack[y]=tmp; } return false;}int KM(){ memset(linked,-1,sizeof(linked)); memset(ly,0,sizeof(ly)); for(int i=0;i<nx;i++) { lx[i]=-inf; for(int j=0;j<ny;j++) if(g[i][j]>lx[i]) lx[i]=g[i][j]; } for(int x=0;x<nx;x++) { for(int y=0;y<ny;y++) slack[y]=inf; while(true) { memset(visx,0,sizeof(visx)); memset(visy,0,sizeof(visy)); if(DFS(x)) break; int d=inf; for(int y=0;y<ny;y++) if(!visy[y]&&d>slack[y]) d=slack[y]; 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 y=0;y<ny;y++) { if(linked[y]!=-1) ans+=g[linked[y]][y]; } return ans;}int main(){ int n; while(scanf("%d",&n)!=EOF&&n) { nx=ny=n; for(int i=0;i<n;i++) scanf("%d",&v[i]); int ch; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { scanf("%1d",&ch);//輸入格式1d if(ch==1) g[i][j]=v[i]^v[j]; else g[i][j]=0; } } printf("%d\n",KM()); } return 0;}
hdu 1269 tarjan WA 了 解釋
沒有迴圈調用
kM演算法與匈牙利演算法的程式碼
//二分圖首選,kuhn munkras演算法,鄰接陣形式,複雜度O(m*m*n)
//返回首選值,傳入二分圖大小m,n和鄰接陣mat,表示權值
//match1,match2返回一個首選,未匹配頂點match值為-1
//一定注意m<=n,否則迴圈無法終止
//最小權匹配可將權值取相反數
#include <string.h>
#define MAXN 310
#define inf 1000000000
#define _clr(x) memset(x,0xff,sizeof(int)*n)int kuhn_munkras(int m,int n,int mat[][MAXN],int* match1,int* match2){
int s[MAXN],t[MAXN],l1[MAXN],l2[MAXN],p,q,ret=0,i,j,k;
for (i=0;i<m;i++)
for (l1[i]=-inf,j=0;j<n;j++)
l1[i]=mat[i][j]>l1[i]?mat[i][j]:l1[i];
for (i=0;i<n;l2[i++]=0);
for (_clr(match1),_clr(match2),i=0;i<m;i++){
for (_clr(t),s[p=q=0]=i;p<=q&&match1[i]<0;p++)
for (k=s[p],j=0;j<n&&match1[i]<0;j++)
if (l1[k]+l2[j]==mat[k][j]&&t[j]<0){
s[++q]=match2[j],t[j]=k;
if (s[q]<0)
for (p=j;p>=0;j=p)
match2[j]=k=t[j],p=match1[k],match1[k]=j;
}
if (match1[i]<0){
for (i--,p=inf,k=0;k<=q;k++)
for (j=0;j<n;j++)
if (t[j]<0&&l1[s[k]]+l2[j]-mat[s[k]][j]<p)
p=l1[s[k]]+l2[j]-mat[s[k]][j];
for (j=0;j<n;l2[j]+=t[j]<0?0:p,j++);
for (k=0;k<=q;l1[s[k++]]-=p);
}
}
for (i=0;i<m;i++)
ret+=mat[i][match1[i]];
return ret;
}===================================================================//二分圖最大匹配,hungary演算法,鄰接陣形式,複雜度O(m*m*n)
//返回最大匹配數,傳入二分圖大小m,n和鄰接陣mat,非零元素表示有邊
//match1,match2返回一個最大匹配,未匹配頂點match值為-1
#include <string.h>
#define MAXN 310
#define _clr(x) memset(x,0xff,sizeof(i......餘下全文>>