hdu 3696 10 福州 现场 G - Farm Game DP+拓扑排序 or spfa+超级源 难度:0
2014-11-10 19:55
387 查看
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
拓扑+dp或者spfa最长路+超级源点都行
我的思路是spfa最长路,建立超级源点,单位为1,到各点边权为数量值,就能跑出最大转换数量,接着对每种商品分别取获利最大的那种转换,队友是拓扑+dp
io07熊代码:
“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
拓扑+dp或者spfa最长路+超级源点都行
我的思路是spfa最长路,建立超级源点,单位为1,到各点边权为数量值,就能跑出最大转换数量,接着对每种商品分别取获利最大的那种转换,队友是拓扑+dp
io07熊代码:
#include <cstdio> #include <cstdlib> #include <iostream> #include <cstring> using namespace std; const int maxn=15000; const int maxe=102000; typedef pair<double,double> PP; struct edge { double cc; int to,next; } P[maxe]; int head[maxn],si; PP thing[maxn]; int nn; int c[maxn],topo[maxn],t; //about topo double dp[maxn]; void add_edge(int s,int t,double cc) { P[si].to=t; P[si].cc=cc; P[si].next=head[s]; head[s]=si++; } bool dfs(int u) { c[u]=-1; for(int i=head[u];i!=-1;i=P[i].next){ int v=P[i].to; if(c[v]<0) return false; else if(!c[v]&&!dfs(v)) return false; } c[u]=1; topo[--t]=u; return true; } bool toposort() { t=nn; memset(c,0,sizeof(c)); for(int i=0;i<nn;i++){ if(!c[i]) if(!dfs(i)) return false; } return true; } void Dp(int u) { if(dp[u]!=0.0) return ; dp[u]=thing[u].first; for(int i=head[u];i!=-1;i=P[i].next){ int v=P[i].to; Dp(v); dp[u]=max(dp[u],dp[v]*P[i].cc); } } int main() { int mm,kk,s,t; double cc,ans; while(scanf("%d",&nn),nn!=0){ memset(head,-1,sizeof(head)); si=0; for(int i=0;i<nn;i++) scanf("%lf%lf",&thing[i].first,&thing[i].second); scanf("%d",&mm); while(mm--){ scanf("%d",&kk); kk--; scanf("%d",&s); s--; for(int i=0;i<kk;i++){ scanf("%lf%d",&cc,&t); add_edge(s,--t,cc); s=t; } } toposort(); for(int i=0;i<=nn;i++) dp[i]=0.0; for(int i=0;i<nn;i++){ if(dp[topo[i]]==0.0) Dp(topo[i]); } ans=0; for(int i=0;i<nn;i++) ans+=thing[i].second*dp[i]; printf("%.2f\n",ans); } return 0; }
相关文章推荐
- hdu 3699 10 福州 现场 J - A hard Aoshu Problem 暴力 难度:0
- hdu 3694 10 福州 现场 E - Fermat Point in Quadrangle 费马点 计算几何 难度:1
- hdu 3695 10 福州 现场 F - Computer Virus on Planet Pandora 暴力 ac自动机 难度:1
- hdu 3697 10 福州 现场 H - Selecting courses 贪心 难度:0
- hdu 3682 10 杭州 现场 C To Be an Dream Architect 容斥 难度:0
- hdu 3689 杭州 10 现场 J - Infinite monkey theorem 概率dp kmp 难度:1
- hdu 3687 10 杭州 现场 H - National Day Parade 水题 难度:0
- hdu 3682 10 杭州 现场 C - To Be an Dream Architect 简单容斥 难度:1
- hdu 3685 10 杭州 现场 F - Rotational Painting 重心 计算几何 难度:1
- HDU 4125 2011福州现场赛E题 (KMP+找规律建树+手工栈模拟)
- HDU 4125 2011福州现场赛E题 KMP+笛卡尔树
- hdu 4771 13 杭州 现场 B - Stealing Harry Potter's Precious 暴力bfs 难度:0
- hdu 4770 13 杭州 现场 A - Lights Against Dudely 暴力 bfs 状态压缩DP 难度:1
- HDU 4126 Genghis Khan the Conqueror MST + 树形DP 2011年福州现场赛F题
- hdu 3697(贪心)2010福州现场赛
- hdu 4122(RMQ)2011福州现场赛B题
- 2011 ACM 福州赛区现场赛第一题 (A .Xiangqi)HDU 4121 Xiangqi by kuangbin
- hdu 3262 09 宁波 现场 C - Seat taking up is tough 暴力 难度:0
- hdu 3268 09 宁波 现场 I - Columbus’s bargain 读题 最短路 难度:1
- HDU 3691 Nubulsa Expo(全局最小割)(10福州B题)#by zh