HDU 3696 Farm Game(dp+拓扑排序)
Farm Game
Feeding animals is also allowed. The farmer can buy chicken, rabbits or cows and feeds them by specific crops or fruits. For example, chicken eat wheat. When the animals grow up, they can also “output” some products. The farmer can collect eggs and milk from hens and cows. They may be sold in a better price than the original crops.
When the farmer gets richer, manufacturing industry can be set up by starting up some machines. For example, Cheese Machine can transfer milk to cheese to get better profits and Textile Machine can spin cony hair to make sweaters. At this time, a production chain appeared in the farm.
Selling the products can get profits. Different products may have different price. After gained some products, the farmer can decide whether to sell them or use them as animal food or machine material to get advanced products with higher price.
Jack is taking part in this online community game and he wants to get as higher profits as possible. His farm has the extremely high level so that he could feed various animals and build several manufacturing lines to convert some products to other products.
In short, some kinds of products can be transformed into other kinds of products. For example, 1 pound of milk can be transformed into 0.5 pound of cheese, and 1 pound of crops can be transformed into 0.1 pound of eggs, etc. Every kind of product has a price. Now Jack tell you the amount of every kind of product he has, and the transform relationship among all kinds of products, please help Jack to figure out how much money he can make at most when he sell out all his products.
Please note that there is a transforming rule: if product A can be transformed into product B directly or indirectly, then product B can never be transformed into product A, no matter directly or indirectly.
Then there is a line containing an integer M (M<=25000) meaning that the following M lines describes the transform relationship among all kinds of products. Each one of those M lines is in the format below:
K a
0, b
1, a
1, b
2, a
2, …, b
k-1, a
k-1
K is an integer, and 2×K-1 numbers follows K. ai is an integer representing product category number. bi is a real number meaning that 1 pound of product a
i-1 can be transformed into bi pound of product ai.
The total sum of K in all M lines is less than 50000.
The input file is ended by a single line containing an integer 0.
2.5 10
5 0
1
2 1 0.5 2
2
2.5 10
5 0
1
2 1 0.8 2
0
40.00
题目大意:
解题思路:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std; const int maxn=11000;
struct edge{
int u,v,next;
double w;
edge(int u0=0,int v0=0,double w0=0){
u=u0;v=v0;w=w0;
}
}e[maxn*5];
double value[maxn],w[maxn];
int n,degree[maxn],head[maxn],cnt; void initial(){
for(int i=0;i<=n;i++){
degree[i]=0;
head[i]=-1;
}
cnt=0;
} void adde(int u,int v,double w){
e[cnt]=edge(u,v,w);
e[cnt].next=head[u];
head[u]=cnt++;
degree[v]++;
} void input(){
int m,k,u0,v0;
double w0;
for(int i=1;i<=n;i++){
scanf("%lf%lf",&value[i],&w[i]);
}
scanf("%d",&m);
while(m-- >0){
scanf("%d%d",&k,&v0);
for(int i=0;i<k-1;i++){
scanf("%lf%d",&w0,&u0);
adde(u0,v0,w0);
v0=u0;
}
}
} void topSort(){
int s,d;
queue <int> q;
for(int i=1;i<=n;i++){
if(degree[i]==0) q.push(i);
}
while(!q.empty()){
s=q.front();
q.pop();
for(int i=head[s];i!=-1;i=e[i].next){
d=e[i].v;degree[d]--;
value[d]=max(value[d],value[s]*e[i].w);
if(degree[d]==0){
q.push(d);
}
}
}
} void computing(){
double ans=0;
topSort();
for(int i=1;i<=n;i++){
ans+=w[i]*value[i];
}
printf("%.2f\n",ans);
} int main(){
while(scanf("%d",&n)!=EOF && n){
initial();
input();
computing();
}
return 0;
}
HDU 3696 Farm Game(dp+拓扑排序)的更多相关文章
- hdu 3696 10 福州 现场 G - Farm Game DP+拓扑排序 or spfa+超级源 难度:0
Description “Farm Game” is one of the most popular games in online community. In the community each ...
- HDU 3696 Farm Game(拓扑+DP)(2010 Asia Fuzhou Regional Contest)
Description “Farm Game” is one of the most popular games in online community. In the community each ...
- HDU.3342 Legal or Not (拓扑排序 TopSort)
HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...
- HDU.1285 确定比赛名次 (拓扑排序 TopSort)
HDU.1285 确定比赛名次 (拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 只不过这道的额外要求是,输出字典序最小的那组解.那么解决方案就是 ...
- The Preliminary Contest for ICPC Asia Nanjing 2019 - D Robots(概率dp+拓扑排序)
这题概率dp + 拓扑排序可以写 改天补解释 #include <bits/stdc++.h> using namespace std; const int maxn=1e5+10; ve ...
- HDU 4857 逃生 (反向拓扑排序 & 容器实现)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- ACM: HDU 1285 确定比赛名次 - 拓扑排序
HDU 1285 确定比赛名次 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
- ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线
hdu 1811 Rank of Tetris Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- hdu 1285 确定比赛名次 拓扑排序
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛 ...
- cf919D 线性dp+拓扑排序
/* 给定一张有向图,图上每个结点都有一个字符,现在要求出一条路径,要使路径上某字符出现的次数最多 如果有环,输出-1即可 拓扑排序+dp dp[i][26]表示排序到结点i时26个字符出现的次数 在 ...
随机推荐
- 信息熵 Information Theory
信息论(Information Theory)是概率论与数理统计的一个分枝.用于信息处理.信息熵.通信系统.数据传输.率失真理论.密码学.信噪比.数据压缩和相关课题.本文主要罗列一些基于熵的概念及其意 ...
- curl文件上传有两种方式,一种是post_fileds,一种是infile
curl文件上传有两种方式,一种是POSTFIELDS,一种是INFILE,POSTFIELDS传递@实际地址,INFILE传递文件流句柄! );curl_setopt($ch, CURLOPT_PO ...
- replicate-do-db参数引起的MySQL复制错误及处理办法
replicate-do-db配置在MySQL从库的my.cnf文件中,可以指定只复制哪个库的数据.但是这个参数有个问题就是主库如果在其他的schema环境下操作,其binlog不会被从库应用,从而出 ...
- MongoDB配置客户端
新建mongodb27017.bat文件 内容为: mongo 127.0.0.1:27017/admin 连接成功! 来自为知笔记(Wiz)
- 用vs2012的命令利用xsd文件生成对应的C#类,把xml的string类型映射到生成的类
输入命令: xsd d:\TDDOWNLOAD\atom-author-link.xsd /c /language:C# /outputdir:d:\ 含义: 将d:\TDDOWNLOAD\atom- ...
- 解析AFNetWorking 网络框架(二)
转:http://blog.csdn.net/andy_jiangbin/article/details/17114989 接着前面写. 本帖先讲AFURLConnectionOperation,它是 ...
- struts2异常处理,global-results定义全局结果处理
<global-results>定义全局结果处理 一般发生异常之后 结果返回errHandler 因为errHandler是由<global-exception-mappings&g ...
- centreon load average 的含义
下面图是centreon监控到的 load 信息 其中的 load1,load5,load15 分别说明上一分钟.最后五分钟以及最后十五分钟的系统负载均值.它们的数字当然是越小越好.数字越高,说明服务 ...
- ORA-12516: TNS: 监听程序无法找到匹配协议栈的可用句柄解决方法
1.查看当前连接进程数SQL>select count(*) from v$process;2.查看连接数上限SQL>select value from v$parameter where ...
- FreeMarker笔记 第二章 数值和类型
2.1 基本内容 2.1.1 简介 2.1.2 什么是数值 和程序语言中的数值类型是相似的. 2.1.3 什么是类型? 2.1.4 数据模型是哈希表 2.2 类型 2.2.1 简介 2.2.2 标量 ...