HDU 3696 SPFA算法

来源:互联网 发布:网络信息安全培训班 编辑:程序博客网 时间:2024/06/11 17:54

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. 

题意:

 N种物品,每个物品初始时有价格数量两个属性,有的物品可以以一定比例交换,问从初始状态能够达得到的最大收益。

解题思路:

设置一个起始节点0,连接每个节点,然后SPFA找最长路(其实是反着找的),dis[i]表示第i个物品所能转换成的最大单价。最后把dis[i]*w[i]累加。

代码:

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;const int N = 10005;const int M = 25005;typedef __int64 ll;int head[N];struct node{    double val;    int to,next;}g[M<<3];int n,m,num;int que[N];bool vis[N];double dis[N];double ans;double p[N],w[N];void build(int s,int e,double v){    g[num].to = e;    g[num].val = v;    g[num].next = head[s];    head[s] = num ++;}void SPFA(){    int i,front,rear;    for(i = 0;i <= n;i ++)    {        dis[i] = 0;        vis[i] = false;    }    front = rear = 0;    que[rear ++] = 0;    vis[0] = true;    while(front != rear)                        //循环队列    {        int u = que[front ++];        if(front == N)            front = 0;        vis[u] = false;        for(i = head[u];~i;i = g[i].next)            if(dis[g[i].to] < dis[u] + g[i].val)            {                dis[g[i].to] = dis[u] + g[i].val;                if(!vis[g[i].to])                {                    vis[g[i].to] = true;                    que[rear ++] = g[i].to;                    if(rear == N)                        rear = 0;                }            }    }}void solve(){    SPFA();    for(int i = 1;i <= n;i ++)    {        if(p[i] < pow(10.0,dis[i]))            p[i] = pow(10.0,dis[i]);        ans += p[i] * w[i];    }    printf("%.2lf\n",ans);}int main(){    int i,k,a,b;    while(scanf("%d",&n),n)    {        ans = 0;        memset(head,-1,sizeof(head));        num = 0;        for(i = 1;i <= n;i ++)        {            scanf("%lf%lf",&p[i],&w[i]);            build(0,i,log10(p[i]));           //设定原始起点        }        scanf("%d",&m);        while(m --)        {            scanf("%d",&k);            scanf("%d",&a);            k --;            double pp;            while(k --)            {                scanf("%lf%d",&pp,&b);                build(b,a,log10(pp));         //从终点找到起点                a = b;            }        }        solve();    }    return 0;}


0 0