Subway POJ 2502

Source: Internet
Author: User

Topic Links:

http://poj.org/problem?id=2502

Main topic:

You've just moved from a quiet town to a noisy big city, so you can't go to school by bike, you can only take the subway or walk to school. Because you don't want to be late, so you want to know how long you can get to school, the speed of your walk is 10km/h,

The speed of the subway is 40km/h, if you are lucky, you just arrived on the subway to have a subway to come over, you can immediately ride, any one subway you can ride many times, if you want you can transfer to different subway, all the subway is two directions.

Input:

Input first contains two coordinates, indicating your home coordinates and school coordinates, followed by a few specifications of the subway lines, each subway line contains several coordinates, each coordinate represents the subway platform, the subway to this coordinates will stop, you can assume that the subway station is a straight line. Give a coordinate is an integer, each subway line has at least two stations, the last two-1 for the subway station end, up to 200 stations,

Output:

Output a number of minutes, indicating the shortest time home to school. Result rounding

Topic Analysis:

The difficulty of this problem is in dealing with data problems, the other is simple, but a bit to note is that each site of the railway line is not straight lines, there may be curves, so in the calculation of the distance when the two points of the subway can not be all in a straight line distance to calculate

Code:

#include <iostream>#include<cstdlib>#include<cstdio>#include<algorithm>#include<vector>#include<queue>#include<cmath>#include<cstring>using namespacestd;#defineINF 0xFFFFFFFF#defineMAXN 520structpoint{Doublex, y; intK;} P[MAXN];BOOLVIS[MAXN];intN;DoubleG[MAXN][MAXN], DIST[MAXN];DoubleLen (Point A, point B) {returnsqrt1.0* (a.x-b.x) * (a.x-b.x) +1.0* (A.Y-B.Y) * (A.Y-b.y));}voidInit () {memset (Vis,false,sizeof(VIS));  for(intI=0; i<=n; i++) {Dist[i]=INF; }}voidInput () {intK =2; N=2; scanf ("%LF%LF%LF%LF", &p[0].x,&p[0].y,&p[1].x,&p[1].y); p[0].K =0, p[1].K =1;  while(SCANF ("%LF%LF", &p[n].x, &p[n].y)! =EOF) {N++; P[n-1].K =K; if(p[n-1].x = =-1&& p[n-1].y = =-1) {n--; K++; }    }     for(intI=0; i<n; i++)    {         for(intj=0; j<=i; J + +)        {            DoubleLen =Len (P[i], p[j]), time; time= Len/10000.0* -; if(P[I].K = = P[J].K && i = = j+1) { time= Len/40000.0* -; } G[i][j]=Time ; G[j][i]=G[i][j]; }    }}DoubleSPFA () {intE =0; Queue<int>Q; dist[0] =0;    Q.push (e);  while( !Q.empty ()) {e=Q.front ();        Q.pop (); Vis[e]=false;  for(intI=0; i<n; i++)        {            if(Dist[i] > Dist[e] +G[e][i]) {Dist[i]= Dist[e] +G[e][i]; if(!Vis[i]) {Vis[i]=true;                Q.push (i); }            }        }    }    returndist[1];}intMain () {Input ();    Init (); DoubleAns =SPFA (); printf ("%0.lf\n", ans); return 0;}

Subway POJ 2502

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.