A1030 Travel Plan (30分)

一、技术总结

  1. 这一题是关于图的遍历,但是涉及Djikstra算法,在求最短路径的同时,还要把路径记录下来;同时增加了边权,也就会每个城市之间的花费;
  2. 这里采用Djikstra算法+DFS遍历的方法
  3. 第一步使用Djikstra算法求出最短路径,使用vector类型pre数组进行存储,每个结点里面保存的是他们的前驱结点。
  4. 然后再使用DFS遍历pre,然后会有得出遍历树。
  5. 要注意下标是0~n-1,还是1~n。
  6. 细节问题参考代码

二、参考代码

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 510;
const int INF = 100000000;
bool vis[MAXN] = {false};
int d[MAXN], c[MAXN];
int G[MAXN][MAXN], cost[MAXN][MAXN];
vector<int> pre[MAXN];
vector<int> path, tempPath;
int n, m, c1, c2;//求的两城市id号
int optValue = INF;
void Djikstra(int s){
    fill(d, d+MAXN, INF);
    d[s] = 0;
    for(int i = 0; i < n; i++){
        int u = -1, MIN = INF;
        for(int j = 0; j < n; j++){
            if(vis[j] == false && d[j] < MIN){
                u = j;
                MIN = d[j];
            }
        }
        if(u == -1) return;
        vis[u] = true;
        for(int v = 0; v < n; v++){
            if(vis[v] == false && G[u][v] != INF){
                if(d[u] + G[u][v] < d[v]){
                    d[v] = d[u] + G[u][v];
                    pre[v].clear();
                    pre[v].push_back(u);
                }else if(d[u] + G[u][v] == d[v]){
                    pre[v].push_back(u);
                }
            }
        }
    }
}
void DFS(int v){
    if(v == c1){

        tempPath.push_back(v);
        int value = 0;
        for(int i = tempPath.size()-1; i > 0; i--){
            int id = tempPath[i], idNext = tempPath[i-1];
            value += cost[id][idNext];
        }
        if(value < optValue){
            path = tempPath;
            optValue = value;
        }
        tempPath.pop_back();
        return;
    }
    tempPath.push_back(v);
    for(int i = 0; i < pre[v].size(); i++){
        DFS(pre[v][i]);
    }
    tempPath.pop_back();
}
int main(){
    scanf("%d%d%d%d", &n, &m, &c1, &c2);
    int id1, id2;
    int distance, price;
    fill(G[0], G[0]+MAXN*MAXN, INF);
    fill(cost[0], cost[0]+MAXN*MAXN, INF);
    for(int i = 0; i < m; i++){
        scanf("%d%d%d%d", &id1, &id2, &distance, &price);
        G[id1][id2] = distance;
        G[id2][id1] = distance;
        cost[id1][id2] = price;
        cost[id2][id1] = price;
    }
    Djikstra(c1);
    DFS(c2);
    for(int i = path.size()-1; i >= 0; i--){
        printf("%d ", path[i]);
    }
    printf("%d %d", d[c2], optValue);
    return 0;
}

原文地址:https://www.cnblogs.com/tsruixi/p/12395412.html

时间: 2024-10-09 17:16:21

A1030 Travel Plan (30分)的相关文章

1030 Travel Plan (30 分)

1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting ci

A1030. Travel Plan (30)

A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination.

1030 Travel Plan (30分)

最短路径问题.一定要注意将visited[u]是否等于0加入判断!很关键,还是没有理解透,所以才忘了加.(第42行和第45行) 其实图相关的题通过率蛮高的...套路题,坑少. 1 #include <iostream> 2 #include<cstdio> 3 #include<vector> 4 #define inf 0x3f3f3f3f 5 using namespace std; 6 struct Edge{ 7 int next,dis,cos; 8 }; 9

pat1030. Travel Plan (30)

1030. Travel Plan (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help

PAT 1030. Travel Plan (30)

1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting cit

1030 Travel Plan (30)(30 分)

A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination.

1030. Travel Plan (30)

dfs使用vector保存最短路径 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a tr

PAT (Advanced Level) 1030. Travel Plan (30)

先处理出最短路上的边.变成一个DAG,然后在DAG上进行DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; const int INF=0x7FFFFFFF; const int

PAT:1030. Travel Plan (30) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int MAXV=510; const int INF=0x3fffffff; int N,M,S,D; //城市数,高速公路数,起点,终点 bool visit[MAXV]; //标记在迪杰斯特拉中是否遍历过 int GD[MAXV][MAXV]; //存储距离图 int GC[MAXV][MAXV];