D. The Fair Nut and the Best Path 树形dp (终于会了)
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=3e5+;
int a[maxn];
int dp[maxn];
int visit[maxn];
vector<int> vs[maxn];
map<pair<int,int>,int> mp;
int ans=;
void dfs(int x)
{
visit[x]=;
int max1=;
int max2=;
for(int i=;i<vs[x].size();i++)
{ int p=vs[x][i];
if(visit[p]) continue;//cout<<p<<endl;
dfs(p);
int k=dp[p]-mp[{x,p}];
if(k>=max1) max2=max1,max1=k;
else if(k>=max2) max2=k;
}
//cout<<x<<" "<<dp[x]<<endl;
dp[x]=a[x]+max1;
ans=max(ans,dp[x]);
ans=max(ans,max1+max2+a[x]);
}
int32_t main()
{
int n; cin>>n;
for(int i=;i<=n;i++) cin>>a[i];
for(int i=;i<=n;i++) dp[i]=a[i];
for(int i=;i<n;i++)
{
int x,y,z; cin>>x>>y>>z;
vs[x].push_back(y); mp[{x,y}]=z;
vs[y].push_back(x); mp[{y,x}]=z;
}
dfs();
cout<<ans<<endl;
return ;
}
2994ms
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=3e5+;
int a[maxn];
int dp[maxn];
int visit[maxn];
vector<pair<int,int> > vs[maxn];
int ans=;
void dfs(int x)
{
visit[x]=;
int max1=;
int max2=;
for(int i=;i<vs[x].size();i++)
{ int p=vs[x][i].first;
if(visit[p]) continue;//cout<<p<<endl;
dfs(p);
int k=dp[p]-vs[x][i].second;
if(k>=max1) max2=max1,max1=k;
else if(k>=max2) max2=k;
}
//cout<<x<<" "<<dp[x]<<endl;
dp[x]=a[x]+max1;
ans=max(ans,dp[x]);
ans=max(ans,max1+max2+a[x]);
}
int32_t main()
{
int n; cin>>n;
for(int i=;i<=n;i++) cin>>a[i];
for(int i=;i<=n;i++) dp[i]=a[i];
for(int i=;i<n;i++)
{
int x,y,z; cin>>x>>y>>z;
vs[x].push_back({y,z}); //mp[{x,y}]=z;
vs[y].push_back({x,z}); //mp[{y,x}]=z;
}
dfs();
cout<<ans<<endl;
return ;
}
2308ms
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=3e5+;
int a[maxn];
int dp[maxn];
int visit[maxn];
vector<pair<int,int> > vs[maxn];
int ans=;
void dfs(int x)
{
visit[x]=;
int max1=;
int max2=;
for(int i=;i<vs[x].size();i++)
{ int p=vs[x][i].first;
if(visit[p]) continue;//cout<<p<<endl;
dfs(p);
int k=dp[p]-vs[x][i].second;
if(k>=max1) max2=max1,max1=k;
else if(k>=max2) max2=k;
}
//cout<<x<<" "<<dp[x]<<endl;
dp[x]=a[x]+max1;
ans=max(ans,dp[x]);
ans=max(ans,max1+max2+a[x]);
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n; cin>>n;
for(int i=;i<=n;i++) cin>>a[i];
for(int i=;i<=n;i++) dp[i]=a[i];
for(int i=;i<n;i++)
{
int x,y,z; cin>>x>>y>>z;
vs[x].push_back({y,z}); //mp[{x,y}]=z;
vs[y].push_back({x,z}); //mp[{y,x}]=z;
}
dfs();
cout<<ans<<endl;
return ;
}
700+ms
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=3e5+;
int a[maxn];
int dp[maxn];
int visit[maxn];
vector<pair<int,int> > vs[maxn];
int ans=;
void dfs(int x)
{
visit[x]=;
int max1=;
int max2=;
for(int i=;i<vs[x].size();i++)
{ int p=vs[x][i].first;
if(visit[p]) continue;//cout<<p<<endl;
dfs(p);
int k=dp[p]-vs[x][i].second;
if(k>=max1) max2=max1,max1=k;
else if(k>=max2) max2=k;
}
//cout<<x<<" "<<dp[x]<<endl;
dp[x]=a[x]+max1;
ans=max(ans,dp[x]);
ans=max(ans,max1+max2+a[x]);
}
int32_t main()
{
/*ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);*/
int n; scanf("%I64d",&n);
for(int i=;i<=n;i++) scanf("%I64d",&a[i]);
for(int i=;i<=n;i++) dp[i]=a[i];
for(int i=;i<n;i++)
{
int x,y,z; scanf("%I64d %I64d %I64d",&x,&y,&z);
vs[x].push_back({y,z}); //mp[{x,y}]=z;
vs[y].push_back({x,z}); //mp[{y,x}]=z;
}
dfs();
printf("%I64d\n",ans);
return ;
}
405ms
D. The Fair Nut and the Best Path 树形dp (终于会了)的更多相关文章
- Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp
D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...
- CF 1083 A. The Fair Nut and the Best Path
A. The Fair Nut and the Best Path https://codeforces.com/contest/1083/problem/A 题意: 在一棵树内找一条路径,使得从起点 ...
- CF1083A The Fair Nut and the Best Path
CF1083A The Fair Nut and the Best Path 先把边权搞成点权(其实也可以不用),那么就是询问树上路径的最大权值. 任意时刻权值非负的限制可以不用管,因为若走路径 \( ...
- Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path
D. The Fair Nut and the Best Path 题目链接:https://codeforces.com/contest/1084/problem/D 题意: 给出一棵树,走不重复的 ...
- CodeForces 1084D The Fair Nut and the Best Path
The Fair Nut and the Best Path 题意:求路径上的 点权和 - 边权和 最大, 然后不能存在某个点为负数. 题解: dfs一遍, 求所有儿子走到这个点的最大值和次大值. 我 ...
- 【Codeforces 1083A】The Fair Nut and the Best Path
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 我们最后要的是一条最长的路径. 这条路径的权值和是所有点的权值和-所有边的权值和且这个值最大. 显然如果我们在某一条边上的累计的权值和< ...
- Codeforces Round #526 D - The Fair Nut and the Best Path /// 树上两点间路径花费
题目大意: 给定一棵树 树上每个点有对应的点权 树上每条边有对应的边权 经过一个点可得到点权 经过一条边必须花费边权 即从u到v 最终得分=u的点权-u到v的边权+v的点权 求树上一条路径使得得分最大 ...
- CF1083E The Fair Nut and Rectangles
CF1083E The Fair Nut and Rectangles 给定 \(n\) 个平面直角坐标系中左下角为坐标原点,右上角为 \((x_i,\ y_i)\) 的互不包含的矩形,每一个矩形拥有 ...
- CF 1083 B. The Fair Nut and Strings
B. The Fair Nut and Strings 题目链接 题意: 在给定的字符串a和字符串b中找到最多k个字符串,使得不同的前缀字符串的数量最多. 分析: 建出trie树,给定的两个字符串就 ...
随机推荐
- laravel创建资源路由控制器
php artisan make:controller PhotoController --resource
- learning scala read from file
scala读文件: example: scala> import scala.io.Sourceimport scala.io.Source scala> var inputFile ...
- css3 居中(推荐弹性盒模型方式)
参考 http://www.zhihu.com/question/20774514 http://caibaojian.com/demo/flexbox/align-items.html 例子:ht ...
- unity中键盘WASD控制。(WS控制物体前后移动,AD控制左右旋转。)
private float rotateSpeed = 30f; private float movespeed = 5; void FixedUpdate() { //第一种控制移动 float h ...
- RegDataToDataType
function RegDataToDataType(Value: TRegDataType): Integer; begin case Value of rdString: Result := RE ...
- Linux 目录配置标准:FHS
目录 应放置内容 /bin 和/user/目录下的/bin/都是用来保存的系统命令 /sbin 和/user/目录下的/sbin是用来保存root的系统命令 /boot 这个目录主要放置开机所用的文件 ...
- node(2) EventEmitter类 事件队列 事件和error事件方法
事件队列的核心:事件触发与事件监听器功能的封装. // 引入 events 模块 var events = require('events'); // 创建 eventEmitter 对象 var e ...
- Centos7部署kubernetes-ETCD集群(三)
1.下载etcd软件包 wget https://github.com/coreos/etcd/releases/download/v3.2.18/etcd-v3.2.18-linux-amd64.t ...
- Java类库和常用类库介绍
Java 类库概念: Java 的应用程序接口 (API) 以包的形式来组织,每个包提供了大量的相关类.接口和异常处理类,这些包的集合就是 Java 的类库 包名以 Java 开始的包是 Java 核 ...
- poj1742(多重背包分解+01背包二进制优化)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...