PTA 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

cpp 复制代码
#include<cstdio>
#include<algorithm>
using namespace std;
int N,M,C1,C2;
const int maxn=510;


const int INF=1000000000;
bool vis[maxn]={false};
int d[maxn],c[maxn],pre[maxn];
struct node{
	int d;
	int cost;
};
node Node[maxn][maxn];
void dijkstra(){
	fill(d,d+maxn,INF);
	fill(c,c+maxn,INF);
	for(int i=0;i<N;i++) pre[i]=i;
	d[C1]=0;c[C1]=0;
	for(int i=0;i<N;i++){
		int u=-1;int 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&&Node[u][v].d!=0){
			if(d[v]>Node[u][v].d+d[u]){
				d[v]=Node[u][v].d+d[u];
				c[v]=Node[u][v].cost+c[u];
				pre[v]=u;
			}else if(d[v]==Node[u][v].d+d[u]){
				if(c[v]>Node[u][v].cost+c[u]){
					c[v]=Node[u][v].cost+c[u];
					pre[v]=u;
				}
			}
		}
	}
	}
}
void DFS(int s){
	if(s==C1){
		printf("%d",s);
		return;
	}
	else{
		DFS(pre[s]);
		printf(" %d",s);
	}
}
int main(){
	scanf("%d%d%d%d",&N,&M,&C1,&C2);
	for(int i=0;i<M;i++){
		int cost,d;
		int c1,c2;
		scanf("%d%d%d%d",&c1,&c2,&d,&cost);
		node* n=new node;
		n->cost=cost;
		n->d=d;
		Node[c1][c2]=Node[c2][c1]=*n;
		
	} 
	dijkstra();
	DFS(C2);
	printf(" %d %d\n",d[C2],c[C2]);
} 

本题参考了《算法笔记》上面的dijkstra算法,对于求解这一类的最小值问题可以将dijkstra算法的模板背过,然后根据题意修改其内容即可。

除了上面这种做法还可以将第二类距离的求解从dijkstra算法中剥离出来,采用DFS方法来处理,比较简单,我觉得两种方法掌握一种即可。

相关推荐
逸狼25 分钟前
【JavaEE初阶】多线程6(线程池\定时器)
java·开发语言·算法
no_play_no_games1 小时前
[模板]树的最长路径
算法·深度优先·图论·树形结构
tan77º1 小时前
【C++】异常
c++·算法
ymchuangke1 小时前
数据清洗-缺失值处理-缺失值可视化图(竖线)
python·算法·数学建模
我要学编程(ಥ_ಥ)2 小时前
滑动窗口算法专题(1)
java·数据结构·算法·leetcode
niceffking2 小时前
JVM 一个对象是否已经死亡?
java·jvm·算法
大油头儿2 小时前
排序算法-冒泡排序
数据结构·算法·排序算法
地平线开发者2 小时前
地平线占用预测 FlashOcc 参考算法-V1.0
算法·自动驾驶
LluckyYH2 小时前
代码随想录Day 46|动态规划完结,leetcode题目:647. 回文子串、516.最长回文子序列
数据结构·人工智能·算法·leetcode·动态规划
源代码:趴菜3 小时前
LeetCode118:杨辉三角
算法·leetcode·动态规划