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

  1. 3 3
  2. 1 2 100000
  3. 2 3 10001
  4. 1 3 100000
  5. 3 3
  6. 1 2 1
  7. 2 3 1
  8. 1 3 2

Sample Output 1

  1. 99993
  2. 2

我觉得这个OJ可能又要没了

题意:从1走到n,其中有m条单向路径,走过某条路,当前拥有的价值乘上路径权值,初始价值1,问你走到n时你最多有多少价值。

思路:最长路可以spfa取负值来做,问题在于怎么把乘法转化为加法,这里用取对数log来解决,这样就能用加法算出乘的最多的是多少了。然后我们再用一个东西保存没log前的数据来计算答案。一开始用了回溯来计算最终乘积,debug不出来了...改了直接在算最长路时就计算乘积的结果。70+%的AC率被我拖到了50%emmm

代码:

  1. #include<cstdio>
  2. #include<set>
  3. #include<vector>
  4. #include<cmath>
  5. #include<queue>
  6. #include<cstring>
  7. #include<algorithm>
  8. typedef long long ll;
  9. using namespace std;
  10. const int maxn = +;
  11. const double INF = 0x3f3f3f3f;
  12. const ll MOD = ;
  13. struct Edge{
  14. int v;
  15. int w;
  16. double logw;
  17. Edge(int _v = , int _w = , double _logw = ): v(_v), w(_w), logw(_logw){}
  18. };
  19. vector<Edge> G[maxn];
  20. bool vis[maxn];
  21. double dis[maxn];
  22. ll dist[maxn];
  23. void spfa(int start,int n){
  24. memset(vis,false,sizeof(vis));
  25. for(int i = ;i <= n;i++) dis[i] = INF;
  26. vis[start] = true;
  27. dis[start] = ;
  28. dist[start] = ;
  29. queue<int> q;
  30. while(!q.empty()) q.pop();
  31. q.push(start);
  32. while(!q.empty()){
  33. int u = q.front();
  34. q.pop();
  35. vis[u] = false;
  36. for(int i = ;i < G[u].size();i++){
  37. int v = G[u][i].v;
  38. double w = G[u][i].logw;
  39. if(dis[v] > dis[u] + w){ //u -> v
  40. dis[v] = dis[u] + w;
  41. dist[v] = (dist[u] * G[u][i].w) % MOD;
  42. if(!vis[v]){
  43. q.push(v);
  44. vis[v] = true;
  45. }
  46. }
  47. }
  48. }
  49. }
  50. void addEdge(int u,int v,int w){
  51. double logw = -log(w);
  52. G[u].push_back(Edge(v, w, logw));
  53. }
  54. int main(){
  55. int n, m;
  56. while(scanf("%d%d", &n, &m) != EOF){
  57. for(int i = ;i <= n;i++) G[i].clear();
  58. int s,e;
  59. int t;
  60. while(m--){
  61. scanf("%d%d%d", &s, &e, &t);
  62. addEdge(s,e,t);
  63. }
  64. spfa(, n);
  65. printf("%lld\n", dist[n]);
  66. }
  67. return ;
  68. }
  69. /*
  70. 3 3
  71. 1 2 100000
  72. 2 3 10001
  73. 1 3 100000
  74. 3 3
  75. 1 2 1
  76. 2 3 1
  77. 1 3 2
  78. */

FJNUOJ the greed of Yehan(最长路 + 权值乘积转化)题解的更多相关文章

  1. spfa求最长路

    http://poj.org/problem?id=1932 spfa求最长路,判断dist[n] > 0,需要注意的是有正环存在,如果有环存在,那么就要判断这个环上的某一点是否能够到达n点,如 ...

  2. zoj 3795 Grouping tarjan缩点 + DGA上的最长路

    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practic ...

  3. 寒冰王座(DGA最长路/完全背包)

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

  4. 【HDU3721】枚举+最长路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3721 题意:给你一颗n个节点n-1条边的树,每条边都有一个权值,现在让你任意移动一条边然后把这条边连接 ...

  5. [USACO2003][poj2138]Travel Games(dp/最长路)

    http://poj.org/problem?id=2138 题意:给你一些单词和初始单词,在初始单词的任意位置你可以加任意一个字母,使得这个新单词在给的单词中有所出现,然后在这样不断迭代下去,让你求 ...

  6. hiho #1050 : 树中的最长路 树的直径

    #1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中, ...

  7. NYOJ16 矩形嵌套(DAG最长路)

    矩形嵌套 紫书P262 这是有向无环图DAG(Directed Acyclic Graph)上的动态规划,是DAG最长路问题 [题目链接]NYOJ16-矩形嵌套 [题目类型]DAG上的dp & ...

  8. 中南大学oj 1317 Find the max Link 边权可以为负的树上最长路 树形dp 不能两遍dfs

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1317经典问题:树上最长路,边权可以为负值的,树形dp,不能用两边dfs.反例:5 41 2 22 ...

  9. zoj 3088 Easter Holidays(最长路+最短路+打印路径)

    Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are prov ...

随机推荐

  1. 异步通信----WebSocket

    什么是WebSocket? WebSocket API是下一代客户端-服务器的异步通信方法.该通信取代了单个的TCP套接字,使用ws或wss协议,可用于任意的客户端和服务器程序.WebSocket目前 ...

  2. 自定义vueawesomeswiper分页器样式

    swiperOption: {//swiper的配置项 notNextTick: true,//想获得swiper实例对象,这个必须为true direction: 'vertical', // gr ...

  3. 洛谷P1494小Z的袜子 [国家集训队] 莫队

    正解:莫队 解题报告: 这是,传送门qwq 昂大概是莫队板子题? 首先可以推出来答案是(∑C(2,color[i]))/C(2,r-l+1)趴?挺显然的不解释了qwq 然后显然除数直接做就成,考虑怎么 ...

  4. linux平台mysql密码设破解

    1.先停止mysql服务 service mysqld stop 2.启动mysql服务 并跳过权限认证 mysqld_safe --skip-grant-tables 3.打开另外一个终端 登录my ...

  5. 前端 HTML的规范

    1.编写HTML规范 1)所有标记元素都要正确的嵌套,不能交叉嵌套.正确写法举例:<h1><font></font></h1> (2)HTML标签通常是 ...

  6. A solution for MySQL Assertion failure FIL_NULL

    A solution for MySQL Assertion failure FIL_NULL http://michaelfranzl.com/2014/01/25/solution-mysql-a ...

  7. js-jquery-SweetAlert【一】使用

    一.下载安装 地址:http://t4t5.github.io/sweetalert/ 二.页面引用 <script src="dist/sweetalert.min.js" ...

  8. Java-小技巧-002-String 转 long

    1.转化 long l = Long.parseLong([String]); 相当于 long l = Long.parseLong([String],10); long l = Long.valu ...

  9. Spark2.x学习笔记:Spark SQL快速入门

    Spark SQL快速入门 本地表 (1)准备数据 [root@node1 ~]# mkdir /tmp/data [root@node1 ~]# cat data/ml-1m/users.dat | ...

  10. hdu1542 Atlantis(矩阵面积的并)

    这个题算是我的第一个扫描线的题,扫描线算是一种思想吧,用到线段树+离散化.感觉高大上. 主要参考了这位大神的博客. http://www.cnblogs.com/kuangbin/archive/20 ...