HDU6201
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
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
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
Sample Input
4
10 40 15 30
1 2 30
1 3 2
3 4 10
Sample Output
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的更多相关文章
- [hdu6201]transaction transaction transaction(树形dp)
题意:某人在一棵树中在某处买物品,价格为i,在某处卖物品,价格为j,每单位距离花费价格1,求最大赚钱数. 解题关键:两次树形dp,分别求出每个点作为被减和被加情况下的最大值,最后取一下max即可. 该 ...
- hdu6201 transaction transaction transaction(from 2017 ACM/ICPC Asia Regional Shenyang Online)
最开始一直想着最短路,不过看完题解后,才知道可以做成最长路.唉,还是太菜了. 先上图: 只要自己添加两个点,然后如此图般求最长路即可,emmm,用SPFA可以,迪杰斯特拉也可以,或者别的都ok,只要通 ...
- 「算法笔记」树形 DP
一.树形 DP 基础 又是一篇鸽了好久的文章--以下面这道题为例,介绍一下树形 DP 的一般过程. POJ 2342 Anniversary party 题目大意:有一家公司要举行一个聚会,一共有 \ ...
随机推荐
- 3 week work—Grid Layout
HTML: <div class="wrapper"> //建立一个三列轨道网格. <div class="one">One</d ...
- IPC_管道
1.管道特点: 1)单向数据通信 2)匿名管道-常用于(父子进程/有血缘关系的进程之间) 3)命名管道-常用于(无血缘关系进程之间通信) 4)提供一种流式服务(发送和接受不接受字节数的大小,可取任意大 ...
- 如何将uniurlframe中html调用delphi的函数
uniGUI总群中台中cmj朋友为我们总结了如下内容,对于利用delphi+uniGUI开发应用,可以说是精品,必须掌握. 一句话,如何在html与delphi间交互代码,是最好的答案. [Clien ...
- 再谈控制 cxGrid 的行列颜色
1. [转]CxGrid 改变某行或单元格的颜色 (2016-01-19 09:37:19) 转载▼ 标签: it delphi 分类: Delphi 一个表(T)的结构结构如下. ID Test 1 ...
- 五、MongoDB的索引
一.MongoDB的下载.安装与部署 二.MongoDB的基础知识简介 三.MongoDB的创建.更新和删除 四.MongoDB的查询 五.MongoDB的索引 1.简介 它就像是一本书的目录,如果没 ...
- .Wait()与.GetAwaiter()之间有什么区别
两者都是同步等待操作的结果差异主要在于处理异常.使用Wait,异常堆栈跟踪不会改变并表示异常时的实际堆栈,因此如果您有一段代码在线程池线程上运行,那么您将拥有类似的堆栈 ThreadPoolThrea ...
- Lerning Entity Framework 6 ------ Handling concurrency With SQL Server Database
The default Way to handle concurrency of Entity Framework is using optimistic concurrency. When two ...
- 将Linux(ubuntu)安装到U盘上,实现即插即用
说明: 本教程是说明如何将ubuntu系统安装到U盘上(也就是把U盘当做电脑的硬盘),可以实现U盘插到任何电脑上都能够在实体机上运行ubuntu系统,而且所有的运行配置都能被保存,相当于随身携带的一个 ...
- MSRA-TD5000数据集使用详解
中文检测的数据集,目前最火的应该是清华的CTW,https://ctwdataset.github.io/ 但是它的数据集只存储在微云和google driver,微云空间受限不能完全保存,所以下载的 ...
- 【Spark调优】:结合业务场景,优选高性能算子
聚合操作使用reduceByKey/aggregateByKey替代groupByKey 参见我的这篇博客说明 [Spark调优]:如果实在要shuffle,使用map侧预聚合的算子 内存充足前提下使 ...