hdu1690 Bus System(最短路 Dijkstra)
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.
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.
print the minimum cost between them. Otherwise, print “Station X and station Y
are not attainable.” Use the format in the sample.
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)的更多相关文章
- hdu1690 Bus System (dijkstra)
Problem Description Because of the huge population of China, public transportation is very important ...
- hdu 1690 Bus System(Dijkstra最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690 Bus System Time Limit: 2000/1000 MS (Java/Others ...
- hdu 2544 最短路 Dijkstra
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...
- 算法学习笔记(三) 最短路 Dijkstra 和 Floyd 算法
图论中一个经典问题就是求最短路.最为基础和最为经典的算法莫过于 Dijkstra 和 Floyd 算法,一个是贪心算法,一个是动态规划.这也是算法中的两大经典代表.用一个简单图在纸上一步一步演算,也是 ...
- HDU ACM 1690 Bus System (SPFA)
Bus System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 1690 Bus System (有点恶心)
Problem Description Because of the huge population of China, public transportation is very important ...
- 单源最短路dijkstra算法&&优化史
一下午都在学最短路dijkstra算法,总算是优化到了我能达到的水平的最快水准,然后列举一下我的优化历史,顺便总结总结 最朴素算法: 邻接矩阵存边+贪心||dp思想,几乎纯暴力,luoguTLE+ML ...
- hdu 1690 Bus System (最短路径)
Bus System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HUD.2544 最短路 (Dijkstra)
HUD.2544 最短路 (Dijkstra) 题意分析 1表示起点,n表示起点(或者颠倒过来也可以) 建立无向图 从n或者1跑dij即可. 代码总览 #include <bits/stdc++ ...
随机推荐
- 23个经典JDK设计模式(转)
下面是JDK中有关23个经典设计模式的示例: Structural(结构模式) Adapter: 把一个接口或是类变成另外一种. o ● java.util.Arrays#asList() o ...
- python operator模块
官网:https://docs.python.org/3.4/library/operator.html http://pymotw.com/2/operator/
- oralce 仅配置精简客户端 连接plsql ( 版本需一直,要不都是32要不是都是64)
1.Oracle服务器已经安装完成,版本10.2.0. 2.访问www.oracle.com,下载Oracle精简客户端. 下载页面地址:http://www.oracle.com/technetwo ...
- A Simple problem
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2497 题意:给出顶点数,边数及节点s,判断s是 ...
- 新的一年新的气象 云计算与SOA
[David S.Linthicum]著 云计算和SOA是不同的概念,但是它们却相互联系.SOA是架构模式,而云计算是架构的实例,或者说是架构的一种选择,SOA更具整体性和战略性,它解决的是包括业务驱 ...
- FreeMarker-TemplateLoader
Java中不乏优秀的模板引擎,Velocity,mvel,FreeMarker等.在构建框架的时候,通常可以拿来即用,但我们需要控制它.最近需要一个数据准备的框架,便选择了FreeMarker,Fre ...
- Best Reward HDU 3613(回文子串Manacher)
题目大意:有一个串(全部由小写字母组成),现在要把它分成两部分,如果分开后的部分是回文串就计算出来它的价值总和,如果不是回文的那么价值就是0,最多能得到的最大价值. 分析:首先的明白这个最大价值有 ...
- 网络编程之TCP异步群聊:服务器端代码
最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档.于是我也觉的有这个必要.写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博客 ...
- Cocos2d-iphone 为sprite添加双击的事件响应
这篇文章介绍两种方式处理cocos2d中的双击事件响应. 在iOS中使用UITapGestureRecognizer ,很容易就可以添加双击事件处理,但是在cocos2d中无法直接向sprite添加U ...
- C#修饰符
声明类的顺序: 访问修饰符+类修饰符 +class+类名 { 成员修饰符+ 成员类型 +成员名称; } C#中类及类型成员权限访问修饰符有以下四类:public,private,protected,i ...