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

    你可能感兴趣的文章
    Please install 'webpack-cli' in addition to webpack itself to use the CLI
    查看>>
    Ploly Dash,更新一个Dash应用程序JJJA上的实时人物
    查看>>
    Ploly烛台的定制颜色
    查看>>
    Ploly:如何在Excel中嵌入完全交互的Ploly图形?
    查看>>
    plotloss记录
    查看>>
    Plotly (Python) 子图:填充构面和共享图例
    查看>>
    Plotly 中的行悬停文本
    查看>>
    Plotly 停用 x 轴排序
    查看>>
    Plotly 域变量解释(多图)
    查看>>
    Plotly 绘制表面 3D 未显示
    查看>>
    Plotly-Dash 存在未知问题并创建“加载依赖项时出错“;通过使用 Python-pandas.date_range
    查看>>
    Plotly-Dash:如何过滤具有多个数据框列的仪表板?
    查看>>
    Plotly:如何为 x 轴上的时间序列设置主要刻度线/网格线的值?
    查看>>
    Plotly:如何从 x 轴删除空日期?
    查看>>
    Plotly:如何从单条迹线制作堆积条形图?
    查看>>
    Plotly:如何以 Root 样式绘制直方图,仅显示直方图的轮廓?
    查看>>
    Plotly:如何使用 Plotly Express 组合散点图和线图?
    查看>>
    Plotly:如何使用 plotly.graph_objects 和 plotly.express 定义图形中的颜色?
    查看>>
    Plotly:如何使用 Python 对绘图对象条形图进行颜色编码?
    查看>>
    Plotly:如何使用 updatemenus 更新一个特定的跟踪?
    查看>>