poj 3728 The merchant(离线LCA 的应用,5级)

路径最大收益计算算法实现
本文介绍了一种解决路径最大收益问题的算法实现方法,包括输入解析、数据结构设计、离线查询处理以及最终结果输出。算法通过离线查询优化路径收益计算,适用于大规模城市网络的收益最大化问题。
The merchant
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 2399 Accepted: 790

Description

There are N cities in a country, and there is one and only one simple path between each pair of cities. A merchant has chosen some paths and wants to earn as much money as possible in each path. When he move along a path, he can choose one city to buy some goods and sell them in a city after it. The goods in all cities are the same but the prices are different. Now your task is to calculate the maximum possible profit on each path.

Input

The first line contains N, the number of cities.
Each of the next N lines contains wi the goods' price in each city.
Each of the next N-1 lines contains labels of two cities, describing a road between the two cities.
The next line contains Q, the number of paths.
Each of the next Q lines contains labels of two cities, describing a path. The cities are numbered from 1 to N.

1 ≤ NwiQ ≤ 50000 

Output

The output contains Q lines, each contains the maximum profit of the corresponding path. If no positive profit can be earned, output 0 instead.

Sample Input

4
1 
5 
3 
2
1 3
3 2
3 4
9
1 2
1 3
1 4
2 3
2 1
2 4
3 1
3 2
3 4

Sample Output

4
2
2
0
0
0
0
2
0

Source


思路:可以使用离线,离线可以计算出查询的一个点到祖先的最优值,然后记录结果,等整个该祖先的子树都遍历完,就可以知道                祖先到达查询的另一点的最优值,两者合并就是查询结果。
     知道以上,就比较容易了,设ix[x],ax[x]分别为x到祖先的最小最大值,up[x],dw[x]分别为x到祖先的正向最优,反向最优。
    ans=MAX(ax[x]-ix[y],up[y],dw[x] )
最早题意理解有误,认为是,可以多次买,卖,的最优值,结果居然是只能一次。哎。
多次买卖的话,直接一个在线LCA就可以轻松搞定了。
#include<iostream>
#include<cstring>
#include<cstdio>
#define ll(x) (1<<x)
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mm=7e5+9;
int head[mm],edge,val[mm],qh[mm],N;
int rt[mm],ax[mm],ix[mm],ans[mm],up[mm],dw[mm],lca[mm];
bool vis[mm];
class Edge
{
  public:int u,v,next,w;
}e[mm];
void data()
{ clr(qh,-1);clr(lca,-1);
  clr(head,-1);edge=0;
}
void add(int u,int v,int w,int*head)
{ e[edge].w=w;e[edge].u=u;
  e[edge].v=v;e[edge].next=head[u];head[u]=edge++;
}
int find(int x)
{ int y;
  if(x!=rt[x])
  {
    y=rt[x];
    rt[x]=find(rt[x]);///从根开始到x的正向最优,反向最优
    up[x]=max(ax[y]-ix[x],max(up[x],up[y]));
    dw[x]=max(ax[x]-ix[y],max(dw[x],dw[y]));
    ax[x]=max(ax[x],ax[y]);
    ix[x]=min(ix[x],ix[y]);
  }
  return rt[x];
}
void dfs(int u)
{ int v;
  rt[u]=u;
  vis[u]=1;
  for(int i=head[u];~i;i=e[i].next)
  { v=e[i].v;
    if(!vis[v])
    {
      dfs(v);
      rt[v]=u;
    }
  }

  for(int i=qh[u];~i;i=e[i].next)
  {
    v=e[i].v;
    if(vis[v])
    {
      int t=find(v);///找到最近公共祖先,更新祖先到达v的正向最优和反向最优
      add(t,v,i,lca);///不过祖先到大u的正反向最优没更新,也无法更新故现在没法计算值
    }
  }
  for(int i=lca[u];~i;i=e[i].next)///只更新以u为最近祖先的查询,因为所有u的子树都遍历完了
  {
    int ki=e[i].w;
    int uu,vv,dd;
    uu=e[ki].u;vv=e[ki].v;
    dd=e[ki].w;
    find(uu);///vv已经更新过了,但uu没更新
    if(dd<0)//反向了
    {
      uu^=vv;vv^=uu;uu^=vv;
      dd=-dd;
    }
    ans[dd]=max(max(up[uu],dw[vv]),ax[vv]-ix[uu]);
  }
}
void find_bcc()
{ clr(vis,0);
  dfs(1);
}
int main()
{ int a,b,Q;
  while(~scanf("%d",&N))
  { data();
    FOR(i,1,N)scanf("%d",&ix[i]),ax[i]=ix[i],up[i]=dw[i]=0;
    FOR(i,2,N)
    scanf("%d%d",&a,&b),add(a,b,1,head),add(b,a,1,head);
    scanf("%d",&Q);
    FOR(i,1,Q)
    {
      scanf("%d%d",&a,&b);
      add(a,b,i,qh);
      add(b,a,-i,qh);
    }
    find_bcc();
    FOR(i,1,Q)
    printf("%d\n",ans[i]);
  }
}


转载于:https://www.cnblogs.com/nealgavin/p/3797583.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值