博客
关于我
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读excel
    查看>>
    Python 中读取 CSV 文件-ChatGPT4o作答
    查看>>
    Python 之 filecmp
    查看>>
    python请求html_使用Python请求获取HTML?
    查看>>
    Python 之匿名函数和偏函数
    查看>>
    python 之栈的实现
    查看>>
    python语音播放
    查看>>
    python语言:装饰器原理
    查看>>
    Python 交互式数据可视化详解
    查看>>
    python语言有哪些优点和缺点_Python有哪些优缺点,你了解吗?
    查看>>
    Python 从入门到精通:30天速成教程到底有多狠?你能坚持下来吗?
    查看>>
    Python 从数据库中存储和检索密码的最安全方法
    查看>>
    Python语言及其应用 - 知识点遍历
    查看>>
    Python 优化提速的 8 个小技巧
    查看>>
    Python 余弦相似度与皮尔逊相关系数 计算
    查看>>
    python 使用execjs 报编码错误解决办法,UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xac in position 145: il
    查看>>
    python 使用filetype校验文件
    查看>>
    Python 使用flush函数将缓冲区数据立即写磁盘
    查看>>
    python 使用in判断不准确,in不好使
    查看>>