luogu P3110 [USACO14DEC]驮运Piggy Back

本文详细介绍了一种经典的最短路径算法——SPFA,并通过一个具体的USACO竞赛题目进行实践解析。文中提供了完整的C++代码实现,展示了如何利用SPFA解决带有多种费用的最短路径问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二次联通门 : luogu P3110 [USACO14DEC]驮运Piggy Back

 

 

 

 

/*
    luogu P3110 [USACO14DEC]驮运Piggy Back

    Spfa 水题
*/
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>

const int BUF = 12312332;
char Buf[BUF], *buf = Buf;
#define INF 1e17
inline void read (int &now)
{
    for (now = 0; !isdigit (*buf); ++ buf);
    for (; isdigit (*buf); now = now * 10 + *buf - '0', ++ buf);
}

#define Max 50004

struct E { E *n; int v; };
E poor[Max], *list[Max], *Ta = poor;

inline void In (int u, int v)
{
    ++ Ta, Ta->v = v, Ta->n = list[u], list[u] = Ta;
    ++ Ta, Ta->v = u, Ta->n = list[v], list[v] = Ta;
}

int d[Max][3];
inline long long min (long long a, int b) { return a < b ? a : b; }
void Spfa (int S, int t)
{
    std :: queue <int> Queue;
    Queue.push (S),  d[S][t] = 0;
    for (int now; !Queue.empty (); Queue.pop ())
    {
        now = Queue.front ();
        for (E *e = list[now]; e; e = e->n)
            if (d[e->v][t] == -1)
            {
                d[e->v][t] = d[now][t] + 1;
                Queue.push (e->v);
            }
    }
    return ;
}

int Main ()
{
    fread (buf, 1, BUF, stdin); long long Answer = INF;
    int A, B, C, N, M; read (A), read (B), read (C);
    read (N), read (M); register int i, j; int x, y;
    for (i = 1; i <= M; ++ i)
        read (x), read (y), In (x, y);
    memset (d, -1, sizeof d);
    Spfa (1, 0), Spfa (2, 1), Spfa (N, 2);
    for (i = 1; i <= N; ++ i)
        Answer = min (Answer, d[i][0] * A + d[i][1] * B + C * d[i][2]);
    printf ("%lld", Answer);
    return 0;
}

int ZlycerQan = Main ();
int main (int argc, char *argv[]) {;}

 

转载于:https://www.cnblogs.com/ZlycerQan/p/7467323.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值