HDU 3696 Farm Game(拓扑+DP)(2010 Asia Fuzhou Regional Contest)
Description
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
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
题目大意:有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)的更多相关文章
- 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 ...
- 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 ...
- HDU 3697 Selecting courses(贪心+暴力)(2010 Asia Fuzhou Regional Contest)
Description A new Semester is coming and students are troubling for selecting courses. Students ...
- 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 ...
- 2010 Asia Fuzhou Regional Contest
A hard Aoshu Problem http://acm.hdu.edu.cn/showproblem.php?pid=3699 用深搜写排列,除法要注意,还有不能有前导零.当然可以5个for, ...
- HDU 3696 Farm Game(dp+拓扑排序)
Farm Game Problem Description “Farm Game” is one of the most popular games in online community. In t ...
- 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 ...
- 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),现在要从 ...
- 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 ...
随机推荐
- SpringBoot学习15:springboot异常处理方式5(通过实现HandlerExceptionResolver类)
修改异常处理方式4中的全局异常处理controller package com.bjsxt.exception; import org.springframework.context.annotati ...
- iOS之创建表格类视图WBDataGridView
项目中创建表格, 引用头文件 #import "WBDataGridView.h" - (void)viewDidLoad{ [superviewDidLoad]; // Do a ...
- 【2018 ICPC亚洲区域赛沈阳站 L】Tree(思维+dfs)
Problem Description Consider a un-rooted tree T which is not the biological significance of tree or ...
- BZOJ3098: Hash Killer II(构造)
Time Limit: 5 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 2162 Solved: 1140[Submit][Status][ ...
- Linux性能监控工具 gtop
给大家介绍一款性能监控工具,个人对比界面比top美观,常用指标比较清晰毕竟top上的指标不是每个人都能熟悉,也不是所有指标参数都需要看,对于新手也不便查找,好了说的再多先上图大家参观一下. 1.安装需 ...
- ELK初学搭建
目录:基础准备 修改相关系统配置 安装elasticsearch 安装 kibana 安装logstash X-pack插件的安装 登录网页查看 ELK名字解释 ELK就是ElasticSearch ...
- SpringBoot向outlook发送邮件
首先要登陆outlook邮箱,点击设置滑到最下面选择完整设置 进入后选择邮件->同步电子邮件 打开pop如上设置 下面是我的application.propertis设置 请填上自己的邮箱名与密 ...
- 23种java设计模式之装饰者模式及动态代理
设计模式不管对于何种语言都是存在的,这里介绍的是java的模式 装饰者模式是在二次开发中应用比较多的一款模式,当然了用反射也是可以实现的,今天介绍的是装饰模式,有兴趣的朋友可以自己去了解一下反射是怎么 ...
- 自定义vim配置文件vimrc,用于c/c++编程
vim作为Linux下广受赞誉的代码编辑器,其独特的纯命令行操作模式可以很大程度上方便编程工作,通过自定义vim配置文件可以实现对vim功能的个性化设置. vim配置文件一般有两份,属于root的/e ...
- 为什么我要放弃javaScript数据结构与算法(第三章)—— 栈
有两种结构类似于数组,但在添加和删除元素时更加可控,它们就是栈和队列. 第三章 栈 栈数据结构 栈是一种遵循后进先出(LIFO)原则的有序集合.新添加的或待删除的元素都保存在栈的同一端,称为栈顶,另一 ...