2016-PTA初赛-L3-1 天梯地图(dijkstra模板)

image

思路

有条件的dijkstra模板题,不需要小根堆优化,这题的第二个样例:路径一样则合并输出,怎么判断路径一样?会想到C++的vector对==进行了重写(即每个对应位置的元素一样)。

AcCode:

#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int inf = 0x7fffffff;
int graph[510][510][2], dirt[510], lst[510], N, M;
vector<int> path[2];
void getPath(int end, int p){
    if(end == -1) return;
    getPath(lst[end], p);
    path[p].push_back(end);
}

int dj_distance(int be, int end){
    bool vis[510];
    memset(vis, false, sizeof(vis));
    memset(dirt, 0x2f, sizeof(dirt));
    memset(lst, 0, sizeof(lst));
    int cnt[510];
    dirt[be] = 0;
    cnt[be] = 1;
    lst[be] = -1;
    for(int c = 0; c < N; c++){
        int cur;
        int mn = inf;
        for(int i = 0; i < N; i++) if(dirt[i] < mn && !vis[i]) mn = dirt[i], cur = i;
        for(int i = 0; i < N; i++){
            if(!vis[i] && graph[cur][i][1]){
                if(graph[cur][i][1] + dirt[cur] < dirt[i]){
                    dirt[i] = graph[cur][i][1] + dirt[cur];
                    lst[i] = cur;
                    cnt[i] = cnt[cur] + 1;
                }else if(graph[cur][i][1] + dirt[cur] == dirt[i] && cnt[cur] + 1 < cnt[i]){
                    lst[i] = cur;
                    cnt[i] = cnt[cur] + 1;
                }
            }
        }
        vis[cur] = true;
    }
    getPath(end, 0);
    // for(auto x: path[0]) cout << x << " " << endl;
    return dirt[end];
}

int dj_time(int be, int end){
    bool vis[510];
    memset(vis, false, sizeof(vis));
    memset(dirt, 0x2f, sizeof(dirt));
    memset(lst, 0, sizeof(lst));
    int cnt[510];
    dirt[be] = 0;
    cnt[be] = 1;
    lst[be] = -1;
    for(int c = 0; c < N; c++){
        int cur;
        int mn = inf;
        for(int i = 0; i < N; i++) if(dirt[i] < mn && !vis[i]) mn = dirt[i], cur = i;
        for(int i = 0; i < N; i++){
            if(!vis[i] && graph[cur][i][0]){
                if(graph[cur][i][0] + dirt[cur] < dirt[i]){
                    dirt[i] = graph[cur][i][0] + dirt[cur];
                    lst[i] = cur;
                    cnt[i] = cnt[cur] + graph[cur][i][1];
                }else if(graph[cur][i][0] + dirt[cur] == dirt[i] && cnt[cur] + graph[cur][i][1] < cnt[i]){
                    lst[i] = cur;
                    cnt[i] = cnt[cur] + graph[cur][i][1];
                }
            }
        }
        vis[cur] = true;
    }
    getPath(end, 1);
    // for(auto x: path[0]) cout << x << " " << endl;
    return dirt[end];
}

int main(){
    cin >> N >> M;
    while(M--){
        int v1, v2, oneWay, length, time; cin >> v1 >> v2 >> oneWay >> length >> time;
        graph[v1][v2][0] = time;
        graph[v1][v2][1] = length;
        if(!oneWay) graph[v2][v1][0] = time, graph[v2][v1][1] = length;
    }
    int be, end; cin >> be >> end;
    int time = dj_time(be, end);
    int distance = dj_distance(be, end);
    if(path[0] == path[1]){
        cout << "Time = " << time << "; Distance = " << distance << ": ";
        for(int i = 0; i < path[1].size(); i++){
            cout << path[1][i];
            if(i != path[1].size() - 1) cout << " => ";
        }
    }else{
        cout << "Time = " << time << ": ";
        for(int i = 0; i < path[1].size(); i++){
            cout << path[1][i];
            if(i != path[1].size() - 1) cout << " => ";
        }
        cout << endl << "Distance = " << distance << ": ";
        for(int i = 0; i < path[0].size(); i++){
            cout << path[0][i];
            if(i != path[0].size() - 1) cout << " => ";
        }
    }
    return 0;
}
posted @ 2025-11-26 01:29  Yuhhhhh  阅读(0)  评论(0)    收藏  举报