HDOJ 3696 Farm Game 【spfa】

2023-11-29 23:08
文章标签 spfa game hdoj farm 3696

本文主要是介绍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 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种物品要卖,每种物品有自己的单价和现在有的重量,根据题意,可以将一种物品转换为另外一种物品,但是有转换百分比(例如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;
}


这篇关于HDOJ 3696 Farm Game 【spfa】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/434683

相关文章

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3159 (spfa差分约束最短路) poj 1201

poj 3159: 题意: 每次给出b比a多不多于c个糖果,求n最多比1多多少个糖果。 解析: 差分约束。 这个博客讲差分约束讲的比较好: http://www.cnblogs.com/void/archive/2011/08/26/2153928.html 套个spfa。 代码: #include <iostream>#include <cstdio>#i

poj 3169 spfa 差分约束

题意: 给n只牛,这些牛有些关系。 ml个关系:fr 与 to 牛间的距离要小于等于 cost。 md个关系:fr 与 to 牛间的距离要大于等于 cost。 隐含关系: d[ i ] <= d[ i + 1 ] 解析: 用以上关系建图,求1-n间最短路即可。 新学了一种建图的方法。。。。。。 代码: #include <iostream>#include

poj 3255 次短路(第k短路) A* + spfa 或 dijkstra

题意: 给一张无向图,求从1到n的次短路。 解析: A* + spfa 或者 dijkstra。 详解见上一题:http://blog.csdn.net/u013508213/article/details/46400189 本题,spfa中,stack超时,queue的效率最高,priority_queue次之。 代码: #include <iostream>#i

poj 2449 第k短路 A* + spfa

poj 2449: 题意: 给一张有向图,求第k短路。 解析: A* + spfa。 一下转自:http://blog.csdn.net/mbxc816/article/details/7197228 “描述一下怎样用启发式搜索来解决K短路。 首先我们知道A*的基础公式:f(x)=g(x)+h(x);对h(x)进行设计,根据定义h(x)为当前的x点到目标点t所需要的实际距

fzu 2275 Game KMP

Problem 2275 Game Time Limit: 1000 mSec    Memory Limit : 262144 KB  Problem Description Alice and Bob is playing a game. Each of them has a number. Alice’s number is A, and Bob’s number i

10400 -Game Show Math

这道题的话利用了暴力深搜,尽管给了20S,但是这样还会超时,所以就需要利用回溯进行减枝,因为是DFS,所以用一个数组vis[i][j]记录是否在状态i时候取到过j值,如果取到过的话,那么直接回溯(往后搜索已经没有意义了,之前到达这个状态的时候是无法得到结果的) 还有需要注意的地方就是题目的要求,每一步的结构都在(-32000,32000)之间,所以需要一步判断,如果在这个范围外直接回溯 最后一

【AcWing】852. spfa判断负环

#include<iostream>#include<algorithm>#include<cstring>#include<queue>using namespace std;const int N= 1e5+10;int n,m;int h[N],w[N],e[N],ne[N],idx;int dist[N],cnt[N];//cnt存最短路径的边数bool st[N];v

透析SPFA算法(图例讲解)

SPFA算法是Bellman-Ford的队列优化,所以先介绍Bellman-Ford算法。        Dijkstra算法是处理单源最短路径的有效算法,但它局限于边的权值非负的情况,若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的。这时候,就需要使用其他的算法来求解最短路径,Bellman-

spfa+多重约束

普通的spfa只是用来求单源最短路(也就是边权和最小),是通过不断松弛边权来求的。 但是在一些情况需要求点权和最大或最小的情况(或者是其他的约束条件) 我们只需要根据条件加几个约束条件就行 以下是例题: L2-001. 紧急救援 时间限制:200 ms 内存限制:65536 kB 代码长度限制:8000 B 作为一个城市的应急救援队伍的负责人,