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的可能路径长度取最大值输出即可。
代码:
 #include<bits/stdc++.h>
//#include<regex>
#define db double
#define ll long long
#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define MP make_pair
#define PB push_back
#define fr(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int N=1e6+;
const int mod=1e9+;
const int MOD=mod-;
const db eps=1e-;
const db pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int a[N],d[N],vis[N]; struct P
{
int u,v,w;
P(int x,int y,int z):u(x),v(y),w(z){};
P(){};
};
vector<P> g[N],e;
queue<int> q;
void add(int x,int y,int z)
{
g[x].push_back(P(x,y,z));
}
void spfa(int n)
{
memset(d,, sizeof(d));
memset(vis,, sizeof(vis));
vis[]=;
q.push();
while(q.size())
{
int u=q.front();q.pop();
vis[u]=;
for(int i=;i<g[u].size();i++){
int v=g[u][i].v;
int w=g[u][i].w;
if(d[v]<d[u]+w){// get the maxmum
d[v]=d[u]+w;
if(!vis[v]){
vis[v]=;//push the new point
q.push(v);
}
}
}
}
pi(d[n+]);
}
int main()
{
int t;
ci(t);
while(t--)
{
int n;
ci(n);
for(int i=;i<=n;i++) g[i].clear();
for(int i=;i<=n;i++)
ci(a[i]),add(,i,a[i]),add(i,n+,-a[i]);
for(int i=;i<n;i++){
int x,y,z;
ci(x),ci(y),ci(z);
add(x,y,-z);
add(y,x,-z);
}
spfa(n); } }

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. spring技术小结

    1.DI和IOC 依赖注入(Dependency Injection)还是控制反转(Inversion of Conctrol) bean通过依赖注入,注册到spring容器里面.spring容器通过 ...

  2. kickstart2019 round_C B. Circuit Board

    思路: 这题应该不止一种解法,其中的一种可以看作是leetcode85https://www.cnblogs.com/wangyiming/p/11059176.html的加强版: 首先对于每一行,分 ...

  3. unicode字符和多字节字符的相互转换接口

    作者:朱金灿 来源:http://blog.csdn.net/clever101 发现开源代码的可利用资源真多,从sqlite3的源码中抠出了几个字符转换接口,稍微改造下了发现还挺好用的.下面是实现代 ...

  4. https验证新发现-老知识

    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); 可以设置https全局的域名校验规则 HttpsURLConnecti ...

  5. IUserStore------Implements Diagram

  6. LeetCode Search Insert Position (二分查找)

    题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...

  7. 数组使用NSUserDefaults存储的问题,

    最近在做搜索记录的时候,由于搜索记录是存储在本地的,而且都是字符串,我考虑到数据量也不是太大,于是就懒的使用数据库了. 于是就想到了NSUserDefaults 存储的方式, 但是由于之间对于数组没有 ...

  8. 18课 Vue第一节

    Q1: url-loader必须搭载file-loader?Q2: 图片的打包问题,如果直接写在img标签里用src引用图片,该如何打包?Q3: 如何根据不同的页面html模板打包与之对应的css/j ...

  9. RxJava2 方法总结

    RxJava2 方法总结 看了许多讲解RxJava的文章,有些文章讲解的内容是基于第一个版本的,有些文章的讲解是通过比较常用的一些API和基础的概念进行讲解的. 但是每次看到RxJava的类中的几十个 ...

  10. 使用FreeMarker导出word文档(支持导出图片)

    一.添加maven依赖,导入FreeMarker所需要的jar包 <dependency> <groupId>org.freemarker</groupId> &l ...