HDU 1690 Bus System arbitrary point Shortest Path Floyd

Source: Internet
Author: User

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&LT;=L1)returnC1;Else if(x>l1&&x&LT;=L2)returnC2;Else if(x>l2&&x&LT;=L3)returnC3;Else if(x>l3&&x&LT;=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", &AMP;L1,&AMP;L2,&AMP;L3,&AMP;L4,&AMP;C1,&AMP;C2,&AMP;C3,&AMP;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

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.