hdu3696 Farm Game 拓扑dp

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

2010 Asia Fuzhou Regional Contest

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

分析:首先题目保证是一个无环图,如果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;
}
时间: 2024-08-25 09:54:21

hdu3696 Farm Game 拓扑dp的相关文章

UVA 11324.The Largest Clique tarjan缩点+拓扑dp

题目链接:https://vjudge.net/problem/UVA-11324 题意:求一个有向图中结点数最大的结点集,使得该结点集中任意两个结点u和v满足:要目u可以到达v,要么v可以到达u(相互可达也可以). 思路:同一个强联通分量中满足结点集中任意两个结点u和v满足:要目u可以到达v,要么v可以到达u(相互可达也可以).把强联通分量收缩点后得到scc图,让每个scc结点的权值等于他的结点数,则求scc图上权最大的路径.拓扑dp,也可以直接bfs,但是要建立一个新的起点,连接所有入度为0

【BZOJ1093】【ZJOI2007】最大半联通子图 [拓扑][DP][Tarjan]

最大半连通子图 Time Limit: 30 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description 一个有向图G=(V,E)称为半连通的(Semi-Connected): 如果满足:∀u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向路径. 若G'=(V',E')满足V'∈V,E'是E中所有跟V'有关的边,则称G'是G的一个导出子图. 若G'是G的导出子图,且G'半连通,则称

【BZOJ1179】【Apio2009】Atm 强连通分量缩点+拓扑DP/拓扑最长路 kosaraju+tarjan+dfs转非递归三种代码

题解: 首先第一个阶段, 可以写kosaraju.也可以写tarjan. 这两种还都分递归和dfs转非递归. ----------------------------------四种方案. 第二个阶段,可以写拓扑DP 也可以写最长路 ----------------------------------乘上之前的,,八种方案. 本文写了kosaraju递归版,tarjan递归版,kosaraju非递归版. --只怪学校oj系统栈太小..都是逼得啊. 代码1(tarjan): #include <c

POJ 3249 拓扑+dp

Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9479   Accepted: 2168 Description Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, It's hard to have a job

CSU 1802 小X的战斗力【拓扑dp】

题目链接 题意:n个人,每个人有一个能力值.给出m组关系A, B, 表示A的能力值大于B的能力值. 问:m组关系中是否有自相矛盾的?若不矛盾,问:第1个人在所有人的能力值中排名第几?有多少人的能力值的排名可以确定? 题解:拓扑排序.存两个图,原图与反图. 若原图可达该点数+反图可达该点数-1 = n,则排名确定. bitset一下. 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define X first 4 #define Y s

洛谷 P2905 [USACO08OPEN]农场危机Crisis on the Farm(恶心的DP)

P2905 [USACO08OPEN]农场危机Crisis on the Farm 1605: [Usaco2008 Open]Crisis on the Farm 牧场危机 题目描述 约翰和他的奶牛组建了一只乐队“后街奶牛”,现在他们正在牧场里排练.奶牛们分成一堆 一堆,共1000)堆.每一堆里,30只奶牛一只踩在另一只的背上,叠成一座牛塔.牧场 里还有M(1 < M < 1000)个高高的草垛. 作为出色的指挥家,约翰可以通过口哨指挥奶牛们移动.他的口哨有四个音,分别能使所有 的牛塔向东南

BZOJ 1512 [POI2006]Pro-Professor Szu Tarjan缩点+拓扑DP

题意: n个别墅以及一个主建筑楼,从每个别墅都有很多种不同方式走到主建筑楼,其中不同的定义是(每条边可以走多次,如果走边的顺序有一条不同即称两方式不同). 询问最多的不同方式是多少,以及有多少个别墅有这么多方式,按照顺序输出别墅编号. 如果最多不同方式超过了36500那么都视作zawsze 解析: 容易想到把边反向,问题转化成求从主建筑楼走向各个点的方案数. 对于一个强连通分量,显然我们可以看做是一个点,所以首先把图缩点. 缩点之后 我们设f[i]表示走到第i个点的方案数. 显然f[i]=∑f[

【POJ3272】Cow Traffic 拓扑DP

题意:给一个有向图,边严格由编号小的点到编号大的点,现在计数所有的极大路径,求每条边遍历次数的最大值. 题解:正着扫一遍,倒着扫一遍,dp值相乘. #include <cstdio> #include <cstring> #include <algorithm> #define N 5050 #define M 50500 using namespace std; struct KSD { int v,next; long long cnt; }e[M],E[M]; i

[NOIP2009][LuoguP1073] 最优贸易 - Tarjan,拓扑+DP

Description&Data 题面:https://www.luogu.org/problemnew/show/P1073 Solution Tarjan对联通块缩点,在DAG上按照拓扑序更新最低买入价,到每个点时再更新一下答案,即联通块内最大卖出价减去沿途的最低价格,复杂度O(n). 看机房其他人有写双向SPFA,代码短一些,反向建一张图,一遍跑最大一遍跑最小,最后枚举一下最优答案即可 Tarjan代码如下:(SPFA在后面) #include<iostream> #includ