Bus System 【dijkstra算法】

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.

来源: http://acm.hdu.edu.cn/showproblem.php?pid=1690

大意:给出里程计费参数 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;
}

  

时间: 2024-08-08 05:19:22

Bus System 【dijkstra算法】的相关文章

hdu 1690 Bus System(Dijkstra最短路)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690 Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6569    Accepted Submission(s): 1692 Problem Description Because of the huge popula

hdu1690 Bus System(最短路 Dijkstra)

Problem Description 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 sys

hdu 1690 Bus System 最短路 Floyd算法。。INF一定要很大很大。。。数据类型用long long。

Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7163    Accepted Submission(s): 1853 Problem Description Because of the huge population of China, public transportation is very import

POJ 1511 Invitation Cards 【最短路,spfa算法,Dijkstra算法堆优化】

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 25219   Accepted: 8346 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They wan

hdu 1960 Bus System

Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6166    Accepted Submission(s): 1580 Problem Description Because of the huge population of China, public transportation is very importa

数据结构:单源最短路径--Dijkstra算法

Dijkstra算法 单源最短路径 给定一带权图,图中每条边的权值是非负的,代表着两顶点之间的距离.指定图中的一顶点为源点,找出源点到其它顶点的最短路径和其长度的问题,即是单源最短路径问题. Dijkstra算法 求解单源最短路径问题的常用方法是Dijkstra(迪杰斯特拉)算法.该算法使用的是贪心策略:每次都找出剩余顶点中与源点距离最近的一个顶点. 算法思想 带权图G=<V,E>,令S为已确定了最短路径顶点的集合,则可用V-S表示剩余未确定最短路径顶点的集合.假设V0是源点,则初始 S={V

Dijkstra算法(三)之 Java详解

前面分别通过C和C++实现了迪杰斯特拉算法,本文介绍迪杰斯特拉算法的Java实现. 目录 1. 迪杰斯特拉算法介绍 2. 迪杰斯特拉算法图解 3. 迪杰斯特拉算法的代码说明 4. 迪杰斯特拉算法的源码 转载请注明出处:http://www.cnblogs.com/skywang12345/ 更多内容:数据结构与算法系列 目录 迪杰斯特拉算法介绍 迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个节点到其他节点的最短路径. 它的主要特点是以起始点为中心向外层层扩展(广度优先搜索思想

Java数据结构 最短路径解法Dijkstra算法

本文为博主原创文章,未经博主允许不得转载! 1.1.定义概览Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Dijkstra算法是很有代表性的最短路径算法,在很多专业课程中都作为基本内容有详细的介绍,如数据结构,图论,运筹学等等.注意该算法要求图中不存在负权边. 问题描述:在无向图 G=(V,E) 中,假设每条边 E[i] 的长度为 w[i],找到由顶点 V0 到其余各点的最短路径.(

hdu 1690 Bus System (最短路径)

Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6082    Accepted Submission(s): 1560 Problem Description Because of the huge population of China, public transportation is very importa