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.
 
 
题目大意:乘公交车的价格随公交站距离的远近有不同的标准,就是按照每个测试数据第一行的数字,接着是有n个站点有m个提问,接着n行,假设有个原点,所有站点在一条直线上,n行每个数字表示第i个站点距离原点的距离,m个提问,表示出发点和终点;
 
 
直接用Dijkastra就ok了 稍稍做一点点的变形,要注意的本题数据比较大 在定义最大值常量的时候要注意 一开始还WA了好多遍 结果定义成
const __int64 inf=0xffffffffffffff;
就过了,输入输出也要用__int64 !
 #include <iostream>
#include <cstdio>
using namespace std;
const __int64 inf=0xffffffffffffff;
__int64 dist[],node[],vis[];
__int64 l[],c[],n; __int64 ab(__int64 a)
{
return a>?a:-a;
}
__int64 cost(__int64 dis)
{
if (dis>=&&dis<=l[]) return c[];
if (dis>l[]&&dis<=l[]) return c[];
if (dis>l[]&&dis<=l[]) return c[];
if (dis>l[]&&dis<=l[]) return c[];
} void Dijkstra(__int64 start,__int64 end)
{
for(int i=; i<=n; i++)
node[i]=inf,vis[i]=;
__int64 tm=start;
node[tm]=;
vis[tm]=;
for(int k=; k<=n; k++)
{
__int64 Min=inf;
for (int i=; i<=n; i++)
if(!vis[i]&&Min>node[i])
{
Min=node[i];
tm=i;
//cout<<" "<<tm<<" "<<Min<<endl;
}
if(tm==end)
{
printf("The minimum cost between station %I64d and station %I64d is %I64d.\n",start,end,node[end]);
return ;
}
vis[tm]=;
for(int i=; i<=n; i++)
if(ab(dist[i]-dist[tm])<=l[]&&!vis[i]&&node[i]>node[tm]+cost(ab(dist[i]-dist[tm])))
{
//cout<<" "<<i<<" "<<node[tm]<<" "<<ab(dist[i]-dist[tm])<<" "<<hash[ab(dist[i]-dist[tm])]<<endl;
node[i]=node[tm]+cost(ab(dist[i]-dist[tm]));
}
}
printf ("Station %I64d and station %I64d are not attainable.\n",start,end);
} int main ()
{
int t,k=;
cin>>t;
while (t--)
{
//int l1,l2,l3,c1,c2,c3,c4;
cin>>l[]>>l[]>>l[]>>l[]>>c[]>>c[]>>c[]>>c[];
int m;
cin>>n>>m;
for(int i=; i<=n; i++)
cin>>dist[i];
printf ("Case %d:\n",k++);
while (m--)
{
int a,b;
cin>>a>>b;
Dijkstra(a,b);
}
}
}


 

hdu1690 Bus System(最短路 Dijkstra)的更多相关文章

  1. hdu1690 Bus System (dijkstra)

    Problem Description Because of the huge population of China, public transportation is very important ...

  2. hdu 1690 Bus System(Dijkstra最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690 Bus System Time Limit: 2000/1000 MS (Java/Others ...

  3. hdu 2544 最短路 Dijkstra

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...

  4. 算法学习笔记(三) 最短路 Dijkstra 和 Floyd 算法

    图论中一个经典问题就是求最短路.最为基础和最为经典的算法莫过于 Dijkstra 和 Floyd 算法,一个是贪心算法,一个是动态规划.这也是算法中的两大经典代表.用一个简单图在纸上一步一步演算,也是 ...

  5. HDU ACM 1690 Bus System (SPFA)

    Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. hdu 1690 Bus System (有点恶心)

    Problem Description Because of the huge population of China, public transportation is very important ...

  7. 单源最短路dijkstra算法&&优化史

    一下午都在学最短路dijkstra算法,总算是优化到了我能达到的水平的最快水准,然后列举一下我的优化历史,顺便总结总结 最朴素算法: 邻接矩阵存边+贪心||dp思想,几乎纯暴力,luoguTLE+ML ...

  8. hdu 1690 Bus System (最短路径)

    Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. HUD.2544 最短路 (Dijkstra)

    HUD.2544 最短路 (Dijkstra) 题意分析 1表示起点,n表示起点(或者颠倒过来也可以) 建立无向图 从n或者1跑dij即可. 代码总览 #include <bits/stdc++ ...

随机推荐

  1. J2EE和EJB有什么关系?

    其实j2EE就是java的企业版,与javaSE(标准版)有是有区别的,所以运行环境会有差异(简单点可以说,这是两组不同的接口),我们一般所熟悉的tomcat仅仅只实现了j2ee的一小部分规范,它只是 ...

  2. 16.缓存(Cache)

    如果每次进入页面的时候都查询数据库生成页面内容的话,如果访问量非常大,则网站性能会非常差.而如果只有第一次访问的时候才查询数据库生成页面内容,以后都直接输出内容,则能提高系统性能,这样无论有多少人访问 ...

  3. QTP关于AOM的Javascript启动方式

    序 QTP的AOM模型想必大家都很熟悉了,平时常用的就是通过VBS脚本的方式编写启动程序(也是我现在用的方法).其实,还有很多其他的方式,如Java,C#,JS,这些语言都是通过调用QTObjectM ...

  4. [LeetCode] 141&142 Linked List Cycle I & II

    Problem: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without ...

  5. B - The Suspects -poj 1611

    病毒扩散问题,SARS病毒最初感染了一个人就是0号可疑体,现在有N个学生,和M个团队,只要团队里面有一个是可疑体,那么整个团队都是可疑体,问最终有多少个人需要隔离... 再简单不过的并查集,只需要不断 ...

  6. poj 2031 Building a Space Station【最小生成树prime】【模板题】

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5699   Accepte ...

  7. MySQL的InnoDB表如何设计主键索引-转自淘宝MySQL经典案例

    创建a表 id主键 CREATE TABLE `a` (`id` bigint(20) NOT NULL AUTO_INCREMENT ,`message_id` int(11) NOT NULL,` ...

  8. Business Analysis and Essential Competencies

    Requirements Classification Schema http://files.cnblogs.com/files/happlyonline/BABOK.pptx http://fil ...

  9. Swoole源代码学习记录(十五)——Timer模块分析

    swoole版本号:1.7.7-stable Github地址:点此查看 1.Timer 1.1.swTimer_interval_node 声明: // swoole.h 1045-1050h ty ...

  10. TCP 连接的要点

    概念 TIME_WAIT: socket 仍然有数据在内核中待发送直到发送成功或超时,此socket不能被内核删除,同时等待是否要重传Ack对端还已发过来的FIN Linger Time:socket ...