FJNUOJ the greed of Yehan(最长路 + 权值乘积转化)题解
Description
During the trip, Yehan and Linlin pass a cave, and there is a board at the door, which says if you have enough ambition, you will get lots of money from me. At the beginning of the cave, you will get one dollar, and then, if you go from A to B (A and B are two vertexs of an edge, and C is the length of the edge), your money will be C times larger, for example, if you have x dollars, your dollars will be C*x dollars. And the next second, a directed map appears on the entrance of the cave. After that, Yehan thinks it is a good chance to make a big money, therefore, Yehan wants to calculate how much money she can get at most. Could you help Yehan calculate how much money she can get at most? Because of the result is so large, you should mod 1000000007.You should walk from 1 to n.
Input
The first line of input is 2 integers n, m (the number of vertex, the number of edges)The following m lines,(1<=n<=1e4,1<=m<=1e5)the i-th line contains 3 integers u, v, w (1<=w<=1e9)(two vertexs and the length of the edge), we guarantee that there is no cycle.
Output
The maximum of dollars Yehan can get after mod 1000000007
Sample Input 1
3 3
1 2 100000
2 3 10001
1 3 100000
3 3
1 2 1
2 3 1
1 3 2
Sample Output 1
99993
2
我觉得这个OJ可能又要没了
题意:从1走到n,其中有m条单向路径,走过某条路,当前拥有的价值乘上路径权值,初始价值1,问你走到n时你最多有多少价值。
思路:最长路可以spfa取负值来做,问题在于怎么把乘法转化为加法,这里用取对数log来解决,这样就能用加法算出乘的最多的是多少了。然后我们再用一个东西保存没log前的数据来计算答案。一开始用了回溯来计算最终乘积,debug不出来了...改了直接在算最长路时就计算乘积的结果。70+%的AC率被我拖到了50%emmm
代码:
#include<cstdio>
#include<set>
#include<vector>
#include<cmath>
#include<queue>
#include<cstring>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = +;
const double INF = 0x3f3f3f3f;
const ll MOD = ;
struct Edge{
int v;
int w;
double logw;
Edge(int _v = , int _w = , double _logw = ): v(_v), w(_w), logw(_logw){}
};
vector<Edge> G[maxn];
bool vis[maxn];
double dis[maxn];
ll dist[maxn];
void spfa(int start,int n){
memset(vis,false,sizeof(vis));
for(int i = ;i <= n;i++) dis[i] = INF;
vis[start] = true;
dis[start] = ;
dist[start] = ;
queue<int> q;
while(!q.empty()) q.pop();
q.push(start);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = ;i < G[u].size();i++){
int v = G[u][i].v;
double w = G[u][i].logw;
if(dis[v] > dis[u] + w){ //u -> v
dis[v] = dis[u] + w;
dist[v] = (dist[u] * G[u][i].w) % MOD;
if(!vis[v]){
q.push(v);
vis[v] = true;
}
}
}
}
}
void addEdge(int u,int v,int w){
double logw = -log(w);
G[u].push_back(Edge(v, w, logw));
}
int main(){
int n, m;
while(scanf("%d%d", &n, &m) != EOF){
for(int i = ;i <= n;i++) G[i].clear();
int s,e;
int t;
while(m--){
scanf("%d%d%d", &s, &e, &t);
addEdge(s,e,t);
}
spfa(, n);
printf("%lld\n", dist[n]);
}
return ;
}
/*
3 3
1 2 100000
2 3 10001
1 3 100000
3 3
1 2 1
2 3 1
1 3 2
*/
FJNUOJ the greed of Yehan(最长路 + 权值乘积转化)题解的更多相关文章
- spfa求最长路
http://poj.org/problem?id=1932 spfa求最长路,判断dist[n] > 0,需要注意的是有正环存在,如果有环存在,那么就要判断这个环上的某一点是否能够到达n点,如 ...
- zoj 3795 Grouping tarjan缩点 + DGA上的最长路
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Status Practic ...
- 寒冰王座(DGA最长路/完全背包)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- 【HDU3721】枚举+最长路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3721 题意:给你一颗n个节点n-1条边的树,每条边都有一个权值,现在让你任意移动一条边然后把这条边连接 ...
- [USACO2003][poj2138]Travel Games(dp/最长路)
http://poj.org/problem?id=2138 题意:给你一些单词和初始单词,在初始单词的任意位置你可以加任意一个字母,使得这个新单词在给的单词中有所出现,然后在这样不断迭代下去,让你求 ...
- hiho #1050 : 树中的最长路 树的直径
#1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中, ...
- NYOJ16 矩形嵌套(DAG最长路)
矩形嵌套 紫书P262 这是有向无环图DAG(Directed Acyclic Graph)上的动态规划,是DAG最长路问题 [题目链接]NYOJ16-矩形嵌套 [题目类型]DAG上的dp & ...
- 中南大学oj 1317 Find the max Link 边权可以为负的树上最长路 树形dp 不能两遍dfs
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1317经典问题:树上最长路,边权可以为负值的,树形dp,不能用两边dfs.反例:5 41 2 22 ...
- zoj 3088 Easter Holidays(最长路+最短路+打印路径)
Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are prov ...
随机推荐
- MySQL(4):主从复制原理
1.主从复制概述 MySQL主从复制也可以称为MySQL主从同步,它是构建数据库高可用集群架构的基础.它通过将一台主机的数据复制到其他一台或多台主机上,并重新应用relay log中的SQL语句来实现 ...
- Web Responsive Table, 只需CSS使table在手机和平板中完美显示
在做responsive或者手机版页面的时候,经常碰到<Table>在手机和平板中会因为长度问题把页面撑大.最近看到一个比较好,比较方便的方法,而且仅仅用CSS 2就可以实现! 实例URL ...
- js-jquery-插件开发(一)
jQuery插件开发模式 jQuery插件开发方式主要有三种:1.通过$.extend()来扩展jQuery 主要是在jQuery命名空间或者理解成jQuery身上添加了一个静态方法2.通过$.fn ...
- col-md-*和col-sm-*
屏幕大于(≥992px) ,使用col-md-* 而不是col-sm-*如果屏幕大于(≥768px),小雨<=992px,使用col-sm-* 而不是col-md-*
- 机器学习理论基础学习12---MCMC
作为一种随机采样方法,马尔科夫链蒙特卡罗(Markov Chain Monte Carlo,以下简称MCMC)在机器学习,深度学习以及自然语言处理等领域都有广泛的应用,是很多复杂算法求解的基础.比如分 ...
- try except else
try except 语句还有一个可选的else子句,如果使用这个子句,那么必须放在所有的except子句之后.这个子句将在try子句没有发生任何异常的时候执行.例如: for arg in sys. ...
- JQuery Form AjaxSubmit(options)在Asp.net中的应用注意事项
所需引用的JS: 在http://www.malsup.com/jquery/form/#download 下载:http://malsup.github.com/jquery.form.js 在ht ...
- mvn deploy 推送到私有仓库,注意当前日期
由于更改了本机系统时间到过去的一个时间,导致mvn deploy推送到私有仓库后,该更新的jar包时间戳比较旧,客户端不能更新得到新的jar包.
- 怎么查看是否安装Scrapy
1.在python shell 下输入 import scrapy
- Linux基础命令---findfs
findfs 查找指定卷标或者UUID的文件系统对应的设备文件.findfs将搜索系统中的磁盘,寻找具有标签匹配标签或与UUID相等的文件系统.如果找到文件系统,文件系统的设备名称将打印在stdout ...