hunnuoj11370(运用状态压缩来剪枝的dfs)

地址:http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11370&courseid=0

Problem A: Kingdoms
Several kingdoms got into serious financial troubles. For many years, they have been secretly
borrowing more and more money from each other. Now, with their liabilities exposed, the crash
is inevitable...
There are n kingdoms. For each pair (A, B) of kingdoms, the amount of gold that kingdom
A owes to kingdom B is expressed by an integer number dAB (we assume that dBA = −dAB).
If a kingdom has negative balance (has to pay more than it can receive), it may bankrupt.
Bankruptcy removes all liabilities, both positive and negative, as if the kingdom ceased to exist.
The next kingdom may then bankrupt, and so on, until all remaining kingdoms are financially
stable.
Depending on who falls first, different scenarios may occur—in particular, sometimes only one
kingdom might remain. Determine, for every kingdom, whether it can become the only survivor.

Input
The first line of the input contains the number of test cases T. The descriptions of the test
cases follow:
The description of each test case starts with a line containing the number of the kingdoms n,
1 ¬ n ¬ 20. Then n lines follow, each containing n space-separated numbers. The j-th number
in the i-th line is the number dij of gold coins that the i-th kingdom owes to the j-th one. You
may assume that dii = 0 and dij = −dji for every 1 ¬ i, j ¬ n. Also, |dij | ¬ 10
6
for all possible
i, j.


Output
Print the answers to the test cases in the order in which they appear in the input. For each
test case, print a single line containing the indices of the kingdoms that can become the sole
survivors, in increasing order. If there are no such kingdoms, print a single number 0.


Input Output
1
3
0 -3 1
3 0 -2
-1 2 0
1 3

 

数据量较大,用暴力深搜超时,所以运用状态压缩来减去重复的部分。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int map[30][30],final[30],m,biao[1050000];
void dfs(int s,int k)
{
	int h=0,i,j;
	if(k==1)
	{
		for(h=1;h<=m;h++)
		{
			if(s%2==0) 
			{
				final[h]=1;
				break;
			}
			s=s>>1;
		}
		return ;
	}
	for(i=1;i<=m;i++)
	{
		int h=i-1,n=1,t=1;
		while(h--)
			n=n<<1;
		if(map[i][0]>0&&!biao[s|n])
		{
			for(j=1;j<=m;j++)
				map[j][0]-=map[j][i];
			biao[s|n]=1;
			dfs(s|n,k-1);
			for(j=1;j<=m;j++)
				map[j][0]+=map[j][i];
		}
	}
}
int main()
{
	int t,i,j,b;
	scanf("%d",&t);
	while(t--)
	{
		memset(map,0,sizeof(map));
		memset(biao,0,sizeof(biao));
		memset(final,0,sizeof(final));b=0;
		scanf("%d",&m);
		for(i=1;i<=m;i++)
		{
			for(j=1;j<=m;j++)
			{
				scanf("%d",&map[i][j]);
				map[i][0]+=map[i][j];
			}
		}
		dfs(0,m);
		for(i=1;i<=m;i++)
		{
			if(final[i])
			{
				if(!b) {printf("%d",i);b++;}
				else printf(" %d",i);
			}
		}
		if(!b) printf("0");
		printf("\n");
	}
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值