POJ 3345 Bribing FIPA

Bribing FIPA
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3209   Accepted: 1016

Description

There is going to be a voting at FIPA (Fédération Internationale de Programmation Association) to determine the host of the next IPWC (International Programming World Cup). Benjamin Bennett, the delegation of Diamondland to FIPA, is trying to seek other delegation's support for a vote in favor of hosting IWPC in Diamondland. Ben is trying to buy the votes by diamond gifts. He has figured out the voting price of each and every country. However, he knows that there is no need to diamond-bribe every country, since there are small poor countries that take vote orders from their respected superpowers. So, if you bribe a country, you have gained the vote of any other country under its domination (both directly and via other countries domination). For example, if C is under domination of B, and B is under domination of A, one may get the vote of all three countries just by bribing A. Note that no country is under domination of more than one country, and the domination relationship makes no cycle. You are to help him, against a big diamond, by writing a program to find out the minimum number of diamonds needed such that at least m countries vote in favor of Diamondland. Since Diamondland is a candidate, it stands out of the voting process.

Input

The input consists of multiple test cases. Each test case starts with a line containing two integers n (1 ≤ n ≤ 200) and m (0 ≤ mn) which are the number of countries participating in the voting process, and the number of votes Diamondland needs. The next n lines, each describing one country, are of the following form:

CountryName DiamondCount DCName1 DCName1 ...

CountryName, the name of the country, is a string of at least one and at most 100 letters and DiamondCount is a positive integer which is the number of diamonds needed to get the vote of that country and all of the countries that their names come in the list DCName1 DCName1 ... which means they are under direct domination of that country. Note that it is possible that some countries do not have any other country under domination. The end of the input is marked by a single line containing a single # character.

Output

For each test case, write a single line containing a number showing the minimum number of diamonds needed to gain the vote of at least m countries.

Sample Input

3 2
Aland 10
Boland 20 Aland
Coland 15
#

Sample Output

20


树形DP

输入的数据处理起来有点麻烦,就像模拟题。。

数据是一个深林,对每一颗树进行DP,d[i][j]代表以结点i为根的树取j个结点的最小花费。

每个结点的处理就像是分组背包。

最后假设所有的树的根是以0为根的子结点,然后分组背包,和树上的每个结点的处理类似。


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<set>
#include<vector>

using namespace std;

#define Min_(a,b) (a<b?a:b)

int N,m;
char ch[11000];
bool hash[210],vis[210];
int d[210][210];
int ss[210];

struct Node{
	char name[110];
	int v;
}num[210];

struct Son{
	char name[110];
	int flag;
};

vector<Son> son[210];

void DFS(int x)
{
	int i,j,k;
	ss[x]=1;
	for(i=0;i<son[x].size();i++)
	{
		if(vis[son[x][i].flag])
			continue;
		int u=son[x][i].flag;
		vis[u]=1;
		DFS(u);
		vis[u]=0;
		ss[x]+=ss[u];
	}
	d[x][0]=0;

	for(i=0;i<son[x].size();i++)
	{
		if(vis[son[x][i].flag])
			continue;
		int u=son[x][i].flag;
		vis[u]=1;

		for(j=m;j>=0;j--)
		{
			for(k=0;k<=m;k++)
			{
				if(d[u][k]!=-1 && d[x][j-k]!=-1)
				{
					if(d[x][j]==-1)
						d[x][j]=d[x][j-k]+d[u][k];
					else
						d[x][j]=Min_(d[x][j],d[x][j-k]+d[u][k]);
				}
			}
		}
	}
	for(i=0;i<=ss[x];i++)
	{
		if(d[x][i]!=-1)
			d[x][i]=Min_(d[x][i],num[x].v);
		else
			d[x][i]=num[x].v;
	}
}

int main()
{
	int i,j,k,len; 
	char tmp[110];


	while(gets(ch),ch[0]!='#')
	{
		sscanf(ch,"%d%d",&N,&m);
		for(i=1;i<=N;i++)
		{
			gets(ch);
			sscanf(ch,"%s",tmp);
			strcpy(num[i].name,tmp);
			
			len=strlen(tmp);
			while(*(ch+len)==' ' || *(ch+len)=='	')
			{
				len++;
			}
			sscanf(ch+len,"%s",tmp);
			num[i].v=atoi(tmp);
			len+=strlen(tmp);
			while(*(ch+len)==' ' || *(ch+len)=='	')
			{
				len++;
			}

			Son a;
			son[i].clear();
			while(sscanf(ch+len,"%s",tmp)==1)
			{
				strcpy(a.name,tmp);	a.flag=-1;
				son[i].push_back(a);
				len+=strlen(tmp);
				while(*(ch+len)==' ' || *(ch+len)=='	')
				{
					len++;
				}
			}
		}

		memset(hash,0,sizeof(hash));
		for(i=1;i<=N;i++)
		{
			for(j=0;j<son[i].size();j++)
				for(k=1;k<=N;k++)
					if(strcmp(son[i][j].name,num[k].name)==0)
					{
						son[i][j].flag=k;
						hash[k]=1;
						break;
					}
		}
		

		memset(vis,0,sizeof(vis));
		memset(d,-1,sizeof(d));
		d[0][0]=0;
		for(i=1;i<=N;i++)
		{
			if(!hash[i])
			{
				vis[i]=1;
				DFS(i);

				for(j=m;j>=0;j--)
				{
					for(k=0;k<=j;k++)
						if(d[i][k]!=-1 && d[0][j-k]!=-1)
						{
							if(d[0][j]==-1)
								d[0][j]=d[0][j-k]+d[i][k];
							else
								d[0][j]=Min_(d[0][j],d[0][j-k]+d[i][k]);
						}
				}
			}
		}
		
		printf("%d\n",d[0][m]);
	}
	return 0;
}


你可能感兴趣的:(POJ 3345 Bribing FIPA)