poj 2531 Network Saboteur (dfs)

有N個結點,要把所有的結點分爲兩個集合,不同集合內節點進行通訊有時間損失,求損失的最大值!

代碼如下:

#include<iostream>
using namespace std;
const int Max = 21;
const bool A= true;
const bool B= false;

int n,map[Max][Max],ans=0;
bool set[Max];

void dfs(int dep,int sum)
{
	if(dep >n)
	{
		if(sum>ans)
			ans=sum;
		return;
	}

	int i,m;
	m=0;                    //枚舉
	set[dep]=A;
	for(i=1;i<=dep;i++)
	if(set[i]==B)
	m+=map[i][dep];
	dfs(dep + 1,sum + m );




    m=0;
	set[dep]=B;
	for(i=1;i<=dep;i++)
	if(set[i]==A)
	m+=map[i][dep];
	dfs(dep+1,sum+m);
}
int main()       
{
    cin >> n;  
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= n; j ++)
            cin >> map[i][j];
    dfs(1, 0);
    cout << ans << endl;
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章