吐槽 isap/ 转dinic

  

       经典问题:方格取数,二分图的最大权独立集,直接把我的isap卡的天崩地裂。。。。。。

      

        >.<!话说当时写现在用的程序是以前看了某某的博客,加上YY了一顿,no 当前弧优化, 单路增广,递归版。。。。。。仅仅加了个gap优化>.<

        就这样还是挤过了秋哥的“最大流强数据”,还踩掉了hyc的多路增广dinic = 。 =!结果这次一个普通的二分图就把我卡了。。。。。。

        

         以前那个丑程序就不贴了=。=!

 

         热烈祝贺 从递归版单路增广无当前弧isap  转变为 非递归版多路增广加当前弧dinic(转变真大), 感谢lzn 提供程序,以及lyp,hyc 提供不同的代码对比长度。

  

         3个过程一共37 行,无奇葩缩行,很好理解。 

     速度快,空间小,代码短,真可谓是居家旅行之必备呀.

# include <cstdlib>
# include <cstdio>
# include <cmath>
#include <ctime>
#include <cstring>
using namespace std;

const int maxn = 110, maxm = 105000, oo=1073741819;
int top, next[maxm*2], point[maxm*2], linke[maxm*2], wis[maxm*2], a[maxn][maxn];
int tot, p, tab[maxm], prev[maxm], shift[maxm], flow, s, t, maxflow, n, m;
int mx[10], my[10];
int l, r, que[maxm], step[maxm], adm[maxm];
bool flag;
 
inline int min(int x, int y){return x<y?x:y;};
void link(int x, int y, int z)
{
	++top; next[top] = linke[x]; linke[x] = top; point[top] = y; wis[top] = z;
	++top; next[top] = linke[y]; linke[y] = top; point[top] = x; wis[top] = 0;
}

bool bfs()
{
	memset(tab, -1, sizeof(tab));
	int l = 1, r = 1; que[l] = s; tab[s] = 1;
	for (;l <= r;l++)
	  for (int ke = linke[que[l]]; ke; ke = next[ke])
      {
	    if (tab[point[ke]]==-1 && wis[ke])
	      tab[que[++r] = point[ke]] = tab[que[l]] + 1;
	    if (que[r] == t) return true;
	  }
	return false;
}

void improve()
{
	flow = oo;
	for (int x = prev[t]; x ;x = prev[x])
	  if (wis[shift[x]] < flow) p = x, flow = wis[shift[x]];
	for (int x = prev[t]; x ;x = prev[x])
	  wis[shift[x]] -= flow, wis[shift[x]^1] += flow;
	maxflow += flow;
}
void dfs()
{
	memcpy(shift, linke, sizeof(shift));
	for (int x = s, y;x ;)
	{
		int flag = false;
		for (int ke = shift[x]; ke; ke = next[ke])
		  if (wis[ke] && (tab[y = point[ke]] == tab[x] + 1))
		  {
				prev[y] = x; shift[x] = ke; x = y; flag = true;
				if (x == t) improve(), x = p;
				if (flag) break;
		  }
		if (!flag) tab[x] = -1,x = prev[x]; 
	}
}
int main()
{
	int i, j, k, ii, jj;
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	scanf("%d%d",&n, &m); top = 1;
	mx[1]=1; mx[2]=0; mx[3]=-1; mx[4]=0;
	my[1]=0; my[2]=1; my[3]=0; my[4]=-1;
	for (i = 1; i <= n; i++)
	  for (j = 1; j <= m; j++)
	    scanf("%d", &a[i][j]), tot+= a[i][j];
	s = n*m+1; t = s+1;
	for (i = 1; i <= n; i++)
	  for (j = 1; j <= m; j++)
	    if (i+j&1) 
		   link(s, (i-1)*m+j, a[i][j]);
	    else 
		   link((i-1)*m+j, t, a[i][j]);
	for (i = 1; i <= n; i++)
	  for (j = 1; j <= m; j++)
	   if (i+j&1)
	    for (k = 1; k <= 4; k++)
	    {
			ii = i+mx[k]; jj= j+my[k];
			if (ii>=1&&ii<=n&&jj>=1&&jj<=m) link((i-1)*m+j, (ii-1)*m+jj, oo);
		}
	memcpy (adm, linke, sizeof(adm));
	while ( bfs()) 
	  dfs();
	printf("%d", tot-maxflow);
	return 0;
}


          话说考场上看出来问题是一个二分图最大权独立集的模型,但是不会算法=。=! 下星期强力强力恶补图论!!!!!!

你可能感兴趣的:(吐槽 isap/ 转dinic)