1030 Travel Plan (30 分) //Dijkstra练习

题干

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

思考

这一题考察的是, Dijkstra算法, 即给一个图, 求这个单点到其他点的最小距离。 通过这个算法, 可以得到通过一个点, 求得, 到其他所有点最小距离, 我们将之分别定义为start and end

但是到具体的题目, 一般来说, 只会给一个, start 和 一个 end, 求以最小距离展开的问题。

例子: 比如,如果start 到 end的距离, 有两条相同, 我们一般来说, 会定义第二个标尺, 比如, 路径的点权之和最小, 比如, 路径的边权(路径的最小长度, 就是边权问题, 这里我们可以定义一个第二边权, 比如, 路途花费)。

一般问题的展开形式: 求start 到 end 的最小dis后, 同时第二标尺最小或者最大(dis相同情况), 可能, 还会要求输出最小路径的, 从start 到 end 的点的顺序。

如何求, 最小路径? 我们用长为n(点数)的数组, pre【n】, pre[i] 表示 序号为i的点 , 此路径中, 之前一个点的序号, 最后一个点 , 一定是, end, 最开始的点, 一定是, start。 我们在Dijkstra算法中, 得到pre的具体内容, 然后通过递归输出

//Dijkstra 和 Dijskstra + DFS的差别
最小路径, 还引出一个问题, Dijkstra + DFS。
其实在Dijkstra输出的时候, 就用到了DFS, 是在用pre输出最短路径的时候。 我们来谈谈差别 ,在纯粹地使用Dijkstra的时候, pre是一个int类型数组, 但是在Dijkstra+ DFS中, pre是vector 类型的二维数组。 为什么? 在前者, 在Dijkstra的运行过程中, 我们就给出了第二标准的判定, 这造成, 最后得到的路径是唯一的。 后者则不然。 其先把所有的最短路径得到,存储在pre中, 然后, 在DFS中判定, 目的是, 减小Dijkstra 太过复杂, 可能引发错误。 把困难分散, 先得到符合第一标准的内容, 如果有多个, 我们在用DFS判定, 第二标准的优劣。 返回。

收获, 新知识

关于, memset 和 fill的新知识
fill可以初始化二维数组, 因为二维数组本质上, 在内存中, 是链状的

图一般有两种表示方法, 临界矩阵和邻接表, 做Dijstra相关的题目, 还是用邻接矩阵, 因为表示边权方便, 如果, 有第二边权, 在建立一个邻接矩阵就可以了。

答案?

自己写的, 一遍过哈哈哈

#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;

const int maxv = 510;
const int INF = 1000000000;

int n, m , st, ed;
int G[maxv][maxv], C[maxv][maxv];
int d[maxv], cost[maxv];
bool vis[maxv] = {false};
int pre[maxv];

void Dijkstra(int s) {
	fill(d, d + maxv, INF);
	fill(cost, cost + maxv, 0);
	d[s] = 0;
	cost[s] = 0;
	pre[s] = s;
	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(d[u] + G[u][v] < d[v]) {
				d[v] = d[u] + G[u][v]; //更新距离 
				cost[v] = cost[u] + C[u][v]; //更新花费 
				pre[v] = u;
			} else 
			if(d[u] + G[u][v] == d[v]) {
				if(cost[u] + C[u][v] < cost[v]) {
					cost[v] = cost[u] + C[u][v]; //更少的价格
					pre[v] = u; 
				}
			}
		}
	}
}

void DFS(int now, int start) {
	if(now == start) {
		printf("%d ",start);
		return;
	}
	DFS(pre[now], start);
	printf("%d ",now);
} 
int main()
{
	scanf("%d %d %d %d",&n, &m, &st, &ed);
	fill(G[0], G[0] + maxv * maxv, INF);
	fill(C[0], C[0] + maxv * maxv, 0);
	for(int i = 0; i < m; i ++) {
		int c1, c2, len, cost;
		scanf("%d %d %d %d",&c1, &c2, &len, &cost);
		G[c1][c2] = G[c2][c1] = len;
		C[c1][c2] = C[c2][c1] = cost;
	}
	Dijkstra(st);
	DFS(ed,st);
	printf("%d %d",d[ed], cost[ed]);
}

题目链接

``

fill 和 memset的知识

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值