题目链接: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 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.
 
记得上次说不和大神一起玩耍了,这次破例和他一起愉快的a了一题,看来和他还是蛮适合一起a题的,(*^__^*) 嘻嘻……
题目大意:
这题的这个是先给出了一个表格,这个表格也就是路程和花费的模板,然后根据这个对下面的问题进行解决,然后第三行给的是n,m,紧接着就是n行,表示的是0到1的距离,0到2的距离,0到3的距离。。。。依次下去。接下来的m行表示的就是要求的起点和终点了,哇哈哈~
解题思路:
一个dijkstra的变形就好了,不过不引用map还是这位大神说的,还有一个更奇葩的就是这位竟然用哈希,来记录路和花费的列表,一看数据1,000,000,000.顿时无奈,被我改成了4个if的判断,还是直接点好~~
 
 
最后还有一个要注意的,我贡献了一次wa,这里要用__int64,还有如果wa了改成const __int64 inf=0xffffffffffffff;就可以ac了!!
 
详见代码。
 
#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--)
{
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);
}
}
}

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

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

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

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

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

  3. HDU 1690 Bus System

    题目大意:给出若干巴士不同价格的票的乘坐距离范围,现在有N个站点,有M次询问,查询任意两个站点的最小花费 解析:由于是多次查询不同站点的最小花费,所以用弗洛伊德求解 时间复杂度(O^3) 比较基础的弗 ...

  4. HDU ACM 1690 Bus System (SPFA)

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

  5. hdu1690 Bus System(最短路 Dijkstra)

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

  6. hdu1690 Bus System (dijkstra)

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

  7. hdu 2377 Bus Pass

    Bus Pass Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  8. Dijkstra最短路算法

    Dijkstra最短路算法 --转自啊哈磊[坐在马桶上看算法]算法7:Dijkstra最短路算法 上节我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最 ...

  9. dijkstra(最短路)和Prim(最小生成树)下的堆优化

    dijkstra(最短路)和Prim(最小生成树)下的堆优化 最小堆: down(i)[向下调整]:从第k层的点i开始向下操作,第k层的点与第k+1层的点(如果有)进行值大小的判断,如果父节点的值大于 ...

随机推荐

  1. tcp发送缓冲区中的数据都是由产生数据的进程给推送到ip层还是有定时任务触发?

    和几个变量有非常大的关系 发送缓冲区的大小,如何单独设置一个socket的发送缓冲区 socketopt 发送缓冲区中的数据,如果被拥塞窗口限制住了,那么这些数据可能就放在tcpbuffer里的,此时 ...

  2. perf record -c

    如果perf record -c -c后面接的是sample_period,也就是说你让这个事件没 我的loop进程一直在执行,我的CPU的频率是2.6G hz,也就是说每一秒会有2,600,000, ...

  3. BZOJ 1079 着色方案(DP)

    如果把当前格子涂什么颜色当做转移的话,状态则是每个格子的颜色数还剩多少,以及上一步用了什么颜色,这样的状态量显然是5^15.不可取. 如果把当前格子涂颜色数还剩几个的颜色作为转移的话,状态则是每个格子 ...

  4. BZOJ4871 Shoi2017摧毁“树状图”(树形dp)

    设f[i][0/1/2/3/4/5]表示i子树中选一条链不包含根/i子树中选一条链包含根但不能继续向上延伸/i子树中选一条链可以继续向上延伸/选两条链不包含根/选两条链包含根但不能继续向上延伸/选两条 ...

  5. Oracle数据库中心双活之道:ASM vs VPLEX

    Oracle数据库中心双活之道:ASM vs VPLEX 来源 https://www.cnblogs.com/wenjiewang/p/7460212.html 双活方案对比:ASM vs V-PL ...

  6. Springboot @Transactional 事务不回滚

    一.异常捕获的原因 这里Exception异常,他又分为运行时异常RuntimeException和非运行时异常 可查的异常(checked exceptions):Exception下除了Runti ...

  7. 【刷题】BZOJ 1003 [ZJOI2006]物流运输

    Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...

  8. PKUWC2019 酱油记

    目录 PKUWC2019 酱油记 day0 Day1 Day2 Day3 Day4 PKUWC2019 酱油记 day0 早上从镇中出发到栎社机场,然后才了解到原来充电宝电脑是必须随身(原以为必须托运 ...

  9. bzoj 1103: [POI2007]大都市meg (dfs序)

    dfs序,加个bit维护前缀和就行了 type arr=record toward,next:longint; end; const maxn=; var edge:..maxn]of arr; bi ...

  10. SpringBoot-配置文件属性注入-3种方式

    配置文件: datasource.username = admin datasource.url = /hello/world 方式一: @Value 前提: <!-- JavaBean处理工具 ...