1030 Travel Plan

news/2024/7/15 20:17:01 标签: 最短路径, 图论

题目来源:PAT (Advanced Level) Practice

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

words:

destination 目的地        separated 分开的        Travel 旅行

题意:

给出一个n个点和m条边的带权无向图,求起始点S到目的点D的最短路径及该路径的花费(若不唯一则选择花费最小的);

思路:

1. 使用邻接矩阵存储无向图;

2. 使用Dijkstra算法求最短路径,在求的过程中,在路径相等时选择花费最少的;

//PAT ad 1030 Travel Plan
#include <iostream>
#define N 505
using namespace std;
#include <vector>
#include <set>
#define MaxInt 65535

int n,m,s,d;
int dis[N]; //最短路径
int co[N];    //最小花费
int path[N];    //前驱
int vis[N];    //访问数组

void print_path(int s,int d)    //回溯路径
{
	if(path[d]==s)
		cout<<s<<" ";
	else
	{
		print_path(s,path[d]);
		cout<<path[d]<<" ";
	}
		
}


int main()
{
	cin>>n>>m>>s>>d;
	vector<vector<pair<int,int> > > adj(n,vector<pair<int,int> >(n,pair<int,int> (MaxInt,MaxInt)));  //邻接矩阵 
	int i,c1,c2,distance,cost;
	for(i=0;i<m;i++)    //建立邻接矩阵 
	{
		cin>>c1>>c2>>distance>>cost;
		adj[c1][c2].first=adj[c2][c1].first=distance;    //distance距离
		adj[c1][c2].second=adj[c2][c1].second=cost;      //cost花费
	}
	int v; 
    //Dijkstra算法开始了
	for(i=0;i<n;i++)   //做标记,定前驱 
	{
		vis[i]=false;
		dis[i]=adj[s][i].first;  //距离 
		co[i]=adj[s][i].second;  	//花费 
		if(adj[s][i].first<MaxInt)	path[i]=s;
		else	path[i]=-1;
	}
	vis[s]=true;
	for(i=0;i<n-1;i++) 	//	n-1次循环
	{
		int  mi1=MaxInt;
		int  mi2=MaxInt;
		for(int w=0;w<n;w++)   //找最短 
		{
			if(!vis[w]&&dis[w]<mi1)
			{
				v=w;mi1=dis[w];mi2=co[w];
			}
			else if(!vis[w]&&(dis[w]==mi1&&dis[w]<mi2))
			{
				v=w;mi1=dis[w];mi2=co[w];
			}
		}
			
		vis[v]=true;
		
		for(int w=0;w<n;w++)
		{
			if(!vis[w]&&(dis[v]+adj[v][w].first)<dis[w])  //间接距离小于直接距离 
			{
				dis[w]=dis[v]+adj[v][w].first;	//做更新 ,更新距离 
				co[w]=co[v]+adj[v][w].second;
				path[w]=v;
			}
			else if(!vis[w]&&(dis[v]+adj[v][w].first)==dis[w]&&(co[v]+adj[v][w].second)<co[w])  //间接距离小于直接距离 
			{
				dis[w]=dis[v]+adj[v][w].first;	//做更新 ,更新距离 
				co[w]=co[v]+adj[v][w].second;
				path[w]=v;
			}
		}
			
			
	 } 
	
	print_path(s,d);	//打印最短路劲 
	cout<<d<<" "<<dis[d]<<" "<<co[d]<<endl;

	 
	
	return 0;
}


http://www.niftyadmin.cn/n/1165094.html

相关文章

1001 A+B Format

题目来源&#xff1a;PAT (Advanced Level) Practice Calculate ab and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). Input Specification: Each input file c…

1132 Cut Integer

题目来源&#xff1a;PAT (Advanced Level) Practice Cutting an integer means to cut a K digits lone integer Z into two integers of (K/2) digits long integers A and B. For example, after cutting Z 167334, we have A 167 and B 334. It is interesting to see …

1017 Queueing at Bank

题目来源&#xff1a;PAT (Advanced Level) Practice Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, u…

1014 Waiting in Line

题目来源&#xff1a;PAT (Advanced Level) Practice Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are: The space insi…

1055 The World‘s Richest

题目来源&#xff1a;PAT (Advanced Level) Practice Forbes magazine publishes every year its list of billionaires based on the annual ranking of the worlds wealthiest people. Now you are supposed to simulate this job, but concentrate only on the people in a…

1100 Mars Numbers

题目来源&#xff1a;PAT (Advanced Level) Practice People on Mars count their numbers with base 13: Zero on Earth is called "tret" on Mars.The numbers 1 to 12 on Earth is called "jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec&qu…

1032 Sharing

题目来源&#xff1a;PAT (Advanced Level) Practice To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, load…

1097 Deduplication on a Linked List

题目来源&#xff1a;PAT (Advanced Level) Practice Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or absolu…