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方法来处理,比较简单,我觉得两种方法掌握一种即可。

相关推荐
Doopny@5 分钟前
计算星期几(信息学奥赛一本通-1083)
数据结构·算法
xinxiangwangzhi_8 分钟前
多视图几何--恢复相机位姿/内参的几种方法
图像处理·算法·计算机视觉
lisanndesu44 分钟前
贪心算法--
算法·贪心算法
巷9551 小时前
机器学习篇——决策树基础
算法·决策树·机器学习
猎人everest1 小时前
机器学习之正则化
人工智能·算法·机器学习
YaoSolar1 小时前
刷题记录(LeetCode 78 子集)
算法·leetcode
熊峰峰1 小时前
数据结构第八节:红黑树(初阶)
开发语言·数据结构·c++·算法
f狐0狸x2 小时前
【蓝桥杯每日一题】3.8
数据结构·c++·算法·蓝桥杯
阿巴~阿巴~2 小时前
动态规划填表技巧:固定最后一个数 vs 固定倒数第二个数
c++·算法·动态规划
我感觉。2 小时前
【机器学习chp12】半监督学习(自我训练+协同训练多视角学习+生成模型+半监督SVM+基于图的半监督算法+半监督聚类)
人工智能·算法·机器学习·半监督学习