HDU 3696 FZU 2006 -- Farm Game SPFA最长路 2010福州区域赛

HDU2059龟兔赛跑-题解动态规划DP 龟兔赛跑<br />Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)<br />Total Submission(s): 3538    Accepted Submission(s): 1277<br /><br />Problem Description据说在很久很久以前,可怜的兔子经历了人生中最大的打击——赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州下沙某农业园卧薪尝胆潜心修 阅读详情
 

Farm Game

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 62768/32768 K (Java/Others)
Total Submission(s): 148 Accepted Submission(s): 65


Problem 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 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.


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.

Sample Input
  
2 2.5 10 5 0 1 2 1 0.5 2 2 2.5 10 5 0 1 2 1 0.8 2 0

Sample Output
  
25.00 40.00

Source

Recommend
chenyongfu
 
题意:有n种产品,每种产品有一定的价值和数量,
某些产品可以转换为另一种产品,有一定的转换率(转换中数量有损失)。
求最多能获得多少价值。
 
在转换过程中,转换率是要乘起来的,最后再乘上转换成的产品的价值,
如果这个价值比原来的大,就转换产品。
由于可以通过多种路径转换成另一种产品,所以用SPFA求最长路。
乘法通过log取对数转换为加法。
现在需要知道每个产品所能转换得到的最多价值,也就是求每个点到其他点最长路的最大值。
加一个附加点s,所有点连接到s,边权为log(p[i]),这样直接求每个点到s的最长路就可以知道最大值了。
这样需要n次SPFA,不过如果我们把图反向,求一次s开始的最长路,就可以知道所有点能转换的最大价值了。
最后再把这些值和以前的价值比较,得到每种产品的最大价值。
PS:由于是一个有向无环图,所以网上一些代码是DP或者拓扑排序。
 
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#define N 10005
using namespace std;

int n,m,num,adj[N],q[N];
bool f[N];
double low[N],p[N],w[N];
struct edge
{
    int v,pre;
    double w;
    edge(){}
    edge(int vv,double ww,int p){v=vv;w=ww;pre=p;}
}e[N*5];
void insert(int u,int v,double w)
{
    e[num]=edge(v,w,adj[u]);
    adj[u]=num++;
}
void spfa(int x)
{
    int i,v,head=0,tail=0;
    memset(f,0,sizeof(f));
    memset(low,-0x3f,sizeof(low));
    low[x]=0;
    q[++tail]=x;
    while(head!=tail)
    {
        x=q[head=(head+1)%N];
        f[x]=0;
        for(i=adj[x];~i;i=e[i].pre)
            if(low[v=e[i].v]<low[x]+e[i].w)
            {
                low[v]=low[x]+e[i].w;
                if(!f[v])
                {
                    f[v]=1;
                    q[tail=(tail+1)%N]=v;
                }
            }
    }
}
int main()
{
    while(scanf("%d",&n),n)
    {
        int i,k,a,b;
        double t,ans=0;
        num=0;
        memset(adj,-1,sizeof(adj));
        for(i=1;i<=n;i++)
            scanf("%lf%lf",p+i,w+i);
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d",&k);
            scanf("%d",&a);
            for(i=1;i<k;i++)
            {
                scanf("%lf%d",&t,&b);
                insert(b,a,log(t));
                a=b;
            }
        }
        for(i=1;i<=n;i++)
            insert(0,i,log(p[i]));
        spfa(0);
        for(i=1;i<=n;i++)
        {
            if(p[i]<exp(low[i]))
                p[i]=exp(low[i]);
            ans+=p[i]*w[i];
        }
        printf("%.2f\n",ans);
    }
}

环形m段最大子段和 题解动态规划DP http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=10581环形m段最大子段和Time Limit: 5000ms, Special Time Limit:15000ms, Memory Limit:65536KB Total submit users: 3, Accepted users: 3 Problem 10581 : No special judgementProblem description  我想你已经做出了一维的最大子段 阅读详情

相关推荐

Latex | IEEE双栏排版中长公式的排版

IEEE双栏排版中,有时候公式过于长了,过宽,导致单栏排版不下。

qq_43466146的博客 1万+

Hdu 3696 Farm Game(最长路)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3696 思路:每种商品可以直接卖掉,也可以换购后卖掉。所以设点n+1,从该点向每个商品连边权值为log(p[i])(将乘法转换为加法,直接使用SPFA)表示直接卖掉的单位价值;对于可以换购的商品 i-->j,连 j--> i 权值为log(b[j])的边(反向建图,只需求一次最长路),表示单位 i

哆啦AC梦的博客 864

LabVIEW QMH模板实战:从创建到错误处理的完整指南

本文提供LabVIEW QMH模板从创建到错误处理的完整实战指南。详细解析了队列消息处理器(QMH)作为LabVIEW核心设计模式如何实现任务解耦与异步通信,涵盖事件处理循环、消息处理循环的搭建、用户事件创建、队列引用管理、消息优先级控制以及健壮的错误处理机制,帮助开发者构建稳定、可维护的复杂应用程序。

white的博客 725

hdu3696(spfa求最长路)

链接:点击打开链接 题意:给出n个物品的价值和数量,有m个关系,每种关系的格式为(a0,b1,a1,b2,a2,…,bk-1,ak-1)表示1个ai可以转换成bi+1个ai+1,问最后n个物品转换后的最大价值和 代码:#include #include #include #include #include #include #include #include #include

stay_accept的专栏 1560

RGB接口电容触摸屏驱动(一)模块介绍

RGB接口电容触摸屏驱动(一)模块介绍

杰克拉力船长个人博客 7876

加法器设计—— 原理图与VHDL设计初步

1、在DE2-115开发板上,完成4位全加器系统的设计、下载和功能演示,并在FPGA上实现1位全加器;2、完成3-8译码器的设计、下载和功能演示(利用板上的8段数码管)本次实验在组合逻辑电路的基础上,运用Verilog编程实现了四位全加器和3-8译码器的设计,同时实现了在DE2-115开发板上的下载和功能演示。整个过程加深了我对电路设计和Verilog编程的理解,同时也锻炼了解决问题的能力。

m0_74749526的博客 1237

HDU - 3696 Farm Game SPFA最长路

题意:有n种产品,每种产品有一定的价值和数量, 某些产品可以转换为另一种产品,有一定的转换率(转换中数量有损失)。 求最多能获得多少价值。   在转换过程中,转换率是要乘起来的,最后再乘上转换成的产品的价值, 如果这个价值比原来的大,就转换产品。 由于可以通过多种路径转换成另一种产品,所以用SPFA求最长路。 乘法通过log取对数转换为加法。 现在需要知道每个产品所能转换得到的最多

Rainie1014的博客 590

保姆级教程:用Docker Compose一键部署WVP-PRO+ZLM+录像服务(含Nginx反代)

本文提供了一套完整的容器化GB28181视频平台部署方案,通过Docker Compose实现WVP-PRO、ZLMediaKit(ZLM)和录像服务的一键式部署。详细介绍了环境准备、架构设计、容器化部署实战以及高级配置优化,帮助用户快速搭建高性能视频监控管理平台,适用于安防行业和物联网设备管理场景。

weixin_30415113的博客 861

hdu3696Farm Game(SPFA求最长路)

题目请戳这里 题目大意:有n种物品,每种有相应的价值和数量。现在给m组关系链,组成一张关系网,表示某物品之间相互转换的关系。求已知的物品能获得的最大价值。 题目分析:每种物品可以直接卖掉,也可以通过转换成价值更高的物品再卖以获取更高的价值。对每个物品的单价建图,求每个物品通过转化所能获得的最大单价,然后再卖掉,可以获得最大价值。设一个源点s,s到每个物品建边,边权log10(p[i]),为相应

ophunter的专栏 1648

DDR5内存命令真值表详解:如何正确解析JESD79-5标准中的关键指令

本文深度解析JESD79-5标准中的DDR5内存命令真值表,将其视为动态通信协议进行拆解。详细阐述了1-Cycle与2-Cycle命令的区别、Bank Group寻址机制以及模式寄存器(MR)操作流程,为硬件与固件工程师提供了从信号解析到系统调试的完整实战指南,是理解和优化高性能计算平台内存子系统的核心。

numpy6sculptor的博客 548

NE555构成多谐振荡器及三角波、正弦波的生成(附工程文件)

NE555构成多谐振荡器及三角波、正弦波的生成 续上一篇:NE555基本原理及相关公式的推导

qq_49979053的博客 5万+

【思维-最长路】hdu 3696 Farm Game

Farm Game Problem 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 crop

Hi_jiaxinwei的博客 689

SolidWorks2020详细步骤安装教程

1、在安装软件前请断开网络连接,需要安装.NETFramework3.5和4.0,最后还要确保把之前安装的旧版本完全卸载,以管理员身份运行SolidWorks_Flexnet_Server\server_remove.bat,然后等到服务“SolidWorksFlexnet服务器”将被删除。12、先不要运行软件,找到_SolidSQUAD_文件夹,将文件夹“ProgramFiles\SOLIDWORKSCorp”中的所有破解文件复制替到“ProgramFiles\SOLIDWORKSCorp”目录下;

qq_53545309的博客 1万+

hdu 1541 Stars

。。。

禅境花园 411

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

SPFA

ACM模版堆栈实现参考题目链接POJ 3159 Candies代码const int INF = 0x3F3F3F3F; const int V = 30001; const int E = 150001;int pnt[E], cost[E], nxt[E]; int e, head[V]; int dist[V]; bool vis[V];int relax(int u, int v, int

逐梦者 1055

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

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

Farm Game Problem 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 kin...

weixin_30426957的博客 219

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

hdu 3696 Farm Game (拓扑排序)

题目大意:有n种物品,每种有相应的价值和数量。现在给m组关系链,组成一张关系网,表示某物品之间相互转换的关系。求已知的物品能获得的最大价值。 思路:如果暴力的话n*m是微微有点超时,所以拓扑排序,时间复杂度是n+m 给出一个转化关系 1->2 2->3 3->4.求出3->4的最优转化或者不转化是自己。2可以转化为3也可以转化为4,或者不转化是自己,2的最优就是不转化,或者转化为3->4的最优

但求心安的博客 336

POJ 3155 Hard Life 最大密度子图

Hard Life Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 4345   Accepted: 1226 Case Time Limit: 2000MS

魔神翼 3909

HDU 4069 Squiggly Sudoku 数独DLX 福州网络赛

Squiggly Sudoku Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 644 Accepted Submiss

魔神翼 3899

SOJ2113: 数字游戏题解动态规划DP

2113: 数字游戏Submit your solution     Discuss this problem     Best solutionsDescription 【问题描述】 丁丁最近沉迷于一个数字游戏之中。这个游戏看似简单,但丁丁在研究了许多天之后却发觉原来在简单的规则下想要赢得这个游戏并不那么容易。游戏是这样的,在你面前有一圈整数(一共n个),你要按顺序将其分为m个部分,各部分内的数字相加,相加所得的m个结果对10取模后再相乘,最终得到一个数k。游戏的要求是使你所得的k最大或者最小。 例如,对

魔神翼 3589

[SCOI2009]windy数 题解动态规划DP

[SCOI2009]windy数 Time Limit:1000MS  Memory Limit:165536KTotal Submit:162 Accepted:74 Descriptionwindy定义了一种windy数。 不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。 windy想知道,在A和B之间,包括A和B,总共有多少个windy数? Input包含两个整数,A B。 Output一个整数。 Sample Input【输入样例一】 1 10 【输入样例二】 25 50

魔神翼 3430

POJ3662 Telephone Lines 二分+最短路

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3468   Accepted: 1245 Description Farmer John wants to set up a telephone line at his farm.

魔神翼 3200

POJ3186Treats for the Cows题解动态规划DP

Treats for the CowsTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2686 Accepted: 1280<br />DescriptionFJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wan

魔神翼 3032

POJ1651Multiplication Puzzle题解动态规划DP

Multiplication PuzzleTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 3460 Accepted: 2034<br />DescriptionThe multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out

魔神翼 2831

HDU2821 Pusher HDU3500 Fling DFS搜索

Pusher Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others) Total Submission(s): 169 Accepted Submission(s): 6

魔神翼 2685
上一篇: POJ 3894 System Engineer 二分图匹配 Hopcroft_Carp 最大流
下一篇: POJ 3921 HDU 2485 Destroying the bus stations 最小费用最大流
power721
博客等级 码龄18年 176粉丝 135原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值