POJ 3278:The merchant(LCA&DP)
The merchant
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 6864 | Accepted: 2375 |
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 ≤ N, wi, Q ≤ 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
题意
给出一棵树,每个节点有一个点权,代表商品的价值
从节点u->v的路径上,有一个商人从一个节点购买商品,并在另一个节点卖出,问商人可以获得的最大利润是多少
思路
商人进行买卖一共有三种情况:
- 在点u->lca(u,v)的路上买,lca(u,v)->v的路上卖
- 在u->lca(u,v)的路上完成买卖
- 在lca(u,v)->v的路上完成买卖
所以我们可以引入四个数组:up,down,maxx,minn。分别代表:在u->lca(u,v)的路上完成买卖的最大获利;在lca(u,v)->v的路上完成买卖的最大获利;lca(u,v)->v点出售的最大利润;u->lca(u,v)购买的最少花费
我们可以得到商人的最大收益=max(max(up[u],down[v]),maxx[v]-minn[u])
问题就变成了去寻找lca(u,v),并更新数组的值。以上四个数组的值可以在并查集find函数中进行更新
代码
1 #include <iostream>
2 #include <algorithm>
3 #include <cstring>
4 #define ll long long
5 #define ull unsigned long long
6 #define ms(a,b) memset(a,b,sizeof(a))
7 const int inf=0x3f3f3f3f;
8 const ll INF=0x3f3f3f3f3f3f3f3f;
9 const int maxn=1e6+10;
10 const int mod=1e9+7;
11 const int maxm=1e3+10;
12 using namespace std;
13 int maxx[maxn],minn[maxn];
14 int up[maxn],down[maxn];
15 int f[maxn];
16 // minn[u] u->lca(u,v)买的最小值
17 // maxx[v] lca(u,v)->v卖的最大值
18 // up[u] u->lca(u,v)进行买卖的最大值
19 // down[v] lca(u,v)->v进行买卖的最大值
20 int find(int x)
21 {
22 if(f[x]==x)
23 return x;
24 int y=f[x];
25 f[x]=find(f[x]);
26 up[x]=max(max(up[y],up[x]),maxx[y]-minn[x]);
27 down[x]=max(max(down[y],down[x]),maxx[x]-minn[y]);
28 maxx[x]=max(maxx[x],maxx[y]);
29 minn[x]=min(minn[x],minn[y]);
30 return f[x];
31 }
32 void join(int x,int y)
33 {
34 int dx=f[x],dy=f[y];
35 if(dx!=dy)
36 f[y]=dx;
37 }
38 struct Edge
39 {
40 int to,Next;
41 }edge[maxn];
42 int head1[maxn];
43 int tot1;
44 void add_edge(int u,int v)
45 {
46 edge[tot1].to=v;
47 edge[tot1].Next=head1[u];
48 head1[u]=tot1++;
49 }
50 struct Query
51 {
52 int to,Next;
53 int index;
54 }query[maxn];
55 int head2[maxn];
56 int tot2;
57 void add_query(int u,int v,int index)
58 {
59 query[tot2].to=v;
60 query[tot2].Next=head2[u];
61 query[tot2].index=index;
62 head2[u]=tot2++;
63 }
64 struct Pos
65 {
66 int to,Next;
67 int index;
68 }pos[maxn];
69 int head3[maxn];
70 int tot3;
71 void add_pos(int u,int v,int index)
72 {
73 pos[tot3].to=v;
74 pos[tot3].Next=head3[u];
75 pos[tot3].index=index;
76 head3[u]=tot3++;
77 }
78 int vis[maxn];
79 int qu[maxn],qv[maxn];
80 int fa[maxn];
81 int ans[maxn];
82 void LCA(int u)
83 {
84 fa[u]=u;
85 vis[u]=1;
86 for(int i=head1[u];~i;i=edge[i].Next)
87 {
88 int v=edge[i].to;
89 if(vis[v])
90 continue;
91 LCA(v);
92 join(u,v);
93 fa[find(u)]=u;
94 }
95 for(int i=head2[u];~i;i=query[i].Next)
96 {
97 int v=query[i].to;
98 if(vis[v])
99 {
100 int t=fa[find(v)];
101 // 储存lca(u,v)的子节点
102 add_pos(t,v,query[i].index);
103 }
104 }
105 for(int i=head3[u];~i;i=pos[i].Next)
106 {
107 int id=pos[i].index;
108 int xx=qu[id],yy=qv[id];
109 // 更新四个数组的值
110 find(xx);find(yy);
111 ans[id]=max(max(up[xx],down[yy]),maxx[yy]-minn[xx]);
112 }
113 }
114 int value[maxn];
115 void init(int n)
116 {
117 tot1=0;tot2=0;tot3=0;
118 for(int i=1;i<=n;i++)
119 f[i]=i;
120 ms(fa,0);ms(vis,0);
121 ms(head1,-1);ms(head2,-1);ms(head3,-1);
122 }
123 int main(int argc, char const *argv[])
124 {
125 #ifndef ONLINE_JUDGE
126 freopen("/home/wzy/in.txt", "r", stdin);
127 freopen("/home/wzy/out.txt", "w", stdout);
128 srand((unsigned int)time(NULL));
129 #endif
130 ios::sync_with_stdio(false);
131 cin.tie(0);
132 int n;
133 cin>>n;
134 init(n);
135 for(int i=1;i<=n;i++)
136 cin>>value[i],minn[i]=value[i],maxx[i]=value[i];
137 int u,v;
138 for(int i=1;i<n;i++)
139 cin>>u>>v,add_edge(u,v),add_edge(v,u);
140 int q;
141 cin>>q;
142 for(int i=0;i<q;i++)
143 cin>>qu[i]>>qv[i],add_query(qu[i],qv[i],i),add_query(qv[i],qu[i],i);
144 LCA(1);
145 for(int i=0;i<q;i++)
146 cout<<ans[i]<<"\n";
147 #ifndef ONLINE_JUDGE
148 cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
149 #endif
150 return 0;
151 }
POJ 3278:The merchant(LCA&DP)的更多相关文章
- POJ 3728 The merchant(LCA+DP)
The merchant Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- POJ 1651:Multiplication Puzzle(区间DP)
http://poj.org/problem?id=1651 题意:给出n个数字,每取中间一个数,就会使得权值加上中间这个数和两边的乘积,求取剩两个数最少的权值是多少. 思路:区间dp. 一开始想了挺 ...
- CodeForces - 1073E :Segment Sum (数位DP)
You are given two integers l l and r r (l≤r l≤r ). Your task is to calculate the sum of numbers from ...
- POJ 2796:Feel Good(单调栈)
http://poj.org/problem?id=2796 题意:给出n个数,问一个区间里面最小的元素*这个区间元素的和的最大值是多少. 思路:只想到了O(n^2)的做法. 参考了http://ww ...
- POJ 3318:Matrix Multiplication(随机算法)
http://poj.org/problem?id=3318 题意:问A和B两个矩阵相乘能否等于C. 思路:题目明确说出(n^3)的算法不能过,但是通过各种常数优化还是能过的. 这里的随机算法指的是随 ...
- SPOJ:House Fence(分治&DP)
"Holiday is coming, holiday is coming, hurray hurray!" shouts Joke in the last day of his ...
- BZOJ 1026:windy数(数位DP)
http://www.lydsy.com/JudgeOnline/problem.php?id=1026 1026: [SCOI2009]windy数 Time Limit: 1 Sec Memor ...
- POJ 1200:Crazy Search(哈希)
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32483 Accepted: 8947 Des ...
- poj 2533 Longest Ordered Subsequence(线性dp)
题目链接:http://poj.org/problem?id=2533 思路分析:该问题为经典的最长递增子序列问题,使用动态规划就可以解决: 1)状态定义:假设序列为A[0, 1, .., n],则定 ...
随机推荐
- 学习Java的第十八天
一.今日收获 1.java完全学习手册第三章算法的3.1比较值 2.看哔哩哔哩上的教学视频 二.今日问题 1.在第一个最大值程序运行时经常报错. 2.哔哩哔哩教学视频的一些术语不太理解,还需要了解 三 ...
- A Child's History of England.2
They made boats of basket-work, covered with the skins of animals, but seldom, if ever, ventured far ...
- CSS系列,三栏布局的四种方法
三栏布局.两栏布局都是我们在平时项目里经常使用的,今天我们来玩一下三栏布局的四种写法,以及它的使用场景. 所谓三栏布局就是指页面分为左中右三部分然后对中间一部分做自适应的一种布局方式. 1.绝对定位法 ...
- [学习总结]9、Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
这个图片异步加载并缓存的类已经被很多开发者所使用,是最常用的几个开源库之一,主流的应用,随便反编译几个火的项目,都可以见到它的身影. 可是有的人并不知道如何去使用这库如何进行配置,网上查到的信息对于刚 ...
- OpenStack之七: compute服务(端口8774)
注意此处的bug,参考o版 官网地址 https://docs.openstack.org/nova/stein/install/controller-install-rdo.html 控制端配置 # ...
- shell脚本下载网页图片
和大家分享一个shell脚本写的图片抓取器.使用方法:img_downloader.sh.使用时在shell下输入:./img_downloader.sh www.baidu.com -d image ...
- ASP.NET管道模型简析
我相信在第一次听到这个名词时,有的小伙伴会一脸懵,而且还有很多疑问,其实我在第一次接触这个概念时跟很多小伙伴一样一脸懵. 接下来我将以我自己的理解来讲述什么是管道模型. 什么是管道模型 首先有没有小伙 ...
- Wireshark(二):应用Wireshark观察基本网络协议
原文出处: EMC中文支持论坛 TCP: TCP/IP通过三次握手建立一个连接.这一过程中的三种报文是:SYN,SYN/ACK,ACK. 第一步是找到PC发送到网络服务器的第一个SYN报文,这标识了T ...
- [BUUCTF]PWN——[BJDCTF 2nd]secret
[BJDCTF 2nd]secret 附件 步骤: 例行检查,64位程序,开启了canary和nx 本地试运行一下,看看程序大概的情况,好像是一个什么游戏 64位ida载入,检索程序里的字符串,发现了 ...
- 记录一次成功CICD完整亲身实践从此踏进入Devops大门
Devops概念 DevOps 强调通过一系列手段来实现既快又稳的工作流程,使每个想法(比如一个新的软件功能,一个功能增强请求或者一个 bug 修复)在从开发到生产环境部署的整个流程中,都能不断地为用 ...