transaction transaction transaction

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

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

 

建立源点和汇点。

源点连所有的树上点, 边权为 -a[i],表示起点,需要花费a[i],所有树上点在连接 汇点, 边权为a[i],表示收益为a[i],然后在根据树建图,边权为-w,表示花费w。

然后spfa跑个最长路,答案为dis[汇点]。

 //2017-09-11
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue> using namespace std; const int N = ;
const int INF = 0x3f3f3f3f;
int head[N], tot;
struct Edge{
int v, w, next;
}edge[N<<]; void init(){
tot = ;
memset(head, -, sizeof(head));
} void add_edge(int u, int v, int w){
edge[tot].v = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
} bool vis[N];
int dis[N];
int cnt[N];
deque<int> dq;
bool spfa(int s, int n){
memset(vis, , sizeof(vis));
memset(cnt, , sizeof(cnt));
for(int i = ; i <= n+; i++)
dis[i] = -INF;
vis[s] = ;
dis[s] = ;
cnt[s] = ;
deque<int> dq;
dq.push_back(s);
while(!dq.empty()){
int u = dq.front();
dq.pop_front();
vis[u] = ;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].v;
if(dis[v] < dis[u] + edge[i].w){
dis[v] = dis[u] + edge[i].w;
if(!vis[v]){
vis[v] = ;
dq.push_back(v);
if(++cnt[v] > n)return false;
}
}
}
}
return true;
} int arr[N], n; int main()
{
int T;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
init();
int s = , t = n+;
for(int i = ; i <= n; i++){
scanf("%d", &arr[i]);
add_edge(s, i, -arr[i]);
add_edge(i, t, arr[i]);
}
int u, v, w;
for(int i = ; i < n-; i++){
scanf("%d%d%d", &u, &v, &w);
add_edge(u, v, -w);
add_edge(v, u, -w);
}
spfa(s, n);
printf("%d\n", dis[t]);
} return ;
}

HDU6201的更多相关文章

  1. [hdu6201]transaction transaction transaction(树形dp)

    题意:某人在一棵树中在某处买物品,价格为i,在某处卖物品,价格为j,每单位距离花费价格1,求最大赚钱数. 解题关键:两次树形dp,分别求出每个点作为被减和被加情况下的最大值,最后取一下max即可. 该 ...

  2. hdu6201 transaction transaction transaction(from 2017 ACM/ICPC Asia Regional Shenyang Online)

    最开始一直想着最短路,不过看完题解后,才知道可以做成最长路.唉,还是太菜了. 先上图: 只要自己添加两个点,然后如此图般求最长路即可,emmm,用SPFA可以,迪杰斯特拉也可以,或者别的都ok,只要通 ...

  3. 「算法笔记」树形 DP

    一.树形 DP 基础 又是一篇鸽了好久的文章--以下面这道题为例,介绍一下树形 DP 的一般过程. POJ 2342 Anniversary party 题目大意:有一家公司要举行一个聚会,一共有 \ ...

随机推荐

  1. 关于esp32的ADC采集

    对于ADC采集 程序源码如下: /* ADC1 Example This example code is in the Public Domain (or CC0 licensed, at your ...

  2. java web中的异常处理

    1.集中处理 参考:https://blog.csdn.net/weililansehudiefei/article/details/73691294

  3. attempt to open datawindow failed@安装两个PB软件

    attempt to open datawindow failed@安装两个PB软件 安装了不同版本的PB软件后,默认打开方式为后一个安装的版本. 如果想改为第一个安装版本,在“右键”——>”打 ...

  4. AFNetworking 3.0中调用[AFHTTPSessionManager manager]方法导致内存泄漏的解决办法

    在使用AFNetworking3.0框架,使用Instruments检查Leaks时,检测到1000多个内存泄漏的地方,定位到 [AFHTTPSessionManager manager] 语句中,几 ...

  5. 背水一战 Windows 10 (80) - 本地化

    [源码下载] 背水一战 Windows 10 (80) - 本地化 作者:webabcd 介绍背水一战 Windows 10 之 本地化 Demo 改变语言 示例1.演示本地化的基本应用Localiz ...

  6. ZKWeb网页框架1.8正式发布

    1.8.0更新的内容有 破坏性更新 更新ZKWeb.System.Drawing到3.0.0 请修改源代码中的System.Drawing到System.DrawingCore 现在已经不再需要使用D ...

  7. 关于Linux MongoDB的安装

    前一篇博文讲解了如何安装与配置MongoDB的windows版,本篇博文接着上一篇讲解如何在Linux系统中安装与配置MongoDB,为了演示,我问同事要了它的云服务器用于演示,当然我自己也有,但是已 ...

  8. 突然 不能f**q

    ss 突然访问不了,于是去查看ip是否被f,发现国outer 国inner 都通,不知道什么情况,后来把 系统代理模式 改为全局,发觉可以,又把他改为 pac模式,正常了.           记录一 ...

  9. Android-如何显示版本号并制作3秒跳转页

    前言 大家好,给大家带来Android-如何显示版本号并制作3秒跳转页的概述,希望你们喜欢 软件技术人员,时代作者,从 Android 到全栈之路,我相信你也可以!阅读他的文章,会上瘾!You and ...

  10. Git - 基础介绍

    Git Git - HomePage Git - CHEAT SHEET 开源的分布式版本控制系统,用于敏捷高效地管理项目版本. 下载与安装Git https://git-scm.com/downlo ...