Multimodal Transport

New methods of shipping goods have transformed the transportation industry and helped usher in an era of global commerce. Nowadays, someone can click a few buttons and have an item leave a factory on the other side of the world and show up at their doorstep the next day. To help accomplish this, there are a number of modes of transport within the shipping industry. The four most common are: air, boat, rail and truck.

Transportation companies tend to keep the mode of transport consistent throughout a packages journey (route/path). However, when customers are not very time sensitive, often times the cheapest way to move a package from one location to another is to use more than one mode of transport. One has to be careful, though, as additional costs are incurred when switching between transport modes within a city on the package path. (Switching transport mode refers to a package leaving a city in a different mode than it arrived at the city, e.g., the package arrived by air and leaves by truck.)

A new startup, KnightShip, has asked for your help in figuring out the cheapest way to ship packages when switching between transportation modes is acceptable.

The Problem:

Given the costs for various modes of transport between cities, and the cost of switching mode within a city, calculate the lowest cost to transport an item from an origin to a destination.

The Input:

The first input line contains a positive integer, n, indicating the number of test cases to process. Each test case will contain multiple input lines. The first line of each test case will contain an integer, c (2 ≤ c ≤ 400), representing the number of cities within the transportation network. Each of the following c input lines contains two values: a string (1 to 20 uppercase letters, inclusive), representing the name of a city and an integer (between 1 and 1000, inclusive), representing the cost of changing the transport mode within that city.

The city information for a test case is followed by the route information for the test case. There will be an input line containing one integer, r (1 ≤ r ≤ 40000), representing the number of route segments in the network. This will be followed by a listing of all route segments, one per line. Each route segment will contain four values: p, q, representing two cities from the previous list, m, representing the transport mode (one of four values AIR, SEA, RAIL, TRUCK) for that route segment, and an integer v (1 ≤ v ≤ 1000), representing the cost to ship a package between the two cities (either direction). Note that there may be no route segments for a particular transport mode. There will be no duplicate city pair within a given mode of transport, but different transport modes (even all four modes) can exist between the same two cities.

The last input line for a test case contains two distinct cities o and d which represent the origin and destination cities for which we want the minimum cost to ship a package. Cities o and d are guaranteed to have a path (of finite cost) that exists between them. Any mode of transport can be used to leave city o and any mode can be used to reach city d (they don’t necessarily need to match). The transport mode can change in the intermediate stages as well.

The Output:

For each test case, output a single integer on a line by itself indicating the minimum cost to move a package from city o to city d.

样例输入
2
4
ORLANDO 10
TAMPA 15
MIAMI 5
JACKSONVILLE 10
7
TAMPA JACKSONVILLE AIR 100
MIAMI TAMPA SEA 70
JACKSONVILLE MIAMI RAIL 45
ORLANDO JACKSONVILLE TRUCK 85
TAMPA ORLANDO RAIL 10
MIAMI JACKSONVILLE SEA 15
ORLANDO MIAMI TRUCK 15
JACKSONVILLE TAMPA
2
ORLANDO 15
TAMPA 10
3
ORLANDO TAMPA AIR 7
TAMPA ORLANDO TRUCK 3
ORLANDO TAMPA RAIL 19
ORLANDO TAMPA
样例输出
55
3
#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define vi vector<int>
#define vc vector<char>
#define pii pair<int,int>
#define pll pair<long,long>
#define pil pair<int,long>
#define pli pair<long,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 405, M = 4e4 + 10, INF = 0x3f3f3f3f;
int head[N], ver[M << 1], edge[M << 1], tpe[M << 1], Next[M << 1], tot;
int sta, fin, val[N];
msi ty;
int T, n, m;
struct P {
    int x, t;
};
bool v[N][5];
int d[N][5];

inline void add(int x, int y, int t, int z) {
    ver[++tot] = y;
    edge[tot] = z;
    tpe[tot] = t;
    Next[tot] = head[x];
    head[x] = tot;
}

inline int spfa() {
    ms(v);
    memset(d, 0x3f, sizeof(d));
    queue<P> q;
    repi(i, 1, 4)q.push({sta, i}), v[sta][i] = true, d[sta][i] = 0;
    while (!q.empty()) {
        P x = q.front();
        q.pop(), v[x.x][x.t] = false;
        reps(x.x) {
            int y = ver[i], t = tpe[i], z = edge[i];
            if (t == x.t) {
                if (d[y][t] > d[x.x][t] + z) {
                    d[y][t] = d[x.x][t] + z;
                    if (!v[y][t]) {
                        v[y][t] = true;
                        q.push({y, t});
                    }
                }
            } else if (d[y][t] > d[x.x][x.t] + z + val[x.x]) {
                d[y][t] = d[x.x][x.t] + z + val[x.x];
                if (!v[y][t]) {
                    v[y][t] = true;
                    q.push({y, t});
                }
            }
        }
    }
    int res = INF;
    repi(i, 1, 4)res = min(res, d[fin][i]);
    return res;
}

int main() {
    T = qr();
    ty["AIR"] = 1;
    ty["SEA"] = 2;
    ty["RAIL"] = 3;
    ty["TRUCK"] = 4;
    while (T--) {
        ms(head), ms(tpe), tot = 0;
        n = qr();
        msi num;
        repi(i, 1, n) {
            string nam;
            int x;
            cin >> nam;
            x = qr();
            num[nam] = i;
            val[i] = x;
        }
        m = qr();
        repi(i, 1, m) {
            string x, y, t;
            int z;
            cin >> x >> y >> t;
            z = qr();
            add(num[x], num[y], ty[t], z);
            add(num[y], num[x], ty[t], z);
        }
        string S, F;
        cin >> S >> F;
        sta = num[S], fin = num[F];
        pi(spfa());
    }
    return 0;
}
发布了360 篇原创文章 · 获赞 28 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_45323960/article/details/105355816
今日推荐