【PAT】1030. Travel Plan (30)

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
分析:多條最短路,找出最優的最短路。和1018那道題目相似:http://blog.csdn.net/realxuejin/article/details/49453677
#include <iostream>
#include <vector>
using namespace std;
#define INF 999999
vector<vector<int> >cost;
vector<vector<int> >dist;
vector<bool> used;
vector<vector<int> > allPath;
int cityNum, wayNum, s, des;
vector<int> d;
vector<int> possiblePath;
vector<int> bestPath;
int minCost = INF;
void findBestPath(int u){ 
	possiblePath.push_back(u);
	if(u==s){
		int costTmp=0;
		int i;
		for(i=possiblePath.size()-1; i>0; i--){
			int from = possiblePath[i];
			int to = possiblePath[i-1];
			costTmp += cost[from][to];
		}		
		if(costTmp<minCost){
			minCost = costTmp;
			bestPath = possiblePath;
		}
		return;		
	}
	
	for(int i=0; i<allPath[u].size(); i++){
		findBestPath(allPath[u][i]);
		possiblePath.pop_back();
	}	
}

void dijkstra(int s){
	cost[s][s] = 0;
	allPath.resize(cityNum);	
	while(true){
		int v = -1;
		//從尚未使用過的頂點中選擇一個距離最小的頂點
		for(int u=0; u<cityNum; u++){
			if(!used[u] && (v==-1 || dist[s][u]<dist[s][v] ))
				v = u;
		} 
		if(v == -1)
		 	break;
		used[v] = true;
		for(int u=0; u<cityNum; u++){
			if(d[u] > d[v] + dist[v][u] ){
				allPath[u].clear();
				allPath[u].push_back(v);
				d[u] = d[v] + dist[v][u];
			}else if(d[u] == d[v] + dist[v][u]){
				allPath[u].push_back(v);
			}
		}
	}
	return;
}

int main(int argc, char** argv) {
	scanf("%d%d%d%d",&cityNum, &wayNum, &s, &des);
	used.resize(cityNum,false);
	int i;
	int city1, city2, distTmp, costTmp;
    cost.resize(cityNum, vector<int>(cityNum,INF) );
	dist.resize(cityNum, vector<int>(cityNum,INF));
	
	for(i=0; i<wayNum; i++){
		scanf("%d%d%d%d",&city1,&city2,&distTmp,&costTmp);
		dist[city1][city2] = dist[city2][city1] = distTmp;
		cost[city1][city2] = cost[city2][city1] = costTmp;		
	}
	d.resize(cityNum,INF);
	d[s] = 0;
	for(i=0; i<cityNum; i++){
		if(dist[s][i] != INF){
			d[i] = dist[s][i];
		}
	}
	
	dijkstra(s);
	findBestPath(des);
	
	for(i=bestPath.size()-1; i>=0; i--){
		printf("%d ",bestPath[i]);
	}
	printf("%d %d\n",d[des],minCost);
	
	return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章