牛客算法周周练3 C -小雨坐地铁(分层最短路)

链接:https://ac.nowcoder.com/acm/contest/5338/C
来源:牛客网

思路:

表示到第个节点时在第个地铁上的最小花费。

然后算法跑最短路即可,

转移有种情况:

1️⃣:换到地铁线路i 且走了一个节点,

2️⃣:不换地铁线路,继续在第个地跌线路上走:

还需要判断的适合答案是

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <unordered_map>
// #include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 3000 + 10;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int dp[maxn][502];
struct node
{
    int f, t, val, id;
    node() {

    }
    node(int ff, int tt, int vv, int ii)
    {
        f = ff;
        t = tt;
        val = vv;
        id = ii;
    }
    bool operator < (const node & b) const
    {
        return val > b.val;
    }
};
priority_queue<node> heap;
int n, m;
int s, t;
int a[maxn];
int b[maxn];
std::vector<node> v[maxn];
void add_edge(int x, int y, int id)
{
    v[x].pb(node(x, y, 0, id));
    v[y].pb(node(y, x, 0, id));
}
void dij()
{
    repd(i, 1, n)
    {
        repd(j, 1, m)
        {
            dp[i][j] = inf;
        }
    }

    heap.push(node(0, s, 0, 0));
    node temp;
    while (!heap.empty())
    {
        temp = heap.top();
        heap.pop();
        for (auto &y : v[temp.t])
        {
            int plus = 0;
            if (y.id != temp.id)
            {
                plus = a[y.id] + b[y.id];
            } else
            {
                plus = b[y.id];
            }
            if (dp[y.t][y.id] > dp[temp.t][temp.id] + plus)
            {
                dp[y.t][y.id] = dp[temp.t][temp.id] + plus ;
                heap.push(node(temp.t, y.t, dp[y.t][y.id], y.id));
            }
        }
    }

}
int main()
{
#if DEBUG_Switch
    freopen("C:\\code\\input.txt", "r", stdin);
#endif
    //freopen("C:\\code\\output.txt","r",stdin);
    n = readint();
    m = readint();
    s = readint();
    t = readint();
    repd(i, 1, m)
    {
        a[i] = readint();
        b[i] = readint();
        int k = readint();
        int x = readint();
        int y;
        repd(j, 1, k - 1)
        {
            y = readint();
            add_edge(x, y, i);
            x = y;
        }
    }
    dij();
    int ans = inf;
    repd(i, 1, m)
    {
        ans = min(ans, dp[t][i]);
    }
    if (s == t)
        ans = 0;
    printf("%d\n", ans == inf ? -1 : ans );
    return 0;
}