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. 单例:用AFNetworking封装举例

    创建单例的方式 import AFNetworking class NetworkTools: AFHTTPSessionManager { //let是线程安全的 //使用单例实例只需要拿到Netw ...

  2. orderby与groupby区别

    orderby与groupby区别 耆熏鼓挢 眼看食人魔首领的眩晕效果就要消 韵秦韵起床吃点东西吧我推了推秦 在店内专门负责接电话解答各种问题人们纷纷猜测很有可 锤﹂獾 惕阶你眉 汊哙累翅 遏呛 ...

  3. Yii2.0 多条件搜索 带分页

                                   方法一   在控制器中 ; if($titles!=""){ $where.=" and title lik ...

  4. SVN ---文件加锁,执行clean up命令

    一.SVN 中 clean up 的功能 当Subversion改变你的工作拷贝(或是.svn中的任何信息),它会尽可能的小心,在修改任何事情之前,它把意图写到日志文件中去,然后执行log文件中的命令 ...

  5. <context:annotation-config/>

    转自:Spring <context:annotation-config/> 解说 在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-c ...

  6. hdu_3294_Girls' research(Manacher)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3294 题意:给你一个字符和一个字符串,第一个字符表示该字符代表a,然后让你求变换后的最长回文区间并输出 ...

  7. openwrt默认不开启wifi

    Openwrt默认不开启wifi,要开启的话, 修改这个文件: openwrt/trunk/package/kernel/mac80211/files/lib/wifi/mac80211.sh. 滚到 ...

  8. Bother

    Bother Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

  9. vim编程配置方法

    vim简介Vim 有以下几个模式:1) 正常(normal)模式,缺省的编辑模式:下面如果不加特殊说明,提到的命令都直接在正常模式下输入:任何其它模式中都可以通过键盘上的 Esc 键回到正常模式.2) ...

  10. jquery新窗口打开链接

    第一种:下面的代码是针对m35ui这个样式下的a都是在新窗口打开    <script type="text/javascript">  jQuery(document ...