码迷,mamicode.com
首页 > 编程语言 > 详细

Bus System 【dijkstra算法】

时间:2017-03-27 23:54:39      阅读:396      评论:0      收藏:0      [点我收藏+]

标签:ret   question   alt   from   blog   mat   which   rom   log   

Because of the huge population of China, public transportation is very important. Bus is an important transportation method in traditional public transportation system. And it’s still playing an important role even now.
The bus system of City X is quite strange. Unlike other city’s system, the cost of ticket is calculated based on the distance between the two stations. Here is a list which describes the relationship between the distance and the cost.

技术分享

 



Your neighbor is a person who is a really miser. He asked you to help him to calculate the minimum cost between the two stations he listed. Can you solve this problem for him?
To simplify this problem, you can assume that all the stations are located on a straight line. We use x-coordinates to describe the stations’ positions.
 

Input
The input consists of several test cases. There is a single number above all, the number of cases. There are no more than 20 cases.
Each case contains eight integers on the first line, which are L1, L2, L3, L4, C1, C2, C3, C4, each number is non-negative and not larger than 1,000,000,000. You can also assume that L1<=L2<=L3<=L4.
Two integers, n and m, are given next, representing the number of the stations and questions. Each of the next n lines contains one integer, representing the x-coordinate of the ith station. Each of the next m lines contains two integers, representing the start point and the destination.
In all of the questions, the start point will be different from the destination.
For each case,2<=N<=100,0<=M<=500, each x-coordinate is between -1,000,000,000 and 1,000,000,000, and no two x-coordinates will have the same value.
 

Output
For each question, if the two stations are attainable, print the minimum cost between them. Otherwise, print “Station X and station Y are not attainable.” Use the format in the sample.
 

Sample Input
2
1 2 3 4 1 3 5 7
4 2
1
2
3
4
1 4
4 1
1 2 3 4 1 3 5 7
4 1
1
2
3
10
1 4

Sample Output
Case 1:
The minimum cost between station 1 and station 4 is 3.
The minimum cost between station 4 and station 1 is 3.
Case 2:
Station 1 and station 4 are not attainable.


 
 
大意:给出里程计费参数 L1, L2, L3, L4, C1, C2, C3, C4,给出每个站口在x轴上离原点的距离,问从起点站口到终点站口的最少花费?
 
题解:这道题并非典型的最短路问题,只是使用了dijkstra计算点到点距离的思想,并且把长度变成费用。

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0xffffffffffff
using namespace std;
__int64 dis[101];
__int64 vis[101];
__int64 map[101][101];
__int64 n;
void dijkstra(__int64 s){
	
	memset(vis,0,sizeof(vis));
	
	for(int i=0;i<n;i++){
		dis[i] = INF;
	}
	dis[s] = 0;
	__int64 min = INF;
	int u;
	for(int i=0;i<n-1;i++){
		min = INF;
		for(int j=0;j<n;j++){
			if(!vis[j] && dis[j] < min){
				min = dis[j];
				u = j;
			}
		} 
		if(min == INF){
			break;
		}
		vis[u] = 1;
		for(int j=0;j<n;j++){
			if(dis[j] > dis[u] + map[u][j]){
				dis[j] = dis[u] + map[u][j];
			}
		}
	}
	
}
int main(){
	int t;
	scanf("%d",&t);
	__int64 l1,l2,l3,l4,c1,c2,c3,c4,m;
	for(int q=0;q<t;q++){
		
		scanf("%I64d %I64d %I64d %I64d %I64d %I64d %I64d %I64d",&l1,&l2,&l3,&l4,&c1,&c2,&c3,&c4);
		scanf("%I64d %I64d",&n,&m);
		
		__int64 x[501];
		for(int i=0;i<n;i++){
			scanf("%I64d",&x[i]);
		}
		
		int tmp = 0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(i == j){
					map[i][j] = 0;
				}else{
					tmp = abs(x[i] - x[j]);
					if(tmp > 0 && tmp <= l1){
						map[i][j] = map[j][i] = c1;
					}else if(tmp > l1 && tmp <= l2){
						map[i][j] = map[j][i] = c2;
					}else if(tmp > l2 && tmp <= l3){
						map[i][j] = map[j][i] = c3;
					}else if(tmp > l3 && tmp <= l4){
						map[i][j] = map[j][i] = c4;
					}else if(tmp > l4){
						map[i][j] = map[j][i] = INF;
					}
				}
			}
		}
		__int64 s,e;
		printf("Case %d:\n",q+1);
		for(int i=0;i<m;i++){
			scanf("%I64d %I64d",&s,&e);
			dijkstra(s-1);
			if(dis[e-1] == INF){
				printf("Station %I64d and station %I64d are not attainable.\n",s,e);
			}else{
				printf("The minimum cost between station %I64d and station %I64d is %I64d.\n",s,e,dis[e-1]);
			}
		}
	}
	return 0;
} 

  

 

Bus System 【dijkstra算法】

标签:ret   question   alt   from   blog   mat   which   rom   log   

原文地址:http://www.cnblogs.com/redscarf/p/6629484.html

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