首页 > 其他分享 >[USACO] Piggy Back

[USACO] Piggy Back

时间:2023-10-14 09:15:11浏览次数:30  
标签:dist int rint 短路 Back USACO dijkstra Piggy include

[USACO] Piggy Back

题目大概意思是一个无向图,Bessie 从 1 号仓库走到 n 号(每次花费 x), Elsie 从 2 号仓库走到 n 号(每次花费 y),如果两个人走同一条路花费 z,求总花费最小。

跑三遍最短路,别得到 Bessie 从 1 号仓库出发的最短路,Elsie 从 2 号仓库出发的最短路,和从 n 出发到其他每个点的最短路。

在走到相遇点之前,两人一定走的是对应最短路才能减小花费

不难得出 \(ans = min(ans, x * d1[i] + y * d2[i] + z * d3[i])\)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>

#define rint register int
#define endl '\n'

using std::cin;
using std::cout;

const int inf = 0x3f3f3f3f;
const int N = 1e6 + 5;
const int M = 4e6 + 5;

int n, m, s;
int idx, h[N], e[M], ne[M], d1[N], d2[N], d3[N];
bool v[N];
std::priority_queue<std::pair<int, int>> q;

void add(int a, int b)
{
    e[++idx] = b, ne[idx] = h[a], h[a] = idx;
}

void dijkstra(int s, int *dist)
{
    for (rint i = 1; i <= n; i++)
    {
        dist[i] = inf;
    }
    memset(v, 0, sizeof v);
    dist[s] = 0;
    q.push(std::make_pair(0, s));

    while (!q.empty())
    {
        int x = q.top().second;
        q.pop();
        if (v[x])
        {
            continue;
        }
        v[x] = 1;
        for (rint i = h[x]; i; i = ne[i])
        {
            int y = e[i];
            int z = 1;
            if (dist[y] > dist[x] + z)
            {
                dist[y] = dist[x] + z;
                q.push(std::make_pair(-dist[y], y));
            }
        }
    }
}

int x, y, z;

int main()
{
    cin >> x >> y >> z >> n >> m;

    for (rint i = 1; i <= m; i++)
    {
        int a, b;
        cin >> a >> b;
        add(a, b);
        add(b, a);
    }

    dijkstra(1, d1);
    dijkstra(2, d2);
    dijkstra(n, d3);

    int ans = inf;

    for (rint i = 1; i <= n; i++)
    {
        ans = std::min(ans, x * d1[i] + y * d2[i] + z * d3[i]);
    }

    cout << ans << endl;

    return 0;
}

标签:dist,int,rint,短路,Back,USACO,dijkstra,Piggy,include
From: https://www.cnblogs.com/spaceswalker-garden/p/17763648.html

相关文章

  • 洛谷 P8192 - [USACO22FEB] Paint by Rectangles P
    比较抽象的一个题。首先先考虑\(T=1\),如果我们建一张图,将图上所有横线与竖线的交点看作图上的点,相邻的有线段相连的点看作图上的边的话,那么显然会得到一张平面图,而我们要计算的是平面图上面的个数,根据公式\(F=E-V+C+1\),其中\(C\)为这张图中连通块的个数。设\(c\)为线段与线......
  • pushback流的例子
    pushback流有PushbackInputStream和PushbackRead。 例子: publicclassSequenceCount{ publicstaticvoidmain(String[]args)throwsIOException{ PushbackInputStreamin=newPushbackInputStream(System.in); intmax=0; //longestsequencefound int......
  • Trying to backward through the graph a second time
    原因是把创建loss的语句loss_aux=torch.tensor(0.)放在循环体外了,可能的解释是第一次backward后把计算图删除,第二次backward就会找不到父节点,也就无法反向传播。参考:https://stackoverflow.com/questions/55268726/pytorch-why-does-preallocating-memory-cause-trying-to-backw......
  • mysql 物理备份xtrabackup
    1.优缺点优点:a.备份过程快速可靠b.支持增量备份c.备份过程不会打断正在执行的事务d.能够基于压缩等功能节约磁盘和空间e.自动实现备份验证f.还原速度快缺点:a.只能对innodb表进行增备,myisam表备份是全备b.对myisam表进行备份时要对全库加readlock,阻塞写操作,若备份在从库上进行会......
  • 洛谷P3118 [USACO15JAN] Moovie Mooving G 题解
    MoovieMoovingG设\(f[i][S]\)表示在第\(i\)场(注意是场,不是部)电影时,已经看了\(S\)里面的电影是否合法。然后贪心地取\(|S|\)最小的状态保存。光荣MLE了,\(21\%\)。发现当一场电影结束后,无论这一场是在哪里看的都没关系。因此我们设\(f[S]\)表示只看集合\(S\)里......
  • Backtrader - AttributeError: 'OptReturn' object has no attribute 'datas'
    1.0ErrorTraceback(mostrecentcalllast):File"D:/PycharmProjects/dbpower.backtrader.001/app/main_machine_learning.py",line191,in<module>img=cerebro.plot(style='line',plotdist=0.1,grid=True)File"D:\P......
  • Blazor Server App Cannot find the fallback endpoint specified by route values
    github官方issues中提到的解决方案,CreateBuilder时指定项目绝对路径可以解决。1//指定项目路径,也可以用Assembly.GetCallingAssembly获取2conststringContentRootPath=@"C:\Users\BlazorServer";//项目的路径3conststringApplicationName=nameof(BlazorServer);......
  • [USACO17JAN] Promotion Counting P 题解
    [USACO17JAN]PromotionCountingP题解前言好久没写题解了,今天趁我心情好赶紧水一篇。思路首先拿到这题,关键词检索:子树,比\(p_i\)大的,树状数组!现在考虑如何去掉其他子树的贡献……emm,我直接在算贡献的时候去掉其他子树的贡献不就好了!当然,暴力去贡献复杂度肯定爆炸,这里考虑......
  • [USACO08FEB]meteor Shower S题解(bfs)
    题目描述贝茜听说一场特别的流星雨即将到来:这些流星会撞向地球,并摧毁它们所撞击的任何东西。她为自己的安全感到焦虑,发誓要找到一个安全的地方(一个永远不会被流星摧毁的地方)。如果将牧场放入一个直角坐标系中,贝茜现在的位置是原点,并且,贝茜不能踏上一块被流星砸过的土地。根据预......
  • P1457 [USACO2.1] 城堡 The Castle 题解
    分析感觉没有蓝题难度一道bfs题目,相较于大部分bfs题,它较为复杂,但分析一下还是很好水过的。建立墙时,可以用三维数组,\(wall_{~i,~j,~pos}\)表示第\(i\)行第\(j\)列\(pos\)方向有墙。观察发现,\(8=2^3,4=2^2,2=2^1,1=2^1\),于是可以用位运算快速储存。这里给出......