HDU-1690-Bus System
2016-05-01 14:08
363 查看
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.
题意:第一行输入T,代表T组数据。每组数据第一行给出L1~L4,C1~C4八个数,第二行给出N和M代表有N个站点,M此询问。接着N行,每行给出一个xi,代表第i个站台在数轴上的坐标为xi。接着M行,每行两个数字xi,yi,询问两点间的最小花费,输出方式参考样例。两点间的花费由两点的距离决定,公式参考图表
思路:用邻接矩阵存图,距离转换成花费后,将花费作为权值。一遍弗洛伊德,后面查询时可以O(1)输出,也可以用迪杰斯特拉。
坑点:坐标xi可以达到十亿,所以我把INF设为了一千亿,数据类型选用I64d。建图时注意,这是个无向图.
弗洛伊德代码(46ms)
来自网络的迪杰斯特拉代码(421ms)
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.
题意:第一行输入T,代表T组数据。每组数据第一行给出L1~L4,C1~C4八个数,第二行给出N和M代表有N个站点,M此询问。接着N行,每行给出一个xi,代表第i个站台在数轴上的坐标为xi。接着M行,每行两个数字xi,yi,询问两点间的最小花费,输出方式参考样例。两点间的花费由两点的距离决定,公式参考图表
思路:用邻接矩阵存图,距离转换成花费后,将花费作为权值。一遍弗洛伊德,后面查询时可以O(1)输出,也可以用迪杰斯特拉。
坑点:坐标xi可以达到十亿,所以我把INF设为了一千亿,数据类型选用I64d。建图时注意,这是个无向图.
弗洛伊德代码(46ms)
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<queue> using namespace std; const int maxn=105; const long long int INF=999999999999;//可能有点小 long long int map[maxn][maxn];//邻接矩阵存图 int N;//图的大小 int M;//询问次数 long long int L1,L2,L3,L4,C1,C2,C3,C4;//含义如题 long long int change(long long int dist) { if(dist>0&&dist<=L1) return C1; if(dist>L1&&dist<=L2) return C2; if(dist>L2&&dist<=L3) return C3; if(dist>L3&&dist<=L4) return C4; return INF;//超过L4返回正无穷 } int main() { int T;//数组组数 scanf("%d",&T); for(int casen=1; casen<=T; casen++) { scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&L1,&L2,&L3,&L4,&C1,&C2,&C3,&C4); scanf("%d%d",&N,&M); for(int i=0; i<=N; i++) //初始化图 for(int j=0; j<=N; j++) i==j?map[i][j]=0:map[i][j]=INF; long long int num[maxn];//存储第i个站点的坐标 for(int i=1; i<=N; i++) { scanf("%I64d",&num[i]);//接收第i个站点的坐标 for(int j=1; j<i; j++) //建图 map[i][j]=map[j][i]=change(abs(num[i]-num[j])); } for(int k=1; k<=N; k++) for(int i=1; i<=N; i++) for(int j=1; j<=N; j++) if(map[i][k]<INF&&map[k][j]<INF&&map[i][j]>map[i][k]+map[k][j]) map[i][j]=map[i][k]+map[k][j]; printf("Case %d:\n",casen); while(M--)//O(1)给出答案 { long long int star,end; scanf("%I64d%I64d",&star,&end); map[star][end]==INF?printf("Station %I64d and station %I64d are not attainable.\n",star,end):printf("The minimum cost between station %I64d and station %I64d is %I64d.\n",star,end,map[star][end]); } } return 0; }
来自网络的迪杰斯特拉代码(421ms)
#include <iostream> #include <cstdio> using namespace std; const __int64 inf=0xffffffffffffff; __int64 dist[105],node[105],vis[105]; __int64 l[5],c[5],n; __int64 ab(__int64 a) { return a>0?a:-a; } __int64 cost(__int64 dis) { if (dis>=0&&dis<=l[1]) return c[1]; if (dis>l[1]&&dis<=l[2]) return c[2]; if (dis>l[2]&&dis<=l[3]) return c[3]; if (dis>l[3]&&dis<=l[4]) return c[4]; } void Dijkstra(__int64 start,__int64 end) { for(int i=1; i<=n; i++) node[i]=inf,vis[i]=0; __int64 tm=start; node[tm]=0; vis[tm]=1; for(int k=1; k<=n; k++) { __int64 Min=inf; for (int i=1; 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]=1; for(int i=1; i<=n; i++) if(ab(dist[i]-dist[tm])<=l[4]&&!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=1; cin>>t; while (t--) { cin>>l[1]>>l[2]>>l[3]>>l[4]>>c[1]>>c[2]>>c[3]>>c[4]; int m; cin>>n>>m; for(int i=1; i<=n; i++) cin>>dist[i]; printf ("Case %d:\n",k++); while (m--) { int a,b; cin>>a>>b; Dijkstra(a,b); } } }
相关文章推荐
- 【LeetCode】Lowest Common Ancestor of a Binary Search Tree 解题报告
- [leetcode] 241. Different Ways to Add Parentheses 解题报告
- 新的开始。
- 正确实现用spring扫描自定义的annotation
- 自我学习
- 2016 UESTC Training for Data Structures A - 卿学姐与公主 CDOJ 1324 线段树
- Gradle DSL method not found android()解决方法
- Java transient关键字的理解
- ActiveRecord 模式
- 2.首先对box2d 的几个重要文件夹里面的内容进行解释
- hdoj The Frog's Games (二分查找)
- Calling startActivity() from outside of an Activity 异常处理
- nodejs 核心模块crypto
- linux基础(2)--服务器组装和基本命令使用--RHEL6.5
- leetcode-225. Implement Stack using Queues
- 字符数组里的成双成对
- JavaScript学习笔记
- CentOS6.5安装VMware Tools
- C3P0连接池参数解释
- 登录