Hdu 6201 transaction transaction transaction【最长路】
来源:互联网 发布:win8安装不了软件 编辑:程序博客网 时间:2024/06/15 22:15
transaction transaction transaction
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 519 Accepted Submission(s): 245
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
题目大意:
给出N个点的一棵树,已知两点间距离,也知道每个点的点权值,点权值代表在这个点买或者卖物品花费的或者是得到的钱数、
我们现在只能在一个点购买物品,在一个点卖出物品,问最大的利润。
思路:
我们建立一个源点,使得源点连入各个点,花费是-val【i】,表示我们想要从这个点作为出发点的花费,然后再建立一个汇点,让各个点连入这个点,花费是val【i】,表示我们想要从这个点作为终点的利润。
中间边都要建成负权值,跑一条最长路即可。
Ac代码:
#include<stdio.h>#include<queue>#include<string.h>using namespace std;#define ll long long intstruct node{ ll from,to,next,w;}e[100005*7];ll cont,ss,tt;ll a[150000];ll head[150000];ll vis[150000];ll dist[150000];void add(ll from,ll to,ll w){ e[cont].to=to; e[cont].w=w; e[cont].next=head[from]; head[from]=cont++;}void SPFA(){ queue<ll>s; memset(vis,0,sizeof(vis)); for(ll i=0;i<=tt;i++)dist[i]=-10000000000000000; dist[0]=0; s.push(0); while(!s.empty()) { ll u=s.front(); vis[u]=0; s.pop(); for(ll i=head[u];i!=-1;i=e[i].next) { ll v=e[i].to; ll w=e[i].w; if(dist[v]<dist[u]+w) { dist[v]=dist[u]+w; if(vis[v]==0) { vis[v]=1; s.push(v); } } } } printf("%lld\n",dist[tt]);}int main(){ ll t; scanf("%lld",&t); while(t--) { cont=0; memset(head,-1,sizeof(head)); ll n;scanf("%lld",&n); for(ll i=1;i<=n;i++)scanf("%lld",&a[i]); for(ll i=1;i<=n-1;i++) { ll x,y,w; scanf("%lld%lld%lld",&x,&y,&w); add(x,y,-w);add(y,x,-w); } ss=0,tt=n+1; for(ll i=1;i<=n;i++)add(ss,i,-a[i]); for(ll i=1;i<=n;i++)add(i,tt,a[i]); SPFA(); }}
阅读全文
0 0
- Hdu 6201 transaction transaction transaction【最长路】
- HDU 6201 transaction transaction transaction【树形DP||SPFA最长路】
- hdu 6201 transaction transaction transaction(最长路)
- HDU 6201 transaction transaction transaction (最长路)
- hdu 6201 transaction transaction transaction (spfa求最长路)
- Hdu 6201 transaction transaction transaction(最长路)
- hdu 6201transaction transaction transaction
- HDU 6201 transaction transaction transaction
- hdu 6201 transaction transaction transaction
- HDU 6201 transaction transaction transaction
- hdu-6201 transaction transaction transaction
- hdu 6201 transaction transaction transaction
- transaction transaction transaction HDU
- transaction transaction transaction HDU
- hdu6201-搜索|最长路-transaction transaction transaction
- HDU-6201 transaction transaction transaction(树dp / 最长(短)路)
- Hdu 6201 transaction transaction transaction 树型DP
- HDU 6201 transaction transaction transaction [网络流]
- Android—将Bitmap图片保存到SD卡目录下或者指定目录
- java继承
- Gensim官方教程翻译(六)——分布式计算(Distributed Computing)
- activeMq发送消息变慢
- 定位
- Hdu 6201 transaction transaction transaction【最长路】
- 优化数学基础
- 修改数据库默认字符编码
- core dump
- Host XXX is not allowed to connect to this MySql 远程连接
- js 页面全屏,退出全屏, fullScreen
- Gensim官方教程翻译(七)——分布式潜在语义分析案例(Distributed Latent Semantic Analysis)
- memcached
- python各种模块的安装