Farm GameTime
limit:2000/1000 MS (java/others) Memory limit:62768/32768 K (java/others)
Total submission (s): 670 Accepted Submission (s): 258
Problem Description "Farm Game" is one of the most popular games in online community. 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. 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 is sold in a better price than the original crops.
When the farmer gets richer, manufacturing industry can is set up by starting up some machines. For example, Cheese-machine can-transfer milk to Cheese-get better profits and Textile machine can spin cony hair to Ma Ke sweaters. At this time, a production chain appeared in the farm.
Selling the products can get profits. Different products may be Different price. After gained some products, the farmer can decide whether to sell them or use them as animal food or machine material to G ET 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 had the extremely high level so, he could feed various animals and build several manufacturing lines to Conve RT some products.
In short, some kinds of the products can is transformed into other kinds of the products. For example, 1 pound of milk can is transformed into 0.5 pound of cheese, and 1 pound of crops can transformed to 0.1 Pound of eggs, etc. Every kind of product have a price. Now Jack is the amount of every kind of product he have, and the transform relationship among all kinds of products, Please help Jack to figure out how the much money he can do at the most if he sell out all of his products.
Please note this there is a transforming rule:if product A can being transformed into product B directly or indirectly and then Product B can never is transformed into product A, no matter directly or indirectly.
Inputthe input contains several test cases. The first line of all test case contains a integers n (n<=10000) representing that there is n kinds of products in J Ack ' s farm. The product categories is numbered for 1 to N. In the following N lines, the ith line contains both real numbers p and W, meaning that the price for the ith kind of produ CT is p per pound and Jack have w pounds of the ith kind of product.
Then there was a line containing an integer M (m<=25000) meaning that the following M lines describes the transform RelA Tionship among all kinds of the products. Each one of those M lines are in the format below:
K A0, B1, A1, B2, A2, ..., Bk-1, ak-1
K is a integer, and 2xk-1 numbers follows K. Ai is an integer representing product category number. Bi is a real number meaning this 1 pound of product ai-1 can be transformed into bi pound of product AI.
The total sum of K in all M lines are less than 50000.
The input file is ended by a, containing an integer 0.
Outputfor each test case, print a line with a real number representing the maximum amount of money that Jack can get. The answer should is rounded to 2 digits after decimal point. We guarantee that the answer are less than 10^10.
Sample Input
22.5 105 012 1 0.5 222.5 105 012 1 0.8 20
Sample Output
25.0040.00
Test instructions: There are n kinds of goods to sell, each item has its own unit price and present weight, according to test instructions, can convert one item to another item, but there is a conversion percentage (for example, 1 0.8 2, meaning 1 units of item 1 is converted to 2 units after item 0.8). Ask for the total amount of money you can get at the end.
Analysis: According to test instructions this problem can be converted into a non-circular map, because can be indirect conversion is the "2, then 2--" 3, this way, we need to find a longest conversion path (with maximum profit), we can set a point (this point represents the original price), Using SPFA to find the longest path depth of each point to that point, it takes n-1 times, and we use this point to find the depth of each point, so it takes only one time. Because there are folds, we can use log to convert to addition.
Code:
#include <cstdio> #include <cstring> #include <cmath> #include <queue>const int M = 1e4+5;using namespace Std;struct node{int to, next; Double V;} E[m*4];int head[m*4];d ouble p[m], w[m], Low[m];int N, tot;bool vis[m];//int que[m];void Add (int a, int b, double c) {e[ Tot].to = b; E[tot].next = Head[a]; E[TOT].V = C; Head[a] = tot++;} void Spfa (int u) {queue<int >q; memset (Vis, 0, sizeof (VIS)); memset (Low, -0x3f, sizeof); Q.push (U); Vis[u] = 1; Low[u] = 0; while (!q.empty ()) {int temp = Q.front (); Q.pop (); for (int i = head[temp]; ~i; i = e[i].next) {int v = e[i].to; if (Low[v] < LOW[TEMP]+E[I].V) {low[v] = LOW[TEMP]+E[I].V; if (!vis[v]) {vis[v] = 1; Q.push (v); }}}}}int main () {while (scanf ("%d", &n), n) {int i; for (i = 1; I <= n; + + i) scanf ("%lf%lF ", P+i, W+i); int A, B, M, K; Double temp; tot = 0; scanf ("%d", &m); Memset (Head,-1, sizeof (head)); while (M-) {scanf ("%d%d", &k, &a); for (i = 1; i < K; + + i) {scanf ("%lf%d", &temp, &b); Add (b, a, log (temp)); Because it is the reverse, the direction also needs to be reversed A = B; }} for (i = 1; I <= n; + + i) {Add (0, I, log (p[i])); } SPFA (0); Double ans = 0; for (i = 1; I <= n; + + i) {if (P[i] < exp (low[i)) P[i] = exp (low[i]); Ans + = p[i]*w[i]; } printf ("%.2lf\n", ans); } return 0;}
Hdoj 3696 Farm Game "SPFA"