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

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

为了解决这个问题,我们需要找到树中的两个不同的点S和T,使得表达式a(T) - a(S) - dist(S, T)的值最大化。树的结构和权值特性使得这个问题可以通过构建有向图来解决。

方法思路

  • 构建有向图:创建一个源点0和一个汇点n+1。将每个节点i连接到源点0,边权为 -a(i),并将每个节点i连接到汇点n+1,边权为a(i)。同时,将原树结构中的每条边转换为两条有向边,权重为负数。
  • 计算最短路径:使用SPFA算法计算源点0到汇点n+1的最短路径。这个路径的总长度即为目标函数的最大值。
  • 解决代码

    import sysfrom collections import dequedef main():    sys.setrecursionlimit(1 << 25)    n = int(sys.stdin.readline())    a = [0] * (n + 2)    for i in range(1, n+1):        a[i] = int(sys.stdin.readline())        maxm = 1e5 + 5    head = [0] * (maxm + 2)    nt = [0] * (maxm + 2)    to = [0] * (maxm + 2)    w = [0] * (maxm + 2)    tot = 0    def add(x, y, z):        nonlocal tot        tot += 1        head[x] = tot        nt[tot] = y        to[tot] = y        w[tot] = z        def spfa(start):        q = deque()        q.append(start)        d = [-float('inf')] * (maxm + 2)        d[start] = 0        in_queue = [False] * (maxm + 2)        in_queue[start] = True        while q:            u = q.popleft()            in_queue[u] = False            for i in range(head[u], -1, -1):                v = to[i]                if d[v] > d[u] + w[i]:                    d[v] = d[u] + w[i]                    if not in_queue[v]:                        q.append(v)                        in_queue[v] = True        return d        add(0, 0, 0)    for i in range(1, n+1):        add(0, i, -a[i])        add(n+1, n+1, 0)    for i in range(1, n+1):        add(i, n+1, a[i])        for i in range(1, n+1):        u = nt[i]        v = to[i]        if u != -1 and v != -1:            add(u, v, -1)            add(v, u, -1)        d = spfa(0)    print(d[n+1])if __name__ == '__main__':    main()

    代码解释

  • 读取输入:读取节点数n和每个节点的权值a(i)。
  • 构建有向图:将每个节点连接到源点0和汇点n+1,并处理原树结构中的每条边。
  • SPFA算法:计算源点0到汇点n+1的最短路径,更新每个节点的最短距离。
  • 输出结果:输出源点0到汇点n+1的最短路径长度,即目标函数的最大值。
  • 通过这种方法,我们能够高效地解决问题,适用于大规模数据。

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

    你可能感兴趣的文章
    npm发布自己的组件UI包(详细步骤,图文并茂)
    查看>>
    npm和yarn清理缓存命令
    查看>>
    npm和yarn的使用对比
    查看>>
    npm如何清空缓存并重新打包?
    查看>>
    npm学习(十一)之package-lock.json
    查看>>
    npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
    查看>>
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm安装教程
    查看>>
    npm报错Cannot find module ‘webpack‘ Require stack
    查看>>
    npm报错Failed at the node-sass@4.14.1 postinstall script
    查看>>
    npm报错fatal: Could not read from remote repository
    查看>>
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    npm编译报错You may need an additional loader to handle the result of these loaders
    查看>>
    npm设置淘宝镜像、升级等
    查看>>