• 1030 Travel Plan(Dijksta +DFS)


    1030 Travel Plan(Dijksta +DFS)

    0、题目

    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. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

    Input Specification:

    Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤500) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

    City1 City2 Distance Cost
    
    • 1

    where the numbers are all integers no more than 500, and are separated by a space.

    Output Specification:

    For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

    Sample Input:

    4 5 0 3
    0 1 1 20
    1 3 2 30
    0 3 4 10
    0 2 2 20
    2 3 1 20
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    Sample Output:

    0 2 3 3 40
    
    • 1

    1、大致题意

    求起点到终点的最短路径最短距离和花费,要求首先路径最短,其次花费最少,要输出完整路径

    2、基本思路

    Dijksta + DFSDijkstra 记录路径 pre 数组,然后用 dfs 求最短的一条 m i n c o s t min_{cost} mincost 以及它的路径 path ,最后输出 path 数组和 m i n c o s t min_{cost} mincost

    注意,路径 path 因为是从末端一直压入 push_backpath 里面的,所以要输出路径的时候倒着输出

    3、解题过程

    3.1 第一份代码(18/30)

    写出第一份 Dijksta + DFS 的代码,很遗憾只对了第一个测试用例

    #include
    #include
    #include
    #include
    using namespace std;
    const long long inf=0x7fffffff;
    int n,m,s,e,cnt,end_cost;
    int head[10100],vis[10100],dis[10100];
    vector<int> pre[10100];
    vector<int> path;
    struct Edge {
    	int v,w,cost,next;
    } edge[501000];
    
    void addEdge(int u, int v,int w,int cost) {
    	edge[++cnt].v=v;
    	edge[cnt].w=w;
    	edge[cnt].cost=cost;
    	edge[cnt].next=head[u];
    	head[u]=cnt;
    }
    
    struct node {
    	int dis;
    	int pos;
    	bool operator <( const node &x )const {
    		return x.dis < dis;
    	}
    }ntmp;
    
    priority_queue<node>q;
    void dijkstra(int s) {
    	for(int i=1; i<=n; i++) { //这行有问题
    		dis[i]=inf;
    		vis[i]=0;
    	}
    	ntmp.dis=0;
    	ntmp.pos=s;
    	q.push(ntmp);
    	dis[s]=0;
    	while(!q.empty()) {
    		node u=q.top();
    		q.pop();
    		int pos=u.pos;
    		if(vis[pos]) {
    			continue;
    		}
    		vis[pos]=1;
    		for(int i=head[pos]; i; i=edge[i].next) {
    			int v=edge[i].v;
    			if(dis[v]>=dis[pos]+edge[i].w) {
    				if(dis[v]>dis[pos]+edge[i].w) {
    					pre[v].clear();
    					dis[v]=dis[pos]+edge[i].w;
    				}
    				pre[v].push_back(pos);
    				if(!vis[v]) {
    					ntmp.dis=dis[v];
    					ntmp.pos=v;
    					q.push((ntmp));
    				}
    			}
    		}
    	}
    }
    
    void dfs(int pos,int cost,vector<int> p) {
    	p.push_back(pos);
    	if(pos==s) {
    		if(end_cost>cost) {
    			end_cost=cost;
    			path=p;
    		}
    		return;
    	}
    	int tmp;
    	for(int i=0; i<pre[pos].size(); i++) {
    		for(int j=head[pos]; j; j=edge[j].next) {
    			if(edge[j].v==pre[pos][i]) {
    				tmp=edge[j].cost;
    				break;
    			}
    		}
    		cost+=tmp;
    		dfs(pre[pos][i],cost,p);
    		cost-=tmp;
    	}
    	return;
    }
    
    int main() {
    	scanf("%d%d%d%d",&n,&m,&s,&e);
    	memset(edge,0,sizeof(edge));
    
    	int u,v,w,cost;
    	cnt=0;
    	for(int i=0; i<m; i++) {
    		scanf("%d%d%d%d",&u,&v,&w,&cost);
    		addEdge(u,v,w,cost);
    		addEdge(v,u,w,cost);
    	}
    	dijkstra(s);
    
    	end_cost=inf;
    	vector<int>p;
    	dfs(e,0,p);
    
    	for(int i=path.size()-1; i>=0; i--) {
    		cout<<path[i]<<" ";
    	}
    	cout<<dis[e]<<" "<<end_cost;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113

    在这里插入图片描述

    3.2 预处理的错误

    经过排查,找到了一个测试用例,如下:

    输入:
    4 5 1 2
    0 1 1 20
    1 3 2 30
    0 3 4 10
    0 2 2 20
    2 3 1 20
    输出:
    1 0 2 3 40
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    主要是在 点0 的位置没有初始化,然后出错。这个原因也很简单,就是因为我使用的Dijksta板子,基本上都是以 0 为起始位置的,所以不用考虑 点0 ,以后还是注意这些细节。

    3.3 AC代码

    #include
    #include
    #include
    #include
    using namespace std;
    const long long inf=0x7fffffff;
    int n,m,s,e,cnt,end_cost;
    int head[10100],vis[10100],dis[10100];
    vector<int> pre[10100];
    vector<int> path;
    struct Edge {
    	int v,w,cost,next;
    } edge[501000];
    
    void addEdge(int u, int v,int w,int cost) {
    	edge[++cnt].v=v;
    	edge[cnt].w=w;
    	edge[cnt].cost=cost;
    	edge[cnt].next=head[u];
    	head[u]=cnt;
    }
    
    struct node {
    	int dis;
    	int pos;
    	bool operator <( const node &x )const {
    		return x.dis < dis;
    	}
    }ntmp;
    
    priority_queue<node>q;
    void dijkstra(int s) {
    	for(int i=0; i<=n; i++) { //注意这行
    		dis[i]=inf;
    		vis[i]=0;
    	}
    	ntmp.dis=0;
    	ntmp.pos=s;
    	q.push(ntmp);
    	dis[s]=0;
    	while(!q.empty()) {
    		node u=q.top();
    		q.pop();
    		int pos=u.pos;
    		if(vis[pos]) {
    			continue;
    		}
    		vis[pos]=1;
    		for(int i=head[pos]; i; i=edge[i].next) {
    			int v=edge[i].v;
    			if(dis[v]>=dis[pos]+edge[i].w) {
    				if(dis[v]>dis[pos]+edge[i].w) {
    					pre[v].clear();
    					dis[v]=dis[pos]+edge[i].w;
    				}
    				pre[v].push_back(pos);
    				if(!vis[v]) {
    					ntmp.dis=dis[v];
    					ntmp.pos=v;
    					q.push((ntmp));
    				}
    			}
    		}
    	}
    }
    
    void dfs(int pos,int cost,vector<int> p) {
    	p.push_back(pos);
    	if(pos==s) {
    		if(end_cost>cost) {
    			end_cost=cost;
    			path=p;
    		}
    		return;
    	}
    	int tmp;
    	for(int i=0; i<pre[pos].size(); i++) {
    		for(int j=head[pos]; j; j=edge[j].next) {
    			if(edge[j].v==pre[pos][i]) {
    				tmp=edge[j].cost;
    				break;
    			}
    		}
    		dfs(pre[pos][i],cost+tmp,p);
    	}
    	return;
    }
    
    int main() {
    	scanf("%d%d%d%d",&n,&m,&s,&e);
    	memset(edge,0,sizeof(edge));
    
    	int u,v,w,cost;
    	cnt=0;
    	for(int i=0; i<m; i++) {
    		scanf("%d%d%d%d",&u,&v,&w,&cost);
    		addEdge(u,v,w,cost);
    		addEdge(v,u,w,cost);
    	}
    	dijkstra(s);
    
    	end_cost=inf;
    	vector<int>p;
    	dfs(e,0,p);
    
    	for(int i=path.size()-1; i>=0; i--) {
    		cout<<path[i]<<" ";
    	}
    	cout<<dis[e]<<" "<<end_cost;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111

    在这里插入图片描述

  • 相关阅读:
    Jenkins 发布 Gitee 上的 SpringBoot 项目全过程(详细)
    KBU1510-ASEMI整流桥KBU1510
    Spring + vue 项目部署(全网最详细教程_含内网穿透部署)
    零基础学python之元组
    metaRTC7集成lvgl ui demo编译指南
    字符串拼接你真的啥都知道了吗
    MapReduce基础
    代码实现:求前N个数字的阶乘
    并发编程之线程池
    C++ Reference: Standard C++ Library reference: C Library: cwctype: iswblank
  • 原文地址:https://blog.csdn.net/qq_46371399/article/details/126550424