HDU 1690 Bus System Shortest path Floyd algorithm: The INF must be very big ... Data type with a long long.

Source: Internet
Author: User

Bus SystemTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 7163 Accepted Submission (s): 1853


Problem Descriptionbecause of the huge population of China, public transportation is very important. Bus is a 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 was calculated based on the distance between the stations. Here are a list which describes the relationship between the distance and the cost.



Your neighbor is a person who is a really miser. He asked the him to calculate, the minimum cost between the stations he listed. Can you solve this problem for him?
To simplify this problem, you can assume that all the stations is located on a straight line. We use X-coordinates to describe the stations ' positions.

Inputthe input consists of several test cases. There is a single number above all, the number of cases. There is no more than cases.
Each case contains eight integers in the first line, which was 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.
Integers, N and M, is given next, representing the number of the stations and questions. Each of the next n lines contains one integers, representing the x-coordinate of the ith station. Each of the next m lines contains-integers, representing the start point and the destination.
The 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 X -coordinates would have the same value.

Outputfor each question, if the stations is attainable, print the minimum cost between them. Otherwise, print "station X and station Y is not attainable." Use the format in the sample.

Sample Input
21 2 3 4 1 3 5 74 212341 44 11 2 3 4 1 3 5 74 1123101 4

Sample Output
Case 1:the minimum cost between Station 1 and station 4 are 3.The minimum cost between station 4 and Station 1 are 3.Case 2: Station 1 and Station 4 is not attainable.
Data type with a long long. INF set into hundreds of billions of best, I seem to tens of billions of WA.
There are also input and output with%i64d ... The rest is what Floyd did. Code:
#include <stdio.h> #include <limits.h> #define INF 110000000000typedef Long long ll; ll graph[110][110], p[110 ];//bool visited[110]; ll min (ll a, ll b) {return a>b?b:a;} void Floyd (int n) {for (int k = 1, k <= N; ++k) {for (int i = 1, i <= N; ++i) {for (int j = 1; j <= N; ++j) {graph I [j] = min (Graph[i][j],graph[i][k]+graph[k][j]);}}} int main () {int T, c = 1;scanf ("%d", &t), while (t--) {ll l1,l2,l3,l4,c1,c2,c3,c4; scanf ("%i64d%i64d%i64d%i64d%i64d%i 64d%i64d%i64d ", &l1,&l2,&l3,&l4,&c1,&c2,&c3,&c4); int n, m; scanf ("%d%d ", &n, &AMP;M); for (int i = 1; I <= n; ++i) {scanf ("%i64d", &p[i]);} for (int i = 1; I <= n; ++i) {Graph[i][i] = 0, for (int j = 1; j < i; ++j) {ll dis = p[i]-p[j];d is = dis>0?dis:- dis; if (dis<=l1) {graph[i][j] = graph[j][i] = C1;} else if (DIS&LT;=L2) {graph[i][j] = graph[j][i] = C2;} else if (DIS&LT;=L3) {graph[i][j] = graph[j][i] = C3;} else if (DIS&LT;=L4) {graph[i][j] = graph[j][i] = C4;} Else{gRAPH[I][J] = graph[j][i] = INF;}}}  Floyd (N);p rintf ("Case%d:\n", C + +), for (int i = 0; i < m; ++i) {int S, T; scanf ("%d%d", &s,&t); if (Graph[s][t] >= INF) {printf ("station%d and station%d is not attainable.\n", s,t);} else{printf ("The minimum cost between station%d and station%d is%i64d.\n", s,t,graph[s][t]);}} return 0;}

with June

HDU 1690 Bus System Shortest path Floyd algorithm: The INF must be very big ... Data type with a long long.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.