码迷,mamicode.com
首页 > 其他好文 > 详细

hdu3696 Farm Game 拓扑dp

时间:2014-11-05 00:31:13      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:hdu

http://acm.hdu.edu.cn/showproblem.php?pid=3696

Farm Game

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


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
 

Source

题意:告诉每种货物的价格和数量,然后一些货物之间可以转换,问最大获利。题目描述好拙计的说。

分析:首先题目保证是一个无环图,如果x可以转换成y那么x向y连边,这是个有向无环图。注意入度为0的点不能被其它点转换来,所以其产生的利润是固定的,然后其它点依次dp转移下看。。。就是拓扑排序下。。

/**
 * @author neko01
 */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair<int,int> pp;
const double eps=1e-8;
const double pi=acos(-1.0);
const int INF=0x7fffffff;
const LL inf=(((LL)1)<<61)+5;
const int N=10005;
struct node{
    double c;
    int to;
};
vector<node>g[N];
double p[N];
double w[N];
int in[N];
int main()
{
    int n,m,k;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=1;i<=n;i++)
        {
            g[i].clear();
            in[i]=0;
            scanf("%lf%lf",&p[i],&w[i]);
        }
        scanf("%d",&m);
        while(m--)
        {
            int a,a1;
            double b;
            scanf("%d%d",&k,&a);
            k--;
            while(k--)
            {
                scanf("%lf%d",&b,&a1);
                node cur;
                cur.to=a,cur.c=b;
                g[a1].pb(cur);
                in[a]++;
                a=a1;
            }
        }
        queue<int>q;
        for(int i=1;i<=n;i++)
            if(in[i]==0) q.push(i);
        double ans=0;
        while(!q.empty())
        {
            int cur=q.front();
            q.pop();
            ans+=p[cur]*w[cur];
            for(int i=0;i<g[cur].size();i++)
            {
                node next=g[cur][i];
                p[next.to]=max(p[next.to],next.c*p[cur]);
                if(--in[next.to]==0) q.push(next.to);
            }
        }
        printf("%.2lf\n",ans);
    }
    return 0;
}




hdu3696 Farm Game 拓扑dp

标签:hdu

原文地址:http://blog.csdn.net/neko01/article/details/40795473

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!