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的更多相关文章

  1. HDU 3696 Farm Game(dp+拓扑排序)

    Farm Game Problem Description “Farm Game” is one of the most popular games in online community. In t ...

  2. 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 ...

  3. 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 ...

  4. hdu 1198 Farm Irrigation(深搜dfs || 并查集)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...

  5. HDU 1198 Farm Irrigation(状态压缩+DFS)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目: Farm Irrigation Time Limit: 2000/1000 MS (Ja ...

  6. hdu.1198.Farm Irrigation(dfs +放大建图)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. 【HDOJ】3696 Farm Game

    SPFA求最短路径.见图的时候注意逆向建图. /* 3696 */ #include <iostream> #include <queue> #include <vect ...

  8. HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. HDU 2412 Farm Irrigation

    题目: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...

随机推荐

  1. UILabel 的属性设置

    .设置字体样式(加粗) label.font = [UIFont boldSystemFontOfSize:30]; 6.设置字体类型 label.font = [UIFont fontWithNam ...

  2. Hadoop作业优化

    mapper数量 reduce数量 combiner 中间值压缩 自定义序列 调整shuffle,减少溢出写 关闭推测执行 任务JVM重用 慢启动reduce

  3. sync命令

    sync命令用于强制被改变的内容立刻写入磁盘,更新超块信息. 在Linux/Unix系统中,在文件或数据处理过程中一般先放到内存缓冲区中,等到适当的时候再写入磁盘,以提高系统的运行效率.sync命令则 ...

  4. dom入门

    当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). //显示,改变html内容 document.getElementById("p1" ...

  5. webstoem自动编译less文件

    去node的主页下载对应版本的nodejs然后安装下载地址:http://nodejs.org/   根据自己的系统选择合适的版本下载. 安装完成之后打开命令提示符(win+r 输入cmd 回车),分 ...

  6. 用js动态的改变img标签里面的src属性实现图片的循环切换

    JS:根据循环切换的条件可以用 document.getElementById('').src=''设置, 或者jquery方法: $('#id').attr('src','图片名称’): 具体: i ...

  7. Hive基础学习

    Hive 学习记录Hive介绍:Hive 是起源于Facebook,使得Hadoop进行SQL查询成为可能,进而使得非程序员也可以进进行对其使用:它是一种数据仓库工具,将结构化的数据文件 映射为一张数 ...

  8. c语言-何为编程?

    大牛,请绕过. 新手,如果你怕我误人子弟,那也请绕过. 以下纯属个人YY 何为编程?何为程序? 说简单也简单,说复杂也复杂. 我在自学的道路上也有两三年了,也探索了两三年(非连续性),却只停留在入门阶 ...

  9. MFC应用程序编写实例—完整版(原创)

    前段时间,将近花了一周至两周上班和上班后的闲余时间,做了一个用于调试和测试工作项目的应用软件,下面将实现软件的重要步骤及主要功能讲解一遍,方便日后查阅. 程序开始后,提示登录框,输入用户名,密码后,登 ...

  10. Windows使用小技巧

    一.windows7 系统切换到 administrator用户 先进入windows 7的安全模式.. (这你应该会吧?) 然后系统会自动调出用administrator 帐号登录.. 密码你必须要 ...