HDOJ 3696 Farm Game 【spfa】

Farm Game

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

题意:有n种物品要卖,每种物品有自己的单价和现在有的重量,根据题意,可以将一种物品转换为另外一种物品,但是有转换百分比(例如1    0.8    2,意思将1单位的物品1转化为物品2之后为0.8单位的物品2)。求最后可以得到的总的钱数。

分析:根据题意这道题可以转换成一个有向无环图,因为可以间接转化就是1——》2,之后2——》3,这样子转化,我们需要找到一个最长的转换路径(有最大利润的),我们可以设置一个点(这个点就代表原来自身的价钱),用spfa求出每一个点到该点的最长路径深度,需要n-1次,我们反过来用这个点求取到每一个点的深度,这样只需要一次就好了。因为有叠成,我们用log就可以转化为加法。

代码:

#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];
double 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(low));
    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));  //因为是反过来求的所以方向也需要反过来
                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;
}
时间: 2024-10-27 12:04:28

HDOJ 3696 Farm Game 【spfa】的相关文章

HDU3339 In Action 【SPFA】+【01背包】

In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4099    Accepted Submission(s): 1306 Problem Description Since 1945, when the first nuclear bomb was exploded by the Manhattan Project

hdoj 1863 畅通工程 【最小生成树】+【kruskal】

题意:... 难点:如何判断是不是信息不全:在输入的时候建立并查集,之后判断有几个节点就可以了,剩下的就是kruskal算法. 代码: #include<stdio.h> #include<string.h> #include<algorithm> #define MAXN 105 #define INF 0x3f3f3f3f using std::sort; struct node{ int from; int to; int w; }edges[MAXN*MAXN]

hdoj 2391 Filthy Rich 【DP】

题目大意:有个二维数组,你从(0,0)出发,最终到(n,m), 在这个二维数组中,每个位置dp[i][j]都有一定量的黄金,你可以拾取,问你最多能失去多少,并且,你的方向有下,右, 斜向下三个方向: 策略:就是每一个都加上它的上方向与左方向的最大值,这样到最后就是最大值.详情见代码 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2391 代码: #include<stdio.h> #include<string.h> int dp[1

hdoj 1045 Fire Net 【DFS】

题意:如果两个点要放在同一行或者同一列,那么两个点中间要有一个墙,否则的话只能放一个点,最后问你最多能放几个点. 看了一个星期.. 这道题的解法我还是第一次见,就是逐个逐个的来放置每个点,然后每经过一个点都判断一次,详情看代码 代码: #include <stdio.h> #include <string.h> int ans, n; char map[10][10]; int judge(int lin, int row) { int i; for(i = lin-1; i &g

HDU1224 Free DIY Tour 【SPFA】

Free DIY Tour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3939    Accepted Submission(s): 1262 Problem Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulf

boj 454 帮帮小叮当【SPFA】

题目链接:http://code.bupt.edu.cn/problem/p/454/ 454. 帮帮小叮当 时间限制5000 ms 内存限制 65536 KB 题目描述 小叮当刚刚学会了传送门的使用方法,可是它不小心跌落到二维空间一个 n * m 的矩阵格子世界的入口(1,1)处, 他得知出口在(n,m)处,每穿越一个格子门,它的体力值会下降. 又饿又累的他 IQ 已经降为负数了,聪明的你,能帮他规划一下路线,使得它体力值下降的最少吗? 每一行有且仅有一个传送门,但是小叮当上课睡着了,只学会了

HDU1142 A Walk Through the Forest 【SPFA】+【记忆化搜索】

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5688    Accepted Submission(s): 2089 Problem Description Jimmy experiences a lot of stress at work these days, especial

POJ 2449 Remmarguts&#39; Date【SPFA】【A*】

Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 21978 Accepted: 5982 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he to

HDU1217 Arbitrage 【SPFA】

Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4460    Accepted Submission(s): 2032 Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform