码迷,mamicode.com
首页 > 其他好文 > 详细

1030. Travel Plan (30)

时间:2017-10-19 19:42:01      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:ack   can   uniq   pre   ring   contains   des   using   number   

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

题目大意:求带权最短路,如果最短路有多种情况,则选择花费最少的情况。

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
#define max 502
#define INF 0x6ffffff
int map[max][max];
vector<int>ShortPath;
vector<int>CurPath;
int curCost=0;
int curPath=0;
int minCost=INF;
int minPath=INF;
int n,m,s,d;
int cost[max][max];
int visit[max];
void DFS(int sd){
	if(minPath < curPath)return;
	if(sd == d){
		if(minPath>curPath){
			minPath=curPath;
			ShortPath=CurPath;
			minCost=curCost;
		}else if(minPath == curPath && minCost>curCost){
			ShortPath=CurPath;
			minCost=curCost;
		}
		return ;
	}
	int i;
	for(i=0;i<n;i++){
		if(visit[i]==0&&map[sd][i]!=INF){
			int c=curCost;
			int p=curPath;
			curCost+=cost[sd][i];
			curPath+=map[sd][i];
			CurPath.push_back(i);
			visit[i]=1;
			DFS(i);
			CurPath.pop_back();
			curCost=c;
			curPath=p;
			visit[i]=0;
		}
	}
}
int main(){
	memset(visit,0,sizeof(visit));
	scanf("%d%d%d%d",&n,&m,&s,&d);
	int i,j;
	int c1,c2,d12,c12;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			map[i][j]=INF;
		}
	}
	for(i=0;i<m;i++){
		scanf("%d%d%d%d",&c1,&c2,&d12,&c12);
		map[c1][c2]=map[c2][c1]=d12;
		cost[c1][c2]=cost[c2][c1]=c12;
	}
	visit[s]=1;
	DFS(s);
	printf("%d ",s);
	int length=ShortPath.size();
	for(i=0;i<length;i++){
		printf("%d ",ShortPath[i]);
	}
	printf("%d %d\n",minPath,minCost);
	return 0;
}

  



1030. Travel Plan (30)

标签:ack   can   uniq   pre   ring   contains   des   using   number   

原文地址:http://www.cnblogs.com/grglym/p/7694088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!