hdu6201(拓扑+dp)

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  n1  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  ( 1T10 ) , the number of test cases. 
For each test case:
first line contains an integer  n  ( 2n100000 ) means the number of cities;
second line contains  n  numbers, the  i th  number means the prices in  i th  city;  (1Price10000)  
then follows  n1  lines, each contains three numbers  x y  and  z  which means there exists a road between  x  and  y , the distance is  z km   (1z1000)
 

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();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值