Bus System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2724 Accepted Submission(s): 677
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 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.
题目大意:先给你好几段距离分别要收多少费用,超过某段距离就不能行走,然后再给你很多个点。问:给你任意两个点,这两个点要达到最少费用是多少? 由于是任意两点,那么用floyd最好。
需要注意:距离范围很大,INF要很大才行,要用到long long,写的时候要加上LL。
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690
代码:
#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <cmath>
using namespace std;
const long long INF = 99999999999LL; //注意!要加LL,不然会报错数据太大
const int N = 105;
int l1, l2, l3, l4, c1, c2, c3, c4;
long long map[N][N]; //距离可能会爆int,所以用long long
int place[N];
int n, m;
void init()
{
int i, j;
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
if(i == j) map[i][j] = 0;
else map[i][j] = INF;
}
void input()
{
int i, j, len;
scanf("%d%d%d%d%d%d%d%d", &l1, &l2, &l3, &l4, &c1, &c2, &c3, &c4);
scanf("%d %d", &n, &m);
init();
for(i = 1; i <= n; i++)
{
scanf("%d", &place[i]);
}
for(i = 1; i <= n; i++)
{
for(j = i+1; j <= n; j++)
{
len = abs(place[i] - place[j]);
if(0 < len && len <= l1) map[i][j] = map[j][i] = c1;
else if(l1 < len && len <= l2) map[i][j] = map[j][i] = c2;
else if(l2 < len && len <= l3) map[i][j] = map[j][i] = c3;
else if(l3 < len && len <= l4) map[i][j] = map[j][i] = c4;
}
}
}
void floyd() //这题绝对是用floyd方便
{
int i, j, k;
for(k = 1; k <= n; k++)
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
if(map[i][j] > map[i][k] + map[k][j])
map[i][j] = map[i][k] + map[k][j];
}
void output()
{
int ti, tj;
static int zz = 1;
printf("Case %d:\n", zz++);
while(m--)
{
scanf("%d %d", &ti, &tj);
if(map[ti][tj] != INF)
printf("The minimum cost between station %d and station %d is %I64d.\n", ti, tj, map[ti][tj]);
else
printf("Station %d and station %d are not attainable.\n", ti, tj);
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
input();
floyd();
output();
}
return 0;
}
分享到:
相关推荐
《最短路问题详解》 在计算机科学领域,最短路问题是一个经典的图论问题,其目标是寻找网络中两点间路径的最小成本。这个问题在众多应用中都有所体现,如交通规划、通信网络设计、社交网络分析等。在本篇内容中,...
DP(Dynamic Programming,动态规划)是一种非常重要的算法思想,在 ACM HDU 题目分类中,DP 问题占据了很大的比例。例如,1003 DP 经典问题,最大连续子段和;1024 经典 DP,最大 M 子段和;1025 经典 DP,最长递增...
"HDU最全ac代码"这个压缩包很可能是包含了在HDU平台上解题通过的完整源代码集合,旨在帮助学习者理解和掌握各种算法,提升编程解决问题的能力。 首先,我们来了解一下ACM/ICPC比赛。这是一项全球性的编程竞赛,参赛...
HDU(杭州电子科技大学在线评测系统)是一个深受程序员喜爱的在线编程练习平台,它提供了丰富的算法题目供用户挑战,帮助他们提升编程技能和算法理解能力。"hdu.rar_hdu"这个压缩包文件很可能是某位程序员整理的他在...
【标题】"HDU_2010.rar"是一个压缩包文件,其中包含了与"HDU 2010"相关的资源,特别是针对"HDU ACM20"比赛的编程题目。"hdu 2010"和"hdu 20"可能是该比赛的不同简称或分类,而"hdu acm20"可能指的是该赛事的第20届...
【标题】"HDU DP动态规划"涉及到的是在算法领域中的动态规划(Dynamic Programming,简称DP)技术,这是解决复杂问题的一种高效方法,尤其适用于有重叠子问题和最优子结构的问题。动态规划通常用于优化多阶段决策...
【标题】"HDU题目java实现"所涉及的知识点主要集中在使用Java编程语言解决杭州电子科技大学(HDU)在线评测系统中的算法问题。HDU是一个知名的在线编程竞赛平台,它提供了大量的算法题目供参赛者练习和提交解决方案...
本题(hdu1250)主要考察的就是如何通过编程实现高精度加法,并解决一个特定的数学问题。 #### 题目解析 根据题目描述,该题目编号为HDU1250,其核心在于利用高精度加法解决问题。具体地,题目涉及到了斐波那契数列...
2. **动态规划**:解决许多具有重叠子问题和最优子结构的问题,如背包问题、最长公共子序列等。 3. **贪心算法**:在每一步选择局部最优解,以期达到全局最优,如活动安排问题、霍夫曼编码等。 4. **数据结构**:...
8. **C++编程**:虽然现在ACM竞赛支持多种编程语言,但C++是最常用的语言,其模板、STL库(容器、算法)等特性在解决ACM问题中非常实用。 9. **调试技巧**:学会使用调试工具(如gdb),理解并处理运行时错误(如段...
其中包含了一个名为 "HDU 1089.cpp" 的源代码文件,这个文件是为了解决杭州电子科技大学(Hangzhou Dianzi University,简称 HDU)在线判题系统(Online Judge,简称 OJ)上的一个问题。具体来说,这个问题是关于 "a...
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
3. **动态规划**:解决具有重叠子问题和最优子结构的问题。 4. **图论算法**:最短路径算法(Dijkstra、Floyd-Warshall)、拓扑排序、最小生成树(Prim、Kruskal)等。 5. **数学方法**:模运算、数论、组合数学等。...
模拟竞赛是提高实战能力的有效方式,通过参与在线判题平台(如HDU OJ)的练习,可以锻炼快速阅读题目、理解和解决问题的能力。同时,ACM竞赛强调团队协作,学习如何与队友有效沟通,分工合作,共同解决问题,也是...
3. **动态规划**:一种通过将问题分解为重叠子问题来寻找最优解的方法,广泛应用于优化问题。 4. **贪心算法**:在每一步选择中都采取当前看来最好的选择,希望这样能够达到全局最优解。 5. **图论算法**:包括深度...
动态规划是计算机科学中一种强大的问题解决方法,尤其在处理最优化问题时非常有效。在这个教案中,我们将深入探讨动态规划的基本概念、应用场景以及如何构建解决方案。 动态规划是一种通过将复杂问题分解为相互重叠...
在IT领域的编程竞赛中,HDU(HaoDong University)OJ(Online Judge)是一个备受推崇的在线编程平台,提供了大量的算法问题供参赛者挑战和学习。根据给定文件的信息,我们可以深入探讨HDU ACM题目分类中的几个关键...
hdu2101AC代码