Link:
http://acm.hdu.edu.cn/showproblem.php?pid=1690
Topic:
Bus System
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4292 accepted Submission (s): 1088
Problem Description
Because of the huge population of, public transportation is very important. Bus was an important transportation the traditional public transportation system. And it ' s still playing a important role even now.
The bus system of City X is quite strange. Unlike other city's system, the cost of the ticket was calculated based on the distance between two. This is a-list which describes the relationship between the distance and the cost.
Your neighbor is a who really miser. He asked the minimum cost between the two stations his listed to calculate. Can you solve this problem for him?
To simplify this problem, you can assume so all the stations are located in 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 cases.
Each case contains eight integers on the "the", which are L1, L2, L3, L4, C1, C2, C3, C4, each number is non-negative And not larger than 1,000,000,000. 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 the "questions", the start point is 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 would have the same value.
Output
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
For each question, if the two stations are attainable, print the minimum cost between. 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 the 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.
Source
2008 "Sunline Cup" National Invitational Contest
Analysis and Summary:
A water problem, but WA 7 times.
1. Careless put 0<dist && dist<=l1 directly into the 0<DIST<=L1 = =| |, which led me to adjust a long time not to make a sample.
2. No use of 64-bit int
3. A long long after the discovery can not AC, it is estimated that hang-electricity and wonderful, so the input and output of all changed into Cin,cout, just AC. Then want to know the direct scanf, printf processing the long type of why WA, and then carried out the experiment, the results found HDU's amazing secret ... You can enter a long long with%LLD, but the output to use the __in64%i64d to AC embarrassment, said to HDU kneeling. Then use the __int64, found that with%lld input can also be AC ... "NOTE: The above is submitted in HDU C + +."
Code:
#include <iostream> #include <cstdio> #include <cstring> using namespace std;
typedef long long Int64;
Const Int64 INF = 1e18;
const int VN = 105;
int n;
int m;
Int64 D[VN][VN];
Int64 X[VN];
Int64 L1,l2,l3,l4;
Int64 c1,c2,c3,c4;
Inline Int64 ABS (Int64 x) {return x<0?-x:x;}
void Init () {for (int i=1; i<=n; ++i) {d[i][i] = 0;
for (int j=i+1; j<=n; ++j) D[i][j]=d[j][i]=inf;
} Int64 Getcost (Int64 dist) {if (0<dist && dist<=l1) return C1;
if (l1<dist && dist<=l2) return C2;
if (l2<dist && dist<=l3) return C3;
if (l3<dist && dist<=l4) return C4;
return INF; void Floyd () {for (int k=1; k<=n; ++k) for (int i=1; i<=n; ++i) if (d[i][k]!=inf) for (int j=1 ; j<=n;
++J) if (d[k][j]!=inf) d[i][j]=min (D[i][j], d[i][k]+d[k][j]);
int main () { int T,cas=1;
scanf ("%d", &t);
while (t--) {cin >> L1 >> L2 >> L3 >> L4;
CIN >> C1 >> C2 >> C3 >> C4;
scanf ("%d%d", &n,&m);
for (int i=1; i<=n; ++i) cin >> X[i];
Init (); for (int i=1; i<=n; ++i) {for (int j=i+1; j<=n; ++j) {d[i][j]=d[j][i]=getcost (ABS (X[i)
-X[J]));
} Floyd ();
int u,v;
printf ("Case%d:\n", cas++);
for (int i=0; i<m; ++i) {scanf ("%d%d", &u,&v);
if (d[u][v]!=inf) {printf ("The minimum cost between station%d and station%d is", u,v);
cout << D[u][v] << ". \ n";
else printf ("Station%d and station%d are not attainable.\n", u,v);
} return 0; }
Author: csdn Blog shuangde800