博客
关于我
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/

    你可能感兴趣的文章
    Python | 爬虫实战——亚马逊搜索页监控(附详细源码)
    查看>>
    python | 高效使用Python工具自动生成模块文档的秘诀
    查看>>
    python 一个list去除另一个list中的值
    查看>>
    python 三大框架的 介绍。
    查看>>
    Python 下载的 11 种姿势,一种比一种高级!
    查看>>
    python读取一个文件夹下所有图片_初学Python-找出文件夹下的所有图片
    查看>>
    Python 中 3 个不可思议的返回功能
    查看>>
    python 中 dict 的另一种用法
    查看>>
    Python 中 PIL 读取图片出现异常旋转的解决方法
    查看>>
    python读取word表格内容(1)
    查看>>
    python 中os.path.join 双斜杠的解决办法
    查看>>
    python 中PIL.Image和OpenCV图像格式相互转换
    查看>>
    Python 中Semaphore 信号量对象、Event事件、Condition
    查看>>
    python 中with的使用及样例
    查看>>
    python读取wav文件并播放[pyaudio/wave]
    查看>>
    python读取txt文件的行数
    查看>>
    Python 中内置的最大堆 API
    查看>>
    Python 中只有一个 True 和一个 False 对象吗?
    查看>>
    python读取mtcars数据集并实现以下操作_关于数据处理。。,Python交流,技术交流区,鱼C论坛 - Powered by Discuz!...
    查看>>
    Python 中多线程与多处理之间的区别
    查看>>