//当时比赛的时候没有想到可以用SPFA做,TLE!

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
 
Recommend
 
 
liuyiding   |   We have carefully selected several similar problems for you:  6205 6204 6203 6202 6201 
 
 
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<functional>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL; //最大费用最大流 简化到SPFA上特例
const int MAXN = + ;
struct edge
{
int to, next, cost;
}E[MAXN << ];
int head[MAXN], tot, n;
int val[MAXN];
bool vis[MAXN];
int lowcost[MAXN];
void init()
{
memset(head, -, sizeof(head));
tot = ;
}
void addedge(int u, int v, int d)
{
E[tot].to = v;
E[tot].cost = d;
E[tot].next = head[u];
head[u] = tot++;
}
int spfa(int st, int ed)
{
memset(vis, false, sizeof(vis));
memset(lowcost, , sizeof(lowcost));
queue<int> q;
q.push(st);
vis[st] = true;
lowcost[st] = ;
while (!q.empty())
{
int f = q.front();
q.pop();
vis[f] = false;
for (int i = head[f]; i != -; i = E[i].next)
{
int v = E[i].to, d = E[i].cost;
if (lowcost[v] < lowcost[f] + d)
{
lowcost[v] = lowcost[f] + d;
if (!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
return lowcost[ed];
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
init();
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
scanf("%d", &val[i]);
addedge(, i, val[i]);
addedge(i, n + , -val[i]);
}
int u, v, d;
for (int i = ; i < n - ; i++)
{
scanf("%d%d%d", &u, &v, &d);
addedge(u, v, -d);
addedge(v, u, -d);
}
printf("%d\n", spfa(, n + ));
}
}

transaction transaction transaction 最大费用最大流转化到SPFA最长路的更多相关文章

  1. 【Luogu】P3381最小费用最大流模板(SPFA找增广路)

    题目链接 哈  学会最小费用最大流啦 思路是这样. 首先我们有一个贪心策略.如果我们每次找到单位费用和最短的一条增广路,那么显然我们可以把这条路添加到已有的流量里去——不管这条路的流量是多大,反正它能 ...

  2. hdu 6437 /// 最小费用最大流 负花费 SPFA模板

    题目大意: 给定n,m,K,W 表示n个小时 m场电影(分为类型A.B) K个人 若某个人连续看了两场相同类型的电影则失去W 电影时间不能重叠 接下来给定m场电影的 s t w op 表示电影的 开始 ...

  3. P1559 运动员最佳匹配问题[最大费用最大流]

    题目描述 羽毛球队有男女运动员各n人.给定2 个n×n矩阵P和Q.P[i][j]是男运动员i和女运动员j配对组成混合双打的男运动员竞赛优势:Q[i][j]是女运动员i和男运动员j配合的女运动员竞赛优势 ...

  4. HDU 6201 transaction transaction transaction(拆点最长路)

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

  5. hdu 6501 transaction transaction transaction 最长路/树形DP/网络流

    最长路: 设置一个虚拟起点和虚拟终点,每个点与起点间一条负边,值为这个点书的价值的相反数(代表买书花钱),每个点与终点连一条正边,值为这个点的书的价格(代表卖书赚钱). 然后按照图中给的边建无向边,权 ...

  6. 【BZOJ】1221: [HNOI2001] 软件开发(最小费用最大流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1221 先吐槽一下,数组依旧开小了RE:在spfa中用了memset和<queue>的版本 ...

  7. Doctor NiGONiGO’s multi-core CPU(最小费用最大流模板)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=693 题意:有一个 k 核的处理器和 n 个工作,全部的工作都须要在一个核上处理一个单位的 ...

  8. [BZOJ2324][ZJOI2011][最小费用最大流]营救皮卡丘

    [Problem Description] 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘,也为了正义,小智和他的朋友们义不容辞的踏上了营救皮卡丘的道路. 火箭队 ...

  9. 2018牛客网暑期ACM多校训练营(第五场) E - room - [最小费用最大流模板题]

    题目链接:https://www.nowcoder.com/acm/contest/143/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

随机推荐

  1. 【工具】sublime使用技巧

    Ctrl+N 新建一个编辑区,Ctrl+Shift+C 或!加 Ctrl+E新建一个骨架完好的文件. Ctrl+Shift+P开启命令模式,sshtml 切换html语法. esc退出,Ctrl+`打 ...

  2. Javascript数据结构之栈

    作者原文:http://hawkzz.com/blog/blog/1515054561771 定义 栈是一种特殊的列表,栈内的元素只能通过列表的一端访问,这一端称为栈顶.栈被称为一种先入后出的数据结构 ...

  3. 用unsigned char 表示字节

    在C中,默认的基础数据类型均为signed,现在我们以char为例,说明(signed) char与unsigned char之间的区别 首先在内存中,char与unsigned char没有什么不同 ...

  4. Python基础教程 读书笔记(2)第二章 列表和元组

    2.1序列概览 列表和元组的主要区别在于,列表可以修改,元组则不能.也就是说如果要根据要求来添加元素,那么列表可能会更好用;而出于某些原因,序列不能修改的时候,使用元组则更为合适.使用后者的理由通常是 ...

  5. 详解java中staitc关键字

    一.static定义 static是静态修饰符意思,什么叫静态修饰符呢?大家都知道,在程序中任何变量或者代码都是在编译时由系统自动分配内存来存储的,而所谓静态就是指在编译后所分配的内存会一直存在,直到 ...

  6. Mybatis 分页插件 PageHelper

    话不多说,直接导入.部署流程. 1. 引入插件依赖包: maven工程中,pom.xml文件下,添加插件配置项: 2. 配置插件拦截器: webapp -> WEB-INF 下添加 .xml配置 ...

  7. CentOS 6.4 linux下编译安装MySQL5.6.14

    CentOS 6.4下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.14. 正文: 一:卸载旧版本 使用下面的命令检查是否安装有MySQL Server rpm ...

  8. H3C AR28-31路由器组网实验

    接线图 可以发现PC1和PC2不在一个网段上,如果不靠路由器就不可能ping,所以要用路由器组网 接线步骤 串行线连接路由器1与路由器2 以太网线连路由器以太网口 与 交换机接口 计算机网线连交换机口 ...

  9. iTOP-4412开发板-实战教程-ssh服务器移植到arm开发板

    本文转自迅为开发板:http://www.topeetboard.com 在前面实战教程中,移植了“串口文件传输工具”,整个移植过程是比较简单的,而且我 们没有做任何协议方面的了解,只是“配置”+“编 ...

  10. OpenFlow_tutorial_1_Introduce

    tutorial出处:https://github.com/mininet/openflow-tutorial/wiki OpenFlow是用于远程控制交换机流表(forwarding tables) ...