transaction transaction transaction

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 1496    Accepted Submission(s): 723

Problem Description
Kelukin is a businessman. Every day, he travels around cities to do some business. On August 17th, in memory of a great man, citizens will read a book named "the Man Who Changed China". Of course, Kelukin wouldn't miss this chance to make money, but he doesn't have this book. So he has to choose two city to buy and sell. 
As we know, the price of this book was different in each city. It is ai yuan in it city. Kelukin will take taxi, whose price is 1yuan per km and this fare cannot be ignored.
There are n−1 roads connecting n cities. Kelukin can choose any city to start his travel. He want to know the maximum money he can get.
 
Input
The first line contains an integer T (1≤T≤10) , the number of test cases. 
For each test case:
first line contains an integer n (2≤n≤100000) means the number of cities;
second line contains n numbers, the ith number means the prices in ith city; (1≤Price≤10000) 
then follows n−1 lines, each contains three numbers x, y and z which means there exists a road between x and y, the distance is zkm (1≤z≤1000). 
 
Output
For each test case, output a single number in a line: the maximum money he can get.
 
Sample Input
1
4
10 40 15 30
1 2 30
1 3 2
3 4 10
 
Sample Output
8
 
Source
 
思路:建立一个原点0和汇点n+1,将所有点从0到n+1的可能路径长度取最大值输出即可。
代码:
  1. #include<bits/stdc++.h>
  2. //#include<regex>
  3. #define db double
  4. #define ll long long
  5. #define vec vector<ll>
  6. #define Mt vector<vec>
  7. #define ci(x) scanf("%d",&x)
  8. #define cd(x) scanf("%lf",&x)
  9. #define cl(x) scanf("%lld",&x)
  10. #define pi(x) printf("%d\n",x)
  11. #define pd(x) printf("%f\n",x)
  12. #define pl(x) printf("%lld\n",x)
  13. #define MP make_pair
  14. #define PB push_back
  15. #define fr(i,a,b) for(int i=a;i<=b;i++)
  16. using namespace std;
  17. const int N=1e6+;
  18. const int mod=1e9+;
  19. const int MOD=mod-;
  20. const db eps=1e-;
  21. const db pi = acos(-1.0);
  22. const int inf = 0x3f3f3f3f;
  23. const ll INF = 0x3f3f3f3f3f3f3f3f;
  24. int a[N],d[N],vis[N];
  25.  
  26. struct P
  27. {
  28. int u,v,w;
  29. P(int x,int y,int z):u(x),v(y),w(z){};
  30. P(){};
  31. };
  32. vector<P> g[N],e;
  33. queue<int> q;
  34. void add(int x,int y,int z)
  35. {
  36. g[x].push_back(P(x,y,z));
  37. }
  38. void spfa(int n)
  39. {
  40. memset(d,, sizeof(d));
  41. memset(vis,, sizeof(vis));
  42. vis[]=;
  43. q.push();
  44. while(q.size())
  45. {
  46. int u=q.front();q.pop();
  47. vis[u]=;
  48. for(int i=;i<g[u].size();i++){
  49. int v=g[u][i].v;
  50. int w=g[u][i].w;
  51. if(d[v]<d[u]+w){// get the maxmum
  52. d[v]=d[u]+w;
  53. if(!vis[v]){
  54. vis[v]=;//push the new point
  55. q.push(v);
  56. }
  57. }
  58. }
  59. }
  60. pi(d[n+]);
  61. }
  62. int main()
  63. {
  64. int t;
  65. ci(t);
  66. while(t--)
  67. {
  68. int n;
  69. ci(n);
  70. for(int i=;i<=n;i++) g[i].clear();
  71. for(int i=;i<=n;i++)
  72. ci(a[i]),add(,i,a[i]),add(i,n+,-a[i]);
  73. for(int i=;i<n;i++){
  74. int x,y,z;
  75. ci(x),ci(y),ci(z);
  76. add(x,y,-z);
  77. add(y,x,-z);
  78. }
  79. spfa(n);
  80.  
  81. }
  82.  
  83. }

2017 ACM/ICPC Shenyang Online SPFA+无向图最长路的更多相关文章

  1. 2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

  2. 2017 ACM ICPC Asia Regional - Daejeon

    2017 ACM ICPC Asia Regional - Daejeon Problem A Broadcast Stations 题目描述:给出一棵树,每一个点有一个辐射距离\(p_i\)(待确定 ...

  3. 2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest

    2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest A - Arranging Wine 题目描述:有\(R\)个红箱和\(W\)个白箱,将这 ...

  4. 2017 ACM/ICPC Asia Regional Qingdao Online

    Apple Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submi ...

  5. XYZZY(spfa求最长路)

    http://acm.hdu.edu.cn/showproblem.php?pid=1317 XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  6. POJ 3126 --Father Christmas flymouse【scc缩点构图 &amp;&amp; SPFA求最长路】

    Father Christmas flymouse Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3007   Accep ...

  7. HDU - 6201 transaction transaction transaction(spfa求最长路)

    题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少. 分析: 1.从某个结点出发,首先需要在该结点a花费price[a]买 ...

  8. spfa求最长路

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

  9. BZOJ 2019 [Usaco2009 Nov]找工作:spfa【最长路】【判正环】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2019 题意: 奶牛们没钱了,正在找工作.农夫约翰知道后,希望奶牛们四处转转,碰碰运气. 而 ...

随机推荐

  1. Maven的学习资料收集--(十)Myeclipse下创建Maven的Web项目

    先要在MyEclipse中对Maven进行设置: 到此Maven对MyEclipse的支持设置完毕.   下面我们在MyEclipse中创建一个Maven标准的Web工程: New --> We ...

  2. Docker | 第七章:Docker Compose服务编排介绍及使用

    前言 前面章节,我们学习了如何构建自己的镜像文件,如何保存自己的镜像文件.大多都是一个镜像启动.当一个系统需要多个子系统进行配合时,若每个子系统也就是镜像需要一个个手动启动和停止的话,那估计实施人员也 ...

  3. SpringBoot | 第七章:过滤器、监听器、拦截器

    前言 在实际开发过程中,经常会碰见一些比如系统启动初始化信息.统计在线人数.在线用户数.过滤敏高词汇.访问权限控制(URL级别)等业务需求.这些对于业务来说一般上是无关的,业务方是无需关系的,业务只需 ...

  4. POI 博客总结.....

    主键类: HSSFRow row1= sheet.createRow(1); row.createCell(0).setCellValue("学生编号"); row.createC ...

  5. 使用pycharm 运行python的django项目时报错“Quit the server with CTRL-BREAK.”

    Quit the server with CTRL-BREAK.Error: [Errno 10013] 1昨晚测试时还好好的,怎么突然出现这个错误,于是GOOLE,找到个帖子说可能是端口占用了,用工 ...

  6. 深入理解jvm jdk1,7(1)

    java 虚拟机管理的内存模型包含以下几个运行时数据区域: 程序计数器: 程序计数器是一块较小的内存空间,它可以看成当前线程执行的字节码的行号指示器.在虚拟机的概念模型里(仅是概念模型,各种虚拟机可能 ...

  7. 【经验总结】datagrid锁定列后重新加载时出现错位问题的解决

    [问题描述]:有时候datagrid设置了锁定列后,在重新加载datagrid数据时,出现锁定列与非锁定列数据错位的问题,如图: [问题分析]:查看css样式我们发现,锁定的列和非锁定的列属于两个不同 ...

  8. 【java】使用URL和CookieManager爬取页面的验证码和cookie并保存

    使用java的net包和io包下的几个工具爬取页面的验证码图片并保存到本地. 然后可以把获取的cookie保存下来,做进一步处理.比如通过识别验证码,进一步使用验证码和用户名,密码,保存下来的cook ...

  9. redis在Windows下以后台服务一键搭建集群(单机--伪集群)

    redis在Windows下以后台服务一键搭建集群(单机--伪集群) 一.概述 此教程介绍如何在windows系统中同一台机器上布置redis伪集群,同时要以后台服务的模式运行.布置以脚本的形式,一键 ...

  10. IOS 隐式动画(非Root Layer)

    ● 每一个UIView内部都默认关联着一个CALayer,我们可用称这个Layer为Root Layer(根 层) ● 所有的非Root Layer,也就是手动创建的CALayer对象,都存在着隐式动 ...