HDU
来源:互联网 发布:淘宝如何使用分期付款 编辑:程序博客网 时间:2024/06/07 07:23
transaction transaction transaction
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 isai yuan in i t city. Kelukin will take taxi, whose price is 1 yuan per km and this fare cannot be ignored.
There aren−1 roads connecting n cities. Kelukin can choose any city to start his travel. He want to know the maximum money he can get.
As we know, the price of this book was different in each city. It is
There are
Input
The first line contains an integer T (1≤T≤10 ) , the number of test cases.
For each test case:
first line contains an integern (2≤n≤100000 ) means the number of cities;
second line containsn numbers, the i th number means the prices in i th city; (1≤Price≤10000)
then followsn−1 lines, each contains three numbers x , y and z which means there exists a road between x and y , the distance is z km (1≤z≤1000) .
For each test case:
first line contains an integer
second line contains
then follows
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 301 3 23 4 10
Sample Output
8
Source
2017 ACM/ICPC Asia Regional Shenyang Online
题意:给你一棵树,书上每个节点代表一个城市,有一本书,每个城市的售价不一样,问你选择两个城市,一个城市买,一个城市卖,能获得的最大利润是多少,树的边的权值就是路费,也要算进去。
解题思路:n遍spfa就过了……比赛时没想到,一直以为这是n平方的算法,实际上不是,因为在前面你求得最短路的时候,后面的spfa实际上不用执行n遍,即很少结点会入队……spfa就会退化变成一个动态规划算法了……另外,因为是树,不用判负环,之前一直负环,导致多了一个数组的初始化,一直超时……然后插入边的时候权值稍作修改,可以节省代码,详见代码。
#include<iostream>#include<deque>#include<memory.h>#include<stdio.h>#include<map>#include<string>#include<algorithm>#include<vector>#include<math.h>#include<stack>#include<queue>#include<set>using namespace std;const int maxn = 100500;inline void scan_d(int &ret){ char c; ret = 0; while ((c = getchar()) < '0' || c > '9'); while (c >= '0' && c <= '9') { ret = ret * 10 + (c - '0'), c = getchar(); }}struct edge{ int v1,v2,w,next;}e[2*maxn];//这里要开两倍……否则超时……醉了int N;int edge_num;int head[maxn];int d[maxn];int price[maxn];void insert_edge(int v1,int v2,int w){ e[edge_num].v1=v1; e[edge_num].v2=v2; e[edge_num].w=w; e[edge_num].next=head[v1]; head[v1]=edge_num++;}void spfa(int s){ queue<int> que; que.push(s); while(!que.empty()){ int tp=que.front(); que.pop(); for(int i=head[tp];i!=-1;i=e[i].next) if(d[tp]+e[i].w>d[e[i].v2]){//求最大路 d[e[i].v2]=d[tp]+e[i].w; que.push(e[i].v2); } }}int main (){ int t; scanf("%d",&t); while(t--){ scan_d(N); memset(d,0,sizeof(d)); memset(head,-1,sizeof(head)); for(int i=1;i<=N;i++){ scan_d(price[i]); } int u, v, ds; edge_num=0; for(int i=1;i<=N-1;i++){ scan_d(u); scan_d(v); scan_d(ds); insert_edge(u, v, -ds-price[u]+price[v]);//实际上就是在u买书,v卖书,直接算进权值就好了 insert_edge(v, u, -ds-price[v]+price[u]); } for(int i=1;i<=N;i++) spfa(i); int ans=0; for(int i=1;i<=N;i++) ans=max(ans,d[i]); printf("%d\n",ans); } return 0;}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- JVM调优总结 -Xms -Xmx -Xmn -Xss
- [BZOJ3211]花神游历各国-树状数组-并查集
- 实战1.Spark及其生态圈简介
- Lintcode119 Edit Distance solution 题解
- tomcat1.8,tomcat-users.xml文件中如下修改才可以使用app manager登录,其中的roles有哪些枚举需要确认
- HDU
- kotlin学习笔记1
- html+js日历
- 168. Excel Sheet Column Title
- 数据库基本操作
- mybatis一级二级缓存
- iOS【开发之 APP自定义换肤、主题更换】
- 制造业从中国回流英国 英媒:工业史上的奇妙反转
- iOS 用symbolicatecrash来符号化崩溃日志