博客
关于我
P1073 最优贸易(SPFA)
阅读量:394 次
发布时间:2019-03-05

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

重新优化后的内容

最优贸易问题分析

在这个问题中,我们需要找到一条路径,使得沿着路径购买和出售水晶球的差价最大化。路径中的点可以重复出现,这意味着我们需要考虑环路的可能性。

方法思路

为了解决这个问题,我们可以将其分解为两个部分:

  • 计算从起点到各点的最小购买成本:使用SPFA算法从起点(1号点)出发,计算每个点的最小购买成本(minn数组)。
  • 计算从终点到各点的最大销售收入:使用SPFA算法从终点(n号点)出发,计算每个点的最大销售收入(maxn数组)。
  • 最后,我们遍历从2号点到n-1号点的所有点,计算每个点的销售收入减去购买成本的差值,找出最大的差值作为结果。

    详细步骤

  • 建立邻接表

    • 使用两个邻接表a1a2分别表示图的边。a1用于计算最小购买成本,a2用于计算最大销售收入。
    • add1add2函数用于添加边到邻接表中。
  • SPFA算法实现

    • spfa1(int o):从起点o出发,计算最小购买成本数组minn
    • spfa2(int o):从终点o出发,计算最大销售收入数组maxn
  • 计算结果

    • 初始化minn数组为大值,使用spfa1计算最小购买成本。
    • 初始化maxn数组为起点值,使用spfa2计算最大销售收入。
    • 遍历从2号点到n-1号点,计算最大差值maxn[i] - minn[i]
  • 代码实现

    #include 
    #include
    using namespace std;#define N 1000005#define M 5000005struct stu { int to; int next; int w;};stu a1[M], a2[M];int head1[M], head2[M];int b[M], c[N];int w[N];int minn[N], maxn[N];int tot1, tot2, head, tail;void add1(int x, int y) { tot1++; a1[tot1].to = y; a1[tot1].next = head1[x]; head1[x] = tot1;}void add2(int x, int y) { tot2++; a2[tot2].to = y; a2[tot2].next = head2[x]; head2[x] = tot2;}void spfa1(int o) { minn[o] = w[o]; head = 0; tail = 1; b[1] = o; c[o] = 1; do { head = (head % N) + 1; int k = b[head]; for (int i = head1[k]; i < M; i++) { if (minn[a1[i].to] > min(minn[k], w[a1[i].to])) { minn[a1[i].to] = min(minn[k], w[a1[i].to]); if (c[a1[i].to] == 0) { tail++; b[tail] = a1[i].to; c[a1[i].to] = 1; } } } c[k] = 0; } while (head < tail);}void spfa2(int o) { maxn[o] = w[o]; head = 0; tail = 1; b[1] = o; c[o] = 1; do { head = (head % N) + 1; int k = b[head]; for (int i = head2[k]; i < M; i++) { if (maxn[a2[i].to] < max(maxn[k], w[a2[i].to])) { maxn[a2[i].to] = max(maxn[k], w[a2[i].to]); if (c[a2[i].to] == 0) { tail++; b[tail] = a2[i].to; c[a2[i].to] = 1; } } } c[k] = 0; } while (head < tail);}int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> w[i]; minn[i] = 2147483647; } for (int i = 1; i <= m; i++) { int x, y, z; cin >> x >> y >> z; add1(x, y); add2(y, x); if (z == 2) { add1(y, x); add2(x, y); } } spfa1(1); for (int i = 1; i <= n; i++) { c[i] = 0; } spfa2(n); int m_max = 0; for (int i = 2; i <= n - 1; i++) { m_max = max(m_max, maxn[i] - minn[i]); } cout << m_max << endl;}

    代码解释

  • 数据结构:使用邻接表来存储图的边信息,分别为a1a2,分别用于计算最小购买成本和最大销售收入。
  • SPFA算法:实现了两个SPFA算法,一次从起点出发,计算最小购买成本;另一次从终点出发,计算最大销售收入。算法使用队列来处理节点,确保松弛操作的正确性。
  • 结果计算:遍历中间点,计算每个点的销售收入减去购买成本的差值,找出最大的差值作为最优解。
  • 通过这种方法,我们能够高效地解决最优贸易问题,找到最大的利润路径。

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

    你可能感兴趣的文章
    oracle 批量生成建同义词语句和付权语句
    查看>>
    oracle 抓包工具,shell 安装oracle和pfring(抓包) 及自动环境配置
    查看>>
    Oracle 拆分以逗号分隔的字符串为多行数据
    查看>>
    Oracle 排序中使用nulls first 或者nulls last 语法
    查看>>
    oracle 插入date日期类型的数据、插入从表中查出的数据,使用表中的默认数据
    查看>>
    Oracle 操作笔记
    查看>>
    oracle 数据库 安装 和优化
    查看>>
    oracle 数据库dg搭建规范1
    查看>>
    Oracle 数据库常用SQL语句(1)
    查看>>
    Oracle 数据库特殊查询总结
    查看>>
    Oracle 数据类型
    查看>>
    oracle 数据迁移 怎么保证 和原表的数据顺序一致_一个比传统数据库快 1001000 倍的数据库,来看一看?...
    查看>>
    oracle 时间函数
    查看>>
    oracle 时间转化函数及常见函数 .
    查看>>
    Oracle 权限(grant、revoke)
    查看>>
    oracle 查询clob
    查看>>
    Oracle 比较 B-tree 和 Bitmap 索引
    查看>>
    Oracle 注意点大全
    查看>>
    UML- 组件图(构件图)
    查看>>
    oracle 用户与锁
    查看>>