Description

“Farm Game” is one of the most popular games in online community. In the community each player has a virtual farm. The farmer can decide to plant some kinds of crops like wheat or paddy, and buy the corresponding crop seeds. After they grow up, The farmer can harvest the crops and sell them to gain virtual money. The farmer can plant advanced crops like soybean, watermelon or pumpkin, as well as fruits like lychee or mango. 
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.
 

Input

The input contains several test cases. The first line of each test case contains an integers N (N<=10000) representing that there are N kinds of products in Jack’s farm. The product categories are numbered for 1 to N. In the following N lines, the ith line contains two real numbers p and w, meaning that the price for the ith kind of product is p per pound and Jack has w pounds of the ith kind of product.
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 a0, b1, a1, b2, a2, …, bk-1, ak-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 ai-1can 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.
 

Output

For each test case, print a line with a real number representing the maximum amount of money that Jack can get. The answer should be rounded to 2 digits after decimal point. We guarantee that the answer is less than 10^10. 

题目大意:有n种水果,每种水果有价格pi和重量wi,然后一种水果一个可以换成另一种水果bi个(题目给定,无环),问这些水果最多能卖多少。

思路:按水果的交换连边,因为无环可以形成拓扑结构,每次都选最优的交换即可。

PS:之前用了个%d来读重量结果不断TLE……原来这样也会TLE又涨姿势了……

代码(187MS):

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std; const int MAXN = ;
const int MAXE = ; double p[MAXN], w[MAXN];
int head[MAXN], indeg[MAXN];
int to[MAXE], next[MAXE];
double b[MAXE];
int n, m, ecnt, k; void init() {
memset(head, , sizeof(head));
memset(indeg, , sizeof(indeg));
ecnt = ;
} void add_edge(int u, int v, double bi) {
to[ecnt] = v; b[ecnt] = bi; next[ecnt] = head[u]; head[u] = ecnt++;
++indeg[v];
} stack<int> stk; double solve() {
double ret = ;
for(int i = ; i <= n; ++i)
if(indeg[i] == ) stk.push(i);
while(!stk.empty()) {
int u = stk.top(); stk.pop();
ret += p[u] * w[u];
for(int q = head[u]; q; q = next[q]) {
int &v = to[q];
p[v] = max(p[v], p[u] * b[q]);
if(--indeg[v] == ) stk.push(v);
}
}
return ret;
} int main() {
while(scanf("%d", &n) != EOF && n) {
init();
for(int i = ; i <= n; ++i) scanf("%lf%lf", &p[i], &w[i]);
scanf("%d", &m);
while(m--) {
scanf("%d", &k);
int tmpa, pre; double tmpb;
scanf("%d", &pre);
for(int i = ; i < k; ++i) {
scanf("%lf%d", &tmpb, &tmpa);
add_edge(tmpa, pre, tmpb);
pre = tmpa;
}
}
printf("%.2f\n", solve());
}
}

HDU 3696 Farm Game(拓扑+DP)(2010 Asia Fuzhou Regional Contest)的更多相关文章

  1. HDU 3698 Let the light guide us(DP+线段树)(2010 Asia Fuzhou Regional Contest)

    Description Plain of despair was once an ancient battlefield where those brave spirits had rested in ...

  2. HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)

    Description Aliens on planet Pandora also write computer programs like us. Their programs only consi ...

  3. HDU 3697 Selecting courses(贪心+暴力)(2010 Asia Fuzhou Regional Contest)

    Description     A new Semester is coming and students are troubling for selecting courses. Students ...

  4. HDU 3699 A hard Aoshu Problem(暴力枚举)(2010 Asia Fuzhou Regional Contest)

    Description Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. N ...

  5. 2010 Asia Fuzhou Regional Contest

    A hard Aoshu Problem http://acm.hdu.edu.cn/showproblem.php?pid=3699 用深搜写排列,除法要注意,还有不能有前导零.当然可以5个for, ...

  6. HDU 3696 Farm Game(dp+拓扑排序)

    Farm Game Problem Description “Farm Game” is one of the most popular games in online community. In t ...

  7. HDU 3689 Infinite monkey theorem(DP+trie+自动机)(2010 Asia Hangzhou Regional Contest)

    Description Could you imaging a monkey writing computer programs? Surely monkeys are smart among ani ...

  8. dp --- 2014 Asia AnShan Regional Contest --- HDU 5074 Hatsune Miku

    Hatsune Miku Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5074 Mean: 有m种音符(note),现在要从 ...

  9. HDU 3685 Rotational Painting(多边形质心+凸包)(2010 Asia Hangzhou Regional Contest)

    Problem Description Josh Lyman is a gifted painter. One of his great works is a glass painting. He c ...

随机推荐

  1. PL/SQL 用户自定义子类型

    子类型具有与其基本类型相同的操作,但只有基本类型有效值的子集. 例如,PL/SQL预先定义子类型CHARACTER和INTEGER,如下所示: SUBTYPE CHARACTER IS CHAR; S ...

  2. 编写可维护的JavaScript之编程风格

    在团队中只有每个人的编程风格一致,大家才能方便的互相看懂和维护对方的代码. 1. 层级缩进 对于层级缩进目前有两种主张:1)使用制表符这种方法有两种好处,第一,制表符和缩进层级之间是一一对应关系,符合 ...

  3. 数组reduce方法以及高级技巧

    基本概念: reduce()方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终为一个值. reduce为数组中的每一个元素依次执行回调函数.不包括数组中被删除或从未赋值的元素,接受两 ...

  4. CPU运行的流程

  5. 【PTA 天梯赛训练】词频统计(map+vector)

    请编写程序,对一段英文文本,统计其中所有不同单词的个数,以及词频最大的前10%的单词. 所谓“单词”,是指由不超过80个单词字符组成的连续字符串,但长度超过15的单词将只截取保留前15个单词字符.而合 ...

  6. git设置.gitignore文件

    .gitignore用来忽略某些git仓库中不需要上传到远程仓库的文件,例如target目录.下面说一下步骤. 1.在项目根目录中通过右键Git Bash,打开控制命令台,新建一个.gitignore ...

  7. php 微信客服信息推送失败 微信重复推送客服消息 40001 45047

    /*** * 微信客服发送信息 * 微信客服信息推送失败 微信重复推送客服消息 40001 45047 * 递归提交到微信 直到提交成功 * @param $openid * @param int $ ...

  8. 使用C6748和C5509A对nRF24L01驱动进行数据传输

    1. 写在前面 今天下午做了一个C5509A和C6748两个DSP的数据传输,经由RF24L01设备传输,都是模拟SPI协议,对于两个DSP来说,无非是配GPIO引脚,写好时序和延时.C5509A的G ...

  9. phpcms2008网站漏洞如何修复 远程代码写入缓存漏洞利用

    SINE安全公司在对phpcms2008网站代码进行安全检测与审计的时候发现该phpcms存在远程代码写入缓存文件的一个SQL注入漏洞,该phpcms漏洞危害较大,可以导致网站被黑,以及服务器遭受黑客 ...

  10. makefile = 与 := 的区别

    “=” make会将整个makefile展开后,再决定变量的值.也就是说,变量的值将会是整个makefile中最后被指定的值.看例子: x = foo            y = $(x) bar  ...