#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int N = 510;

int n, m, S, T;
int d1[N][N], d2[N][N];
//d1存放length ,d2存放time
int dist1[N], dist2[N], pre[N];
bool st[N];

pair<int, string> dijkstra(int d1[][N], int d2[][N], int type)
{
    memset(dist1, 0x3f, sizeof dist1);
    memset(dist2, 0x3f, sizeof dist2);
    memset(st, 0, sizeof st);
    dist1[S] = dist2[S] = 0;

    for (int i = 0; i < n; i ++ )
    {
        int t = -1;
        for (int j = 0; j < n; j ++ )
            if (!st[j] && (t == -1 || dist1[t] > dist1[j]))
                t = j;
        st[t] = true;
        for (int j = 0; j < n; j ++ )
        {
            int w;
            //0:最短路径,距离第一关键字,时间第二关键字
            //1:最快,时间第一关键字,节点数最少第二
            if (type == 0) w = d2[t][j];
            else w = 1;
			//第一关键字可以更新
            if (dist1[j] > dist1[t] + d1[t][j])
            {
                dist1[j] = dist1[t] + d1[t][j];
                dist2[j] = dist2[t] + w;
                pre[j] = t;
            }
            //第二关键字可以更新
            else if (dist1[j] == dist1[t] + d1[t][j])
            {
                if (dist2[j] > dist2[t] + w)
                {
                    dist2[j] = dist2[t] + w;
                    pre[j] = t;
                }
            }
        }
    }

    vector<int> path;
    for (int i = T; i != S; i = pre[i]) path.push_back(i);

    pair<int, string> res;
    res.first = dist1[T];
    res.second = to_string(S);
    for (int i = path.size() - 1; i >= 0; i -- )
        res.second += " -> " + to_string(path[i]);
    return res;
}

int main()
{
    cin >> n >> m;

    memset(d1, 0x3f, sizeof d1);
    memset(d2, 0x3f, sizeof d2);

    while (m -- )
    {
        int a, b, t, c, d;
        cin >> a >> b >> t >> c >> d;
        d1[a][b] = min(d1[a][b], c);
        d2[a][b] = min(d2[a][b], d);
        if (!t)
        {
            d1[b][a] = min(d1[b][a], c);
            d2[b][a] = min(d2[b][a], d);
        }
    }

    cin >> S >> T;

    auto A = dijkstra(d1, d2, 0);
    auto B = dijkstra(d2, d1, 1);

    if (A.second != B.second)
    {
        printf("Distance = %d: %s\n", A.first, A.second.c_str());
        printf("Time = %d: %s\n", B.first, B.second.c_str());
    }
    else
    {
        printf("Distance = %d; Time = %d: %s\n", A.first, B.first, A.second.c_str());
    }

    return 0;
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐