题目描述
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 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.
输入
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 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).
输出
For each test case, output a single number in a line: the maximum money he can get.
样例输入
1
4
10 40 15 30
1 2 30
1 3 2
3 4 10
样例输出
8
思路
树形DP
设dp[i][0]为在点i买书的消耗,dp[i][0]为在点i卖书的获得,每次向下递推状态转移公式为
dp[u][0]=max(dp[u][0],dp[v][0]-w);
dp[u][1]=max(dp[u][1],dp[v][1]-w);
最后所得即为ans=max(ans,dp[u][0]+dp[u][1]);
代码实现
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
const int N=100005;
struct node
{
int v,w;
node(int _v=0,int _w=0):v(_v),w(_w) {}
};
int T,n;
int dp[N][2];
vector<node>E[N];
int val[N];
int ans;
void addedge(int u,int v,int w)
{
E[u].push_back(node(v,w));
E[v].push_back(node(u,w));
}
void dfs(int u,int fa)
{
dp[u][0]=-val[u];
dp[u][1]=val[u];
for(int i=0;i<E[u].size();i++)
{
int v=E[u][i].v,w=E[u][i].w;
if(v==fa) continue;
dfs(v,u);
dp[u][0]=max(dp[u][0],dp[v][0]-w);
dp[u][1]=max(dp[u][1],dp[v][1]-w);
}
ans=max(ans,dp[u][0]+dp[u][1]);
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++) E[i].clear();
for(int i=1;i<=n;i++) scanf("%d",&val[i]);
for(int i=1;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
}
ans=0;
dfs(1,-1);
printf("%d\n",ans);
}
return 0;
}
SPFA最长路
建立一个超级原点,一个超级汇点,对于每个点,从该点到超级原点的边权为val[i],到超级汇点的边权为-val[i],对于一条由超级原点到超级汇点的路,这条路的总权值即为在这条路上所有城市的获利,所以由超级原点到超级汇点的最长路即为最大值,因存在负权,因此使用SPFA
代码实现
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100005;
const int INF=0x3f3f3f3f;
typedef pair<int,int> P;
struct node
{
int w,nxt,to;
}E[N<<2];
int a[N];
int head[N],cnt,n;
bool vis[N];
int dist[N];
void add(int u,int v,int w)
{
E[++cnt].to=v;
E[cnt].nxt=head[u];
E[cnt].w=w;
head[u]=cnt;
}
void spfa(int s)
{
memset(dist,0,sizeof(dist));
memset(vis,false,sizeof(vis));
vis[s]=true;
dist[s]=0;
queue<int>que;
que.push(s);
while(!que.empty())
{
int u=que.front();
que.pop();
vis[u]=false;
for(int i=head[u];i;i=E[i].nxt)
{
int v=E[i].to;
if(dist[v]<dist[u]+E[i].w)
{
dist[v]=dist[u]+E[i].w;
if(!vis[v])
{
vis[v]=true;
que.push(v);
}
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(head,0,sizeof(head));
cnt=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
add(0,i,a[i]);
add(i,n+1,-a[i]);
}
for(int i=1;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,-w);
add(v,u,-w);
}
spfa(0);
printf("%d\n",dist[n+1]);
}
return 0;
}