luogu P3110 [USACO14DEC]驮运Piggy Back

二次联通门 : 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