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. python3 随机数

    random库 random.random()返回n,则 0 <= n < 1的小数. random.uniform(a,b)    返回n ,则 a <= n <= b的浮点 ...

  2. 证明:一个整数a若不能被6整除,则a2+24必能被24整除。(整除理论,1.1.4)

    证明:一个整数a若不能被6整除,则a2+24必能被24整除. 证明: 因为,a不能被6整除 所以,a不可以同时被2和3整除 所以,a一定是一个奇数, 所以,令a=2k+1,k是整数: 又因为,a2+2 ...

  3. js控制日期选择框datetime-local和select的展开

    注: js控制元素展开不受元素css属性的限制,例如opacity,z-index等 1. 使用js控制日期选择框的展开 ios: document.querySelector(".targ ...

  4. 解决Eclipse无法添加Tomcat服务器的问题

    eclipse配置好以后,如果Tomcat服务器在文件系统的位置发生了变化,则需要重新配置Tomcat服务器,这时会遇到无法设置服务器的问题 即图中框起来的部分无法进行操作,这时需要 关闭Eclips ...

  5. 滚动条加粗和panel,gridControl结合用

    private void FrmLotterycs_Load(object sender, EventArgs e) { t = 0; UserJuanquanTB = O.get_UserJuanq ...

  6. 将dom4j格式化为标准的xml字符串

    StringWriter writer = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); fo ...

  7. 《Windows驱动开发技术详解》之Windows内核函数

    内核模式下字符串操作 ANSI_STRING和UNICODE_STRING分别定义如下:

  8. mvc 母版页、用户自定义控件

    母版页(Master) 1.母版页是与Controller无关的,母版页只是一个View文件,而没有任何Controller与之相对应. 2.其实在ASP.NET MVC中View的aspx与母版页并 ...

  9. linux tar包追加问题【转】

    只能已归档的文件才能追加文件. 如果tar.gz文件是如此生成:#tar -zcvf test.tar.gz  a.txt即tar.gz是压缩(-z)和归档(-c)文件,则无法给它追加文件:若果tar ...

  10. Nginx 和 IIS 实现动静分离【转载】

    前段时间,搞Nginx+IIS的负载均衡,想了解的朋友,可以看这篇文章:<nginx 和 IIS 实现负载均衡>,然后也就顺便研究了Nginx + IIS 实现动静分离.所以,一起总结出来 ...