hdu 1690 Bus System
Bus System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7295 Accepted Submission(s): 1903
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.
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.
公交车系统在X市中还是很陌生的
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.
这个表格是距离和价钱的关系
Distance | Cost
0 < dist <= L1 | C1
L1 < dist <= L2 | C2
L2 < dist <= L3 | C3
L3 < dist <= L4 | C4
dist > L4 | No ticket for this route
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.
我们用X轴去描述车站的位置(第一次做人肉翻译好羞涩(*/ω\*))
题目要求很简单,思路也很清晰就是一个floyd搞定
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 using namespace std; 8 typedef __int64 LL; 9 #define inf 0xffffffffffff //第一次开这么大的inf 10 #define maxn 505 11 12 LL Map[maxn][maxn]; 13 LL place[maxn]; 14 LL C[5],L[5]; 15 16 void floyd(int n) 17 { 18 for(int k = 1; k <= n; k++) 19 for(int i = 1; i <= n; i++) 20 if(Map[i][k] != inf) 21 for(int j = 1; j <= n; j++) 22 if(Map[i][j] > Map[i][k] + Map[k][j]) 23 Map[i][j] = Map[i][k] + Map[k][j]; 24 }//Floyd大法好 25 26 LL Abs(LL a) 27 { 28 return (a>=0)? a:-a; 29 }//注意位置并不是按由小到大排列所以可能出现副权值 30 31 int main() 32 { 33 int m,n,T; 34 scanf("%d",&T); 35 C[4] = L[4] = inf; 36 for(int t = 1; t <= T; t++) 37 { 38 for(int i = 0; i < 4; i++) 39 scanf("%I64d",&C[i]); 40 for(int i = 0; i < 4; i++) 41 scanf("%I64d",&L[i]); 42 scanf("%d%d",&n,&m); 43 for(int i = 1; i <= n; i++) 44 scanf("%I64d",&place[i]); 45 46 for(int i =1; i <= n; i++) 47 { 48 for(int j = i + 1; j <= n; j++) 49 { 50 int cut = 0; 51 while(C[cut] < Abs(place[j] - place[i])) cut++; 52 Map[i][j] = Map[j][i] = L[cut]; 53 } 54 Map[i][i] = 0; 55 } 56 57 floyd(n); 58 59 printf("Case %d:\n",t); 60 int p,q; 61 for(int i = 0; i < m; i++) 62 { 63 scanf("%d%d",&p,&q); 64 if(Map[p][q] == inf) 65 printf("Station %d and station %d are not attainable.\n",p,q); 66 else 67 printf("The minimum cost between station %d and station %d is %I64d.\n",p,q,Map[p][q]); 68 } 69 } 70 return 0; 71 }