hdu 3696 10 福州 现场 G - Farm Game DP+拓扑排序 or spfa+超级源 难度:0
Description
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
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
Sample Input
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
40.00
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring> using namespace std; const int maxn=15000;
const int maxe=102000; typedef pair<double,double> PP; struct edge
{
double cc;
int to,next;
} P[maxe]; int head[maxn],si;
PP thing[maxn];
int nn;
int c[maxn],topo[maxn],t; //about topo
double dp[maxn]; void add_edge(int s,int t,double cc)
{
P[si].to=t;
P[si].cc=cc;
P[si].next=head[s];
head[s]=si++;
} bool dfs(int u)
{
c[u]=-1;
for(int i=head[u];i!=-1;i=P[i].next){
int v=P[i].to;
if(c[v]<0) return false;
else if(!c[v]&&!dfs(v)) return false;
}
c[u]=1; topo[--t]=u;
return true;
}
bool toposort()
{
t=nn;
memset(c,0,sizeof(c));
for(int i=0;i<nn;i++){
if(!c[i]) if(!dfs(i)) return false;
}
return true;
}
void Dp(int u)
{
if(dp[u]!=0.0) return ;
dp[u]=thing[u].first;
for(int i=head[u];i!=-1;i=P[i].next){
int v=P[i].to;
Dp(v);
dp[u]=max(dp[u],dp[v]*P[i].cc);
}
} int main()
{
int mm,kk,s,t;
double cc,ans;
while(scanf("%d",&nn),nn!=0){
memset(head,-1,sizeof(head));
si=0;
for(int i=0;i<nn;i++) scanf("%lf%lf",&thing[i].first,&thing[i].second);
scanf("%d",&mm);
while(mm--){
scanf("%d",&kk);
kk--;
scanf("%d",&s);
s--;
for(int i=0;i<kk;i++){
scanf("%lf%d",&cc,&t);
add_edge(s,--t,cc);
s=t;
}
}
toposort();
for(int i=0;i<=nn;i++) dp[i]=0.0;
for(int i=0;i<nn;i++){
if(dp[topo[i]]==0.0) Dp(topo[i]);
}
ans=0;
for(int i=0;i<nn;i++) ans+=thing[i].second*dp[i];
printf("%.2f\n",ans);
}
return 0;
}
hdu 3696 10 福州 现场 G - Farm Game DP+拓扑排序 or spfa+超级源 难度:0的更多相关文章
- hdu 3695 10 福州 现场 F - Computer Virus on Planet Pandora 暴力 ac自动机 难度:1
F - Computer Virus on Planet Pandora Time Limit:2000MS Memory Limit:128000KB 64bit IO Format ...
- hdu 3697 10 福州 现场 H - Selecting courses 贪心 难度:0
Description A new Semester is coming and students are troubling for selecting courses. Students ...
- hdu 3699 10 福州 现场 J - A hard Aoshu Problem 暴力 难度:0
Description Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. N ...
- hdu 3694 10 福州 现场 E - Fermat Point in Quadrangle 费马点 计算几何 难度:1
In geometry the Fermat point of a triangle, also called Torricelli point, is a point such that the t ...
- Educational DP Contest G - Longest Path (dp,拓扑排序)
题意:给你一张DAG,求图中的最长路径. 题解:用拓扑排序一个点一个点的拿掉,然后dp记录步数即可. 代码: int n,m; int a,b; vector<int> v[N]; int ...
- HDU 6170 FFF at Valentine(强联通缩点+拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6165 题意:给你一个无环,无重边的有向图,问你任意两点,是否存在路径使得其中一点能到达另一点 解析:强 ...
- P3008 [USACO11JAN]Roads and Planes G (最短路+拓扑排序)
该最短路可不同于平时简单的最短路模板. 这道题一看就知道用SPFA,但是众所周知,USACO要卡spfa,所以要用更快的算法. 单向边不构成环,双向边都是非负的,所以可以将图分成若干个连通块(内部只有 ...
- HDU 3696 Farm Game(dp+拓扑排序)
Farm Game Problem Description “Farm Game” is one of the most popular games in online community. In t ...
- hdu 3685 10 杭州 现场 F - Rotational Painting 重心 计算几何 难度:1
F - Rotational Painting Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
随机推荐
- 用virtualenv建立多个Python独立开发环境(转)
add by zhj: 在virtualenv环境下,安装第三方包时,不要用sudo pip install xxx,要直接用pip install xxx,如果用sudo时,那会安装在原来的系统Py ...
- go-004-数据结构
在 Go 编程语言中,数据类型用于声明函数和变量. 数据类型的出现是为了把数据分成所需内存大小不同的数据,编程的时候需要用大数据的时候才需要申请大内存,就可以充分利用内存. Go 语言按类别有以下几种 ...
- maven解决“Could not calculate build plan”问题
错误提示如下:(eclipse+maven) Could not calculate build plan: Failure to transfer org.apache.maven.plugins: ...
- html编码常见的有utf-8和gb2312编码等,应该如何判断选择?
html如何选择编码,常见utf-8和gb2312编码如何判断选择 一.首先了解目前国内中文网页常用编码是utf-8 还是gb2313. 比如: 百度搜索 网页使用utf-8 腾讯新闻 网页使用utf ...
- MyBtis—原理及初始化
Mybatis的功能架构分为三层: 1) API接口层:提供给外部使用的接口API,开发人员通过这些本地API来操纵数据库.接口层 一接收到调用请求就会调用数据处理层来完成具体的数据处理. ...
- Java backup
待补充 ........ 0:常用头文件(待补充) import java.util.Arrays; import java.util.HashSet; import java.util.TreeSe ...
- python webdriver 测试框架-数据驱动excel驱动的方式
简介: 数据驱动excel驱动方式,就是数据配置在excel里面,主程序调用的时候每次用从excel里取出的数据作为参数,进行操作, 需要掌握的地方是对excel的操作,要灵活的找到目标数据 测试数据 ...
- python使用cx_Oracle连接oracle
1.使用pip命令安装cx_Oracle $ pip install cx_Oracle 2.安装oracle客户端,并添加到path 下载路径: http://www.oracle.com/tech ...
- String StringBuilder StringBuffer 对比 总结得非常好
转自:http://www.iteye.com/topic/522167 作者:每次上网冲杯Java时,都能看到关于String无休无止的争论.还是觉得有必要让这个讨厌又很可爱的String美眉,赤裸 ...
- java bitmap/bitvector的分析和应用
转自: http://shmilyaw-hotmail-com.iteye.com/blog/1741608 简介 bitmap在很多海量数据处理的情况下会用到.一些典型的情况包括数据过滤, ...