首页 技术 正文
技术 2022年11月18日
0 收藏 312 点赞 3,312 浏览 3349 个字

Bribing FIPA

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 704    Accepted Submission(s): 251

Problem DescriptionThere 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. InputThe input consists of multiple test cases. Each test case starts with a line containing two integers n (1 ≤ n ≤ 200) and m (0 ≤ m ≤ n) 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.

 OutputFor 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 Input3 2
Aland 10
Boland 20 Aland
Coland 15
# Sample Output20 主要就是建立虚根0,构造一个树,然后常规的从父节点向下进行就可以了,细节见代码

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=388;
int n,m,v[maxn];
char str1[111],str2[112],str[1155];
int dp[310][310];
bool vis[310];
vector<int>vec[maxn];
map<string,int>mp;
int dfs(int u){
    int tot=1;
    dp[u][0]=0;
    dp[u][1]=v[u];
    for(int i=0;i<(int)vec[u].size();++i){
        tot+=dfs(vec[u][i]);
        for(int j=n;j>=0;–j)    //有01背包的意思,从后面向前面
            for(int k=0;k<=j;++k)
            dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[vec[u][i]][k]);
    }
    for(int i=1;i<=tot;++i)
        dp[u][i]=min(dp[u][i],v[u]);
    return tot;
}
int main(){
   while(gets(str)&&str[0]!=’#’){
    sscanf(str,”%d%d”,&n,&m);
    int top=1,x;
    memset(vis,0,sizeof(vis));
    for(int i=0;i<=n;++i) vec[i].clear();
    mp.clear();
    for(int i=1;i<=n;++i)
    {
        scanf(“%s%d”,str1,&x);
        if(!mp[str1]) mp[str1]=top++;
        v[mp[str1]]=x;
        gets(str);
        stringstream ss(str);
        while(ss>>str2) {
            if(!mp[str2]) mp[str2]=top++;
            vec[mp[str1]].push_back(mp[str2]);
            vis[mp[str2]]=1;
        }
    }
    v[0]=INF;
    for(int i=1;i<=n;++i) if(!vis[i]) vec[0].push_back(i);
    for(int i=0;i<=n;++i)
        for(int j=0;j<=n;++j)
        dp[i][j]=INF;
    dfs(0);
    printf(“%d\n”,*min_element(dp[0]+m,dp[0]+n+1));

}
}

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,405
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898