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 ...
随机推荐
- sharepoint2010配置个人网站的offical方法 来自Jetluning的专栏
Configuring My Site in SharePoint 201 SharePoint My Sites are commonly referred to as “Facebook fo ...
- android 动态string
android开发过程之中,动态的插入string内容时候使用, 例如, <string name="time">当前时间:<xliff:g id="p ...
- Python 学习笔记6
做人要有目标感! 继续学习Python哦.
- elicpse之tomcat配置
环境:eclipse4.5.0,tomcat7.0.57 部署描述:按照一般的部署,把tomcat部署到webapps的下面,server options 下面选的是 Modules auto rel ...
- ClassLoader类加载解惑
1.通过类加载器获取路径: String path = Thread.currentThread().getContextClassLoader().getResource("." ...
- laravel安装 笔记
http://laod.cn/hosts/2015-google-hosts.html 谷歌FQIP laravel安装和设置流程 1安装composer , VirtualBox和Vagrant 下 ...
- json数据格式在IE浏览器中报错问题
如果一个json数据,最后一个数据的后面带有分号,则在IE浏览器中会报错 { "books": [ { "language":"Java" ...
- php curl调用相关api
一.基本步骤 1.本次模拟是php的相关post请求,可通过CURLOPT_CUSTOMREQUEST设定相关POST.GET.PUT.DELETE相关适应REST API 2.相关重要的是curl_ ...
- UpdateData使用说明
UpdateData() MFC的窗口函数,下面是MSDN的说明: Call this member function to initialize data in a dialog box, or t ...
- The List ADT
1.Definiation A list is a sequence. a0, a1,a2,..., aN (N>0) 2.Character For any list except the ...