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.
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.
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
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.
#include<stdio.h>
const long long INF = 99999999999LL;
__int64 map[105][105],node[105];
int n,s[105];
__int64 abs(__int64 a)
{
return a>0?a:-a;
}
void set_1()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
map[i][j]=INF;
}
}
void set_2()
{
for(int i=1;i<=n;i++)
{
s[i]=0; node[i]=INF;
}
}
__int64 dijkstra(int stra,int end)
{
int tstra=stra;
__int64 min;
s[stra]=1; node[stra]=0;
for(int k=2;k<=n;k++)
{
min=INF;
for(int i=1;i<=n;i++)
if(s[i]==0)
{
if(node[i]>map[tstra][i]+node[tstra])
node[i]=map[tstra][i]+node[tstra];
if(min>node[i])
{
min=node[i]; stra=i;
}
}
s[stra]=1; tstra=stra;
if(s[end])
break;
}
if(node[end]==INF)
return -1;
return node[end];
}
int main()
{
int t,m,k=1,cas[505][2];
__int64 l[5],c[5],posit[105],dist;
scanf("%d",&t);
while(t--)
{
for(int i=1;i<=4;i++)
scanf("%I64d",&l[i]);
for(int i=1;i<=4;i++)
scanf("%I64d",&c[i]);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%I64d",&posit[i]); set_1();
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
{
dist=abs(posit[i]-posit[j]);
if(dist<=l[1])
map[i][j]=map[j][i]=c[1];
else if(dist<=l[2])
map[i][j]=map[j][i]=c[2];
else if(dist<=l[3])
map[i][j]=map[j][i]=c[3];
else if(dist<=l[4])
map[i][j]=map[j][i]=c[4];
} printf("Case %d:\n",k++);
__int64 cost;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&cas[i][0],&cas[i][1]); set_2();
cost=dijkstra(cas[i][0],cas[i][1]);
if(cost!=-1)
printf("The minimum cost between station %d and station %d is %I64d.\n",cas[i][0],cas[i][1],cost);
else
printf("Station %d and station %d are not attainable.\n",cas[i][0],cas[i][1]);
}
}
}
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 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 ...
- hdu 1690 Bus System (最短路径)
Bus System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 1690 Bus System
题目大意:给出若干巴士不同价格的票的乘坐距离范围,现在有N个站点,有M次询问,查询任意两个站点的最小花费 解析:由于是多次查询不同站点的最小花费,所以用弗洛伊德求解 时间复杂度(O^3) 比较基础的弗 ...
- Bus System(Flody)
http://acm.hdu.edu.cn/showproblem.php?pid=1690 坑爹的题,必须用__int64 %I64d(以前没用过) 因为这题的数据特别大,所以用-1 #includ ...
- Chrysler -- CCD (Chrysler Collision Detection) Data Bus
http://articles.mopar1973man.com/general-cummins/34-engine-system/81-ccd-data-bus CCD (Chrysler Coll ...
- Bus Pass
ZOJ Problem Set - 2913 Bus Pass Time Limit: 5 Seconds Memory Limit: 32768 KB You travel a lot b ...
随机推荐
- [放松一下] 经典高清电影合集 170G BT种子下载
经典高清电影合集 170G BT种子下载 点击文件名下载 经典高清电影合集170G BT种子.torrent 下载方法 经典高清电影合集详情见目录: 1. 杀手47 2. 这个杀手不太冷 3. 放牛班 ...
- sql: update from
sql server提供了update的from 子句,可以将要更新的表与其它的数据源连接起来.虽然只能对一个表进行更新,但是通过将要更新的表与其它的数据源连接起来,就可以在update的表达式 中引 ...
- windows 2003 域控制器(AD)的常规命令行操作以及修复
查询服务器的角色 Netdom query fsmo 强制升级操作主机角色(如果两台DC都无损,可以直接用图形模式传送,这里指的是一台DC出问题,另一台强制升级占用角色的情况) Ntdsutil Ro ...
- 14.10.4 Defragmenting a Table 整理表
14.10.4 Defragmenting a Table 整理表: 随机插入或者删除从一个secondary index 可以导致index变的fragmented Fragmentation意味着 ...
- Godiva_百度百科
Godiva_百度百科 北京 三里屯 北京市朝阳区三里屯路19号院10号楼一层S10-13单元及二层S10-22单元 100027 北京朝阳大悦城北京市朝阳区朝阳北路101号朝阳大悦城1号商业楼1F- ...
- yii Query Builder (yii 查询构造器) 官方指南翻译
/**** Query Builder translated by php攻城师 http://blog.csdn.net/phpgcs Preparing Query Builder 准备 Quer ...
- 九度OJ 1065 输出梯形 (模拟)
题目1065:输出梯形 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3745 解决:2043 题目描写叙述: 输入一个高度h.输出一个高为h.上底边为h的梯形. 输入: 一个整数h(1& ...
- UML之九图概述
最近看了UML的九种图的讲解,这九种图在我们以后的学习中起着举足轻重的作用,不管是在写文档,还是在对系统的需求.设计进行分析时,都很重要,所以首先做一下概述,希望能和大家分享. 首先和大家展示一下我对 ...
- C++ overloading contructor
// overloading class constructors #include <iostream> using namespace std; class Rectangle ...
- 轻量级工具网站SimpleTools
[解释]本来这篇文章是在前天发出来的,可是当时是刚申请的域名,现在都要域名实名认证,导致我发的项目网址打不开,惹来了很多博友的吐槽,在此说声抱歉,今天一大早就把实名认证提交了,现在网站已经可以正常访问 ...