HDU 3696 Farm Game
SPFA最长路,思路如下:
先对题目中给出的每条边建边,权值为转化率;增加一个终点S,每个节点到S建边,权值为该物品的单价。
假设X物品最终转化为了Y物品,那么转化之后得到的钱就是 W[x]*转化率1*转化率2*转化率3*转化率4*.....*P[Y]
现在我们关注 转化率1*转化率2*转化率3*转化率4*.....*P[Y] 这个式子,实际上就是求这个式子的最大值。
怎么求?事实上就是节点X到节点S的最长路。如果这样去求,那么需要N次SPFA;所以我们需要反向建边,从S出发开始求最长路。最终比较一下转换后的价格和之前的价格哪个大,就用哪个。.
描述的不够清楚的话,请看这篇博客:
http://blog.csdn.net/power721/article/details/6968014
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const double INF=2100000000.0;
struct Edge
{
int from,to;
double cost;
}e[+];
int tot;
vector<int>G[+];
double price[+],num[+];
int N,M,K;
int a[+];
double b[+];
int flag[+];
double dis[+]; void init()
{
for(int i=;i<+;i++) G[i].clear();
tot=;
} void spfa()
{
queue<int>Q;
for(int i=;i<=N;i++) dis[i]=-INF;
memset(flag,,sizeof flag);
dis[]=;
flag[]=;
Q.push();
while(!Q.empty())
{
int h=Q.front();Q.pop();flag[h]=;
for(int i=;i<G[h].size();i++)
{
int id=G[h][i];
if(dis[h]*e[id].cost>dis[e[id].to])
{
dis[e[id].to]=dis[h]*e[id].cost;
if(flag[e[id].to]==)
{
flag[e[id].to]=;
Q.push(e[id].to);
}
}
}
}
} int main()
{
while(~scanf("%d",&N))
{
if(N==) break;
init();
for(int i=;i<=N;i++)
scanf("%lf%lf",&price[i],&num[i]);
scanf("%d",&M);
for(int i=;i<=M;i++)
{
scanf("%d",&K);
scanf("%d",&a[]);
for(int j=;j<=K-;j++)
scanf("%lf%d",&b[j],&a[j]);
for(int j=;j<=K-;j++)
{
e[tot].from=a[j];
e[tot].to=a[j-];
e[tot].cost=b[j];
G[a[j]].push_back(tot);
tot++;
}
}
for(int i=;i<=N;i++)
{
e[tot].from=;
e[tot].to=i;
e[tot].cost=price[i];
G[].push_back(tot);
tot++;
}
spfa();
double ans=;
for(int i=;i<=N;i++)
ans=ans+num[i]*max(dis[i],price[i]);
printf("%.2lf\n",ans);
}
return ;
}
HDU 3696 Farm Game的更多相关文章
- HDU 3696 Farm Game(dp+拓扑排序)
Farm Game Problem Description “Farm Game” is one of the most popular games in online community. In t ...
- HDU 3696 Farm Game(拓扑+DP)(2010 Asia Fuzhou Regional Contest)
Description “Farm Game” is one of the most popular games in online community. In the community each ...
- hdu 3696 10 福州 现场 G - Farm Game DP+拓扑排序 or spfa+超级源 难度:0
Description “Farm Game” is one of the most popular games in online community. In the community each ...
- hdu 1198 Farm Irrigation(深搜dfs || 并查集)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...
- HDU 1198 Farm Irrigation(状态压缩+DFS)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目: Farm Irrigation Time Limit: 2000/1000 MS (Ja ...
- hdu.1198.Farm Irrigation(dfs +放大建图)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 【HDOJ】3696 Farm Game
SPFA求最短路径.见图的时候注意逆向建图. /* 3696 */ #include <iostream> #include <queue> #include <vect ...
- HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 2412 Farm Irrigation
题目: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...
随机推荐
- great C++ socket library
NETLINK: http://netlinksockets.sourceforge.net/index.html
- DataTable Javascript Link not working on 2nd page
$(document).ready(function () { var otable = $('#tbl-resources').dataTable( { bJQueryUI: false, bFil ...
- VS2013程序打包部署详细图解
目录(?)[+] 新建项目 FILE –> New –> Project,如下图所示: 注意:如果 InstallShield Limited Edition Project 显 ...
- toggle的使用心得
点击同一个标签可以实现不同效果 或者几个效果 以前一般都是if 判断的 逻辑还有判断比较繁琐 看啦手册后 发现这个功能可以不用判断的自动循环点击事件 比以前的简单好用 主要用法:$("td& ...
- RPC框架基本原理(一):服务注册
什么是RPC框架 RPC整个过程涉及四类对象:客户端.客户端代理.服务端和服务端代理.RPC要求客户端和服务端之间约定好调用接口和传输格式(如JSON,Xml等),客户端在调用该接口时,由客户端的代理 ...
- 编写一个闹钟和定时关机工具(MFC VS2010)
这个小工具在自己生活当中能用到,运行软件以后,会显示当前的系统时间,然后你可以设定时间,再选择是定时响铃还是关机.截图如下: 前言:本程序采用visual studio 2010 ,对话框类型的应用程 ...
- STM32F207 两路ADC连续转换及GPIO模拟I2C给MT9V024初始化参数
1.为了更好的方便调试,串口必须要有的,主要打印一些信息,当前时钟.转换后的电压值和I2C读出的数据. 2.通过GPIO 模拟I2C对镁光的MT9V024进行参数初始化.之前用我以前公司SP0A19芯 ...
- 《JS权威指南学习总结--4.13运算符》
一.typeof运算符 typeof是一元运算符,放在其单个操作数的前面,操作数可以是任意类型.返回值为表示操作数类型的一个字符串. 例如: typeof x ...
- HDU2539:点球大战
Problem Description 在足球比赛中,有不少赛事,例如世界杯淘汰赛和欧洲冠军联赛淘汰赛中,当比赛双方经过正规比赛和加时赛之后仍然不分胜负时,需要进行点球大战来决定谁能够获得最终的胜利. ...
- JSP内置对象--request对象 (setCharacterEncoding("GBK"),getParameter(),getParameterValues(),getParameterNames(),getServletPath(),getContextPath()
使用最多,主要用来接收客户端发送而来的请求信息,他是javax.servlet.http.HttpServletRequest接口的实例化对象. public interface HttpServle ...