Original title: http://acm.hdu.edu.cn/showproblem.php?pid=1690
Main topic:
The table in the figure is the cost of representing the different lengths of the path, entering a little bit of coordinates, to find the minimum cost between any two points.
Because it is for any two points, it is best to use the Floyd algorithm.
Several big pits in the title:
The data may be super int, with a long long int;
The coordinates can be negative, and the ABS absolute value function is used to find the distance.
The reference code is as follows:
#include <iostream>#include "iostream"#include "string.h"#include "stdlib.h"#include "stdio.h"#include "math.h"#define INF 0x3f7f7f7f7f7f7f7fUsing namespace Std;typedef long longintLint;const Lint N = the; Lint Maze[n][n];lint Polint[n];lint L1,l2,l3,l4,c1,c2,c3,c4;lint N,m; void Init () { for(Lint i=0; i<n;i++) { for(Lint j=0; j<n;j++) {maze[i][j]=inf; }}}lint Getlen (lintx){if(x<=L1)returnC1;Else if(x>l1&&x<=L2)returnC2;Else if(x>l2&&x<=L3)returnC3;Else if(x>l3&&x<=L4)returnC4;Else returnINF;} void Floyd () { for(Lint k=1; k<=n;k++) { for(Lint i=1; i<=n;i++) { for(Lint j=1; j<=n;j++) {maze[i][j]=min (maze[i][j],maze[i][k]+maze[k][j]); } } }}intMain () {lint cases; scanf"%i64d", &cases); for(Lint k=1; k<=cases;k++) {scanf ("%i64d %i64d %i64d %i64d %i64d %i64d %i64d %I 64d", &L1,&L2,&L3,&L4,&C1,&C2,&C3,&C4); scanf"%i64d %i64d",&n,&m); for(Lint i=1; i<=n;i++) {scanf ("%i64d", &polint[i]); } init (); for(Lint i=1; i<=n;i++) { for(Lint j=i+1; j<=n;j++) {Lint Len=getlen (ABS(Polint[j]-polint[i])); Maze[i][j]=len; Maze[j][i]=len; }} Floyd ();printf("Case %i64d: \ n", k); for(Lint i=1; i<=m; i++) {Lintx,y; scanf"%i64d %i64d",&x,&y);if(maze[x][y]!=inf)printf("The minimum cost between station %I64D and station %i64d are %i64d. \ n",x,y, maze[x][y]);Else printf("Station %i64d and station %i64d is not attainable.\n",x,y); } }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1690 Bus System arbitrary point Shortest Path Floyd