PAT-1030-最短路径问题+DFS

1030 Travel Plan
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

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

Sample Output:

0 2 3 3 40

谷歌中文翻译:
旅行者的地图会给出沿公路行驶的城市之间的距离以及每条公路的费用。现在,您应该编写一个程序来帮助旅行者确定他/她的出发城市和目的地之间的最短路径。如果这样的最短路径不是唯一的,则应该以最小的成本输出该路径,这保证是唯一的。

输入规格:

每个输入文件包含一个测试用例。每种情况都从包含4个正整数N,M,S和D的行开始,其中N(<= 500)是城市数(因此,城市从0到N-1编号); M是高速公路的数量; S和D分别是起始城市和目标城市。然后是M行,每行以以下格式提供公路信息:

城市1 城市2 距离 成本

其中所有数字均为不超过500的整数,并用空格分隔。

输出规格:

对于每个测试用例,在一行中打印沿起点到目的地的最短路径的城市,然后是路径的总距离和总成本。数字必须用空格隔开,并且输出末尾不得有多余的空格。

思路:
这里涉及到两个边权,一个是路径长度越小越好,一个是经过路径的花费越小越好。
所以我们先dijkstra将最短路径求出,再在最短路径的基础上用Dfs遍历所有最短路径将花费最小的那条路径找出来。
很坑很坑的地方,我找了很久的问题,我在init()函数的时候把初始化放到输入的后面,导致一直出来的都是INF的值。在这里感谢恒队大佬的猛男关怀!!!一眼帮我看出问题,鸣谢!!!

#include<bits/stdc++.h>
using namespace std;

const int INF = 99999999;
const int MAX_N = 510;

int n, m, s, d; //点数 边数 起点 终点
int e[MAX_N][MAX_N]; //首要边权 点与点之间距离 
int dis[MAX_N]; //到各点的最短路径 
bool visit[MAX_N];
int cost[MAX_N][MAX_N]; //次边权 即在考虑完距离最小后还要考虑花费最小 
int mincost = INF; //由于之后会dfs不断改变mincost的值  所以此处拎到全局变量 
vector<int> path;
vector<int> temppath;
vector<int> pre[MAX_N];

void init() {
	fill(e[0], e[0] + MAX_N * MAX_N, INF);
	fill(dis, dis + MAX_N, INF);
	cin >> n >> m >> s >> d;	
	for(int i = 0; i < m; i++) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		e[a][b] = c;
		e[b][a] = c;
		cost[a][b] = d;
		cost[b][a] = d;
	} 	 
}

void dijkstra(int s) {
	dis[s] = 0;
	pre[s].push_back(s); //这里将起点的前驱置为自身 
	for(int i = 0; i < n; i++) {
		int u = -1;
		int minn = INF;
		for(int j = 0; j < n; j++) {
			if(visit[j] == false && dis[j] < minn){
				u = j;
				minn = dis[j];
			}
		}
		if(u == -1)	break;
		visit[u] = true;
		for(int v = 0; v < n; v++) {
			if(visit[v] == false && e[u][v] != INF) {
				if(dis[v] > dis[u] + e[u][v]) {
					dis[v] = dis[u] + e[u][v];
					pre[v].clear();
					pre[v].push_back(u);
				} else if(dis[v] == dis[u] + e[u][v]) {
					pre[v].push_back(u);
				}
			}
		}
	}
}

void dfs(int v) {
	//开始咯 从后往前推导 
	temppath.push_back(v);  
	if(v == s) {
		int tempcost = 0;
		for(int i = temppath.size() - 1; i > 0; i--) {
			//如果是计算点权的话 需要i取到0为止
			//但这里我们看的是次边权,即花费
			//为了确定两个点间的那个边的花费 我们需要知道此时的点和往前一个点 
			int id = temppath[i];
			int nextid = temppath[i - 1];
			tempcost += cost[id][nextid];
			//写成cost[nextid][id]都一样  这里是双向
		}		
		if(tempcost < mincost) {
			mincost = tempcost;
			path = temppath;
		} 
		temppath.pop_back();
		return ;
	}
	for(int i = 0; i < pre[v].size(); i++) {
		dfs(pre[v][i]);
	}
	temppath.pop_back();
}


int main() {
	init();
	dijkstra(s);
	dfs(d);
	for(int i = path.size() - 1; i >= 0; i--)
        printf("%d ", path[i]);
    printf("%d %d", dis[d], mincost);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值