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 题目大意:有一家公司要举行一个聚会,一共有 \ ...
随机推荐
- 异步请求获取JSON数据
json格式的数据广泛应用于异步请求中前后端的数据交互,本文主要介绍几种使用场景和使用方法. <script type="text/javascript"> funct ...
- Codeforces gym101612 E.Equal Numbers(贪心)
传送:http://codeforces.com/gym/101612 题意:给出一个大小为n的序列a[i],每次选其中一个数乘以一个正整数,问进行k步操作后最少剩下多少种数字,输出0≤k≤n,所有的 ...
- 关于height、offsetheight、clientheight、scrollheight、innerheight、outerheight的区别一览
平时,不管在pc端页面还是移动端页面,因为我们一般很少会设置某个块的的高度,但是呢,我有时候有需要取到这些高度以便于我们方便进行判断和下一步的编写.一般这个时候我都是直接的获取一个块的高度.heigh ...
- Shell-14--awk
awk ' 条件1{ 动作1} 条件2{动作2}...' 文件名 awk处理数据是 先读取第一行 然后再去处理 printf 不会加入换行符,需要手动加入 print 会自动加换行 begin 是在后 ...
- RSA实现JS前端加密,PHP后端解密
web前端,用户注册与登录,不能直接以明文形式提交用户密码,容易被截获,这时就引入RSA. 前端加密 需引入4个JS扩展文件,jsbn.js.prng4.js.rng.js和rsa.js. <h ...
- fast.ai(零)windows + pytorch 0.4
一.下载 git clone https://github.com/fastai/fastai.git 或者直接下载下来 二.安装pytorch 去官网安装建议安装即可 https://pytorch ...
- jvm-class文件简介
jvm全称 java virtual machine (java虚拟机),也就是在计算机上再虚拟一个计算机,它存在于计算机内存中并运行在操作系统之上的. javap -v class文件名 > ...
- Java 基础学习总结(一)抽象类和接口
接触java的时间不是很长,以前对抽象类和接口的定义和区别也是模糊不清,最近拿起学校的教程读了起来,也参阅了网上的博客大神理解和总结,于是决定自己按照自己的理解来总结一下. 抽象类(半成品) 一般 ...
- [EXP]XAMPP 5.6.8 - SQL Injection / Persistent Cross-Site Scripting
<!-- # Exploit Title: SQL injection (and previous) # Date: -- # Exploit Author: Rafael Pedrero # ...
- JAVA基础--重新整理(1)后版
比较喜欢用demo来讲解. 变量: public static void main(String[] args) { int age;//变量声明 age = 16;//变量的初始化,第一次赋值 ag ...