【PAT】A1030 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

第一次盲写dijkstra,还是有边界条件没有考虑到

1)初始时,将起点的distance设置为0;

2)O(N^2)的时间复杂度

3)在一层循环的内部

  • 循环A 查找未访问过,且distance<INF_MAX的节点
  • 在这些节点中找到最小distance的节点,记录更新最小distance,注意此时还不涉及节点之间的距离
  • 如果没有找到这样的点,则将select设置为-1,即已经处理完毕了,所有的节点都处理完毕,返回
  • 设置select visit =true
  • 循环B 查找,以当前select节点为中心,更新所有与其相接未访问的节点的距离

   那么,初始节点是怎么引入的呢?将初始节点对应的distance元素设置为0,那这样初始节点就是最小的了,必然在循环中,以此节点开始。

#include <iostream>
#include <vector>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
const int CITY_NUM=500;
const int INF = 100000000;
int city_num,road_num,start_city,end_city;
int city_dis[CITY_NUM][CITY_NUM];
int city_cost[CITY_NUM][CITY_NUM];
vector<int> city_pre[CITY_NUM];
int city_path[CITY_NUM];
bool city_visit[CITY_NUM]={false};


int cost_sum = INF;
vector<int> final_path,path;

void init(){
	for(int i =0;i<CITY_NUM;i++){
		for(int j=0;j<CITY_NUM;j++){
			city_dis[i][j]=INF;
			city_dis[j][i]=INF;
		}
	}
	fill(city_path,city_path+CITY_NUM,INF);
}

void dijkstra(int start_city){
	int city_id = start_city;
	int vis_id	= start_city;
	city_path[start_city]=0;
	int min_dis = INF;
	for(int k=0;k<city_num;k++){
		min_dis = INF;
		vis_id = -1;
		for(int i=0;i<city_num;i++){
			if ((city_path[i]<INF)&&(!city_visit[i])){
				if(min_dis>city_path[i]){
					vis_id = i;
					min_dis=city_path[i];
				}
			}
		}
		if(vis_id == -1) return;
		city_visit [vis_id]=true;

		for(int j=0;j<city_num;j++){
			if ((city_dis[vis_id][j]<INF)&&(!city_visit[j])){
				if((city_dis[vis_id][j]+city_path[vis_id])==city_path[j]){
					city_pre[j].push_back(vis_id);
				}else if((city_dis[vis_id][j]+city_path[vis_id])<city_path[j]){
					city_pre[j].clear();
					city_pre[j].push_back(vis_id);
					city_path[j]=city_dis[vis_id][j]+city_path[vis_id];
				}
			}
		}
	}	
}


void DFS(int city_id){
	if (city_id==start_city){
		path.push_back(city_id);
		int cost_sum_tmp=0;
		for(int i = path.size()-1;i>0;i--){
			int city_id_cur = path[i];
			int city_id_pre = path[i-1];
			cost_sum_tmp +=city_cost[city_id_pre][city_id_cur];
			//cout<<city_id_cur<<" "<<endl; 
		}
		if(cost_sum_tmp<cost_sum){
			final_path = path;
			cost_sum = cost_sum_tmp;
		}
		path.pop_back();
	}
	
	path.push_back(city_id);
	for(int i=0;i<city_pre[city_id].size();i++){
		DFS(city_pre[city_id][i]);
	}
	path.pop_back();
	 
}

int main(int argc, char** argv) {
	
	
	cin>>city_num>>road_num>>start_city>>end_city;
	
	int city0,city1,dis,cost;
	init();
	
	while(road_num--){
		cin>>city0>>city1>>dis>>cost;
		city_dis[city0][city1]=dis;
		city_dis[city1][city0]=dis;
		city_cost[city0][city1]=cost;
		city_cost[city1][city0]=cost;
	}

	dijkstra(start_city);
	DFS(end_city);
	

	for(int i=final_path.size()-1;i>=0;i--){
		cout<<final_path[i]<<" ";
	}
	cout<<city_path[end_city]<<" "<<cost_sum<<endl;

	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值