transaction transaction transaction
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 239 Accepted Submission(s): 102
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 i t city. Kelukin will take taxi, whose price is 1 yuan 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.
As we know, the price of this book was different in each city. It is ai yuan in i t city. Kelukin will take taxi, whose price is 1 yuan 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 i th number means the prices in i th 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 z km (1≤z≤1000) .
For each test case:
first line contains an integer n ( 2≤n≤100000 ) means the number of cities;
second line contains n numbers, the i th number means the prices in i th 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 z km (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
题意:买卖书,一共有n个城市,每个城市书的价格不一样,从一个城市买入书再到另一个城市卖出(这些城市由n-1条边相连,且走每条边的花费不一样),求所能获得的最大收益。(只能买卖一次)
思路:书必定是从价格低处买入到价格高出卖出,而且途径的成市中,书的价格必定在高低之间,否则收益必定不是最高。所以可以把无向图变成有向图,从价格低处指向价格高处,又因为n-1条边连接了n个点,所以一定是个有向无环图。建图完成,接下来拓扑排序,从入度为0的点开始找,每次更新指向点的收益大小(一开始初始化为0),并把入度为1的指向点入队列,直到找不到入度为0的点为止。从所有点中找到收益最大的。
#include<stdio.h>
#include<iostream>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
const int maxn=100005;
int n;
int pri[maxn];
int res[maxn];
int ind[maxn];
struct node
{
int to;
int cost;
};
vector<node>q[maxn];
queue<int>g;
void solve()
{
for(int i=1;i<=n;i++)
{
if(!ind[i])g.push(i);
}
int x;
while(!g.empty())
{
x=g.front();
g.pop();
for(int i=0;i<q[x].size();i++)
{
res[q[x][i].to]=max(res[q[x][i].to],res[x]+pri[q[x][i].to]-pri[x]-q[x][i].cost);
ind[q[x][i].to]--;
if(!ind[q[x][i].to])g.push(q[x][i].to);
}
}
int ans=0;
for(int i=1;i<=n;i++)
{
if(res[i]>ans)ans=res[i];
}
printf("%d\n",ans);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
for(int i=0;i<maxn;i++)
{
q[i].clear();
}
while(!g.empty())g.pop();
memset(pri,0,sizeof(pri));
memset(ind,0,sizeof(ind));
memset(res,0,sizeof(res));
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&pri[i]);
}
int f,t,c;
node v;
for(int i=0;i<n-1;i++)
{
scanf("%d%d%d",&f,&t,&c);
if(pri[f]>=pri[t])swap(f,t);
v.to=t;
v.cost=c;
ind[t]++;
q[f].push_back(v);
}
solve();
}
}