博客
关于我
hdu6201 transaction transaction transaction(新建源汇点,带负权最长路)
阅读量:250 次
发布时间:2019-03-01

本文共 799 字,大约阅读时间需要 2 分钟。

题意:

给定n个点的树,每个点有一个权值a(i),

要求找到两个不同的点S和T,满足a(T)-a(S)-dist(S,T)最大,dist(x,y)是点x和y的树上距离。
输出最大值。

数据范围:n<=1e5

解法:

题目给的是树,很容易往树形dp想,但是树形dp应该很难写,挺坑的.建立源点0,向n个点建立有向边,边权为-a[i],建立汇点n+1,n个点都向它有向边,边权为a[i],令原图的双向边边权取反变为负数,那么点0到点n+1的最短路就是式子的答案.

code:

#include
using namespace std;const int maxm=1e5+5;int head[maxm],nt[maxm<<2],to[maxm<<2],w[maxm<<2],tot;int mark[maxm];int d[maxm];int a[maxm];int n;void add(int x,int y,int z){ tot++;nt[tot]=head[x];head[x]=tot;to[tot]=y;w[tot]=z;}void spfa(int st){ queue
q; q.push(st); for(int i=1;i<=n+1;i++){ d[i]=-1e9; mark[i]=0; } d[st]=0; mark[st]=1; while(!q.empty()){ int x=q.front();q.pop(); mark[x]=0; for(int i=head[x];i!=-1;i=nt[i]){ int v=to[i]; if(d[v]

转载地址:http://wdkv.baihongyu.com/

你可能感兴趣的文章
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>