【思维-最长路】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 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

题意:第一行输入n,表示有n种产品,接下来输入n行的p、w,分别表示第 i 种产品的单价和数量;然后输入m,表示有m种转换关系,接下来输入m行,每行第一个是k,表示后面有2k-1个元素,后面的元素是这种形式的:a0 b1 a1 b2 a2.......... 表示ai-1可以换成bi倍数量的ai商品!最后要求输出通过转换关系转化后,最终可以得到的最大价值。
思路:
考虑每个物品转化后是这个物品的单价会改变,求每个点单价改变的最长路。比如:
                             质量:                                        1 -->(0.8)--> 0.8
                             单价:                                        5                    8
质量的转化可视作此物品的单价发生改变:          6.4<--(0.8)<--  8
设一个终点,将各个点作为源点求到终点的最长路,即找到每个源点所能转化到的最大单价。
反向建图,将此终点作为源点,求到每一点的最长路即可。
其中注意一个小技巧,改变后的单价=单价*转化率。将乘法变为加法,取个log

代码:
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;

int n,m;

const int maxn=100000;
const int inf =0x3f3f3f3f;
struct Edge
{
    int v;
    double cost;
    Edge(int _v=0,double _cost=0):v(_v),cost(_cost){}
};
vector<Edge>E[maxn];
void addedge(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}
bool vis[maxn];
double dist[maxn];

bool spfa(int start)
{
    memset(vis,false,sizeof(vis));
    for(int i=0;i<=n;i++) dist[i]=-inf;
    vis[start]=true;
    dist[start]=0;
    queue<int>que;
    while(!que.empty()) que.pop();
    que.push(start);

    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        vis[u]=false;
        for(int i=0;i<E[u].size();i++)
        {
            int v=E[u][i].v;
            if(dist[v]<dist[u]+E[u][i].cost)
            {
                dist[v]=dist[u]+E[u][i].cost;
                if(!vis[v])
                {
                    vis[v]=true;
                    que.push(v);
                }
            }
        }
    }
    return true;
}

double num[maxn];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0) break;

        memset(num,0,sizeof(num));
        for(int i=0;i<=n;i++)
            E[i].clear();
        for(int i=1;i<=n;i++)
        {
            double p;
            scanf("%lf%lf",&p,&num[i]);
            E[0].push_back(Edge(i,log(p)));
        }

        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            int k,a0;
            scanf("%d",&k);
            scanf("%d",&a0);
            for(int j=1;j<k;j++)
            {
                double b;int a1;
                scanf("%lf%d",&b,&a1);
                E[a1].push_back(Edge(a0,log(b)));
                a0=a1;
            }
        }

//        printf("/****/\n");
//        for(int i=0;i<n;i++)
//        {
//            for(int j=0;j<(int)E[i].size();j++)
//            {
//                printf("%d %d %.30lf\n",i,E[i][j].v,E[i][j].cost);
//            }
//        }
//        printf("/****/\n");

        spfa(0);
        double ans=0;
        for(int i=1;i<=n;i++)
        ans+=(exp(dist[i]))*num[i];

//        printf("%.2lf\n",dist[i]);
        printf("%.2f\n",ans);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值