程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> POJ 2502 Subway(迪傑斯特拉)

POJ 2502 Subway(迪傑斯特拉)

編輯:關於C++

 

Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6692   Accepted: 2177

 

Description

You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bike to school every day, you now get to walk and take the subway. Because you don't want to be late for class, you want to know how long it will take you to get to school.
You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.

Input

Input consists of the x,y coordinates of your home and your school, followed by specifications of several subway lines. Each subway line consists of the non-negative integer x,y coordinates of each stop on the line, in order. You may assume the subway runs in a straight line between adjacent stops, and the coordinates represent an integral number of metres. Each line has at least two stops. The end of each subway line is followed by the dummy coordinate pair -1,-1. In total there are at most 200 subway stops in the city.

Output

Output is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.

Sample Input

0 0 10000 1000
0 200 5000 200 7000 200 -1 -1 
2000 600 5000 600 10000 600 -1 -1

Sample Output

21

題目鏈接:http://poj.org/problem?id=2502

 

題目大意:給出家和學校坐標表示固定的起點和終點,接下來每一行代表一條地鐵線,輸入該條地鐵線的每個站點的坐標,以-1,-1(不是坐標)結束一行,保證地鐵線沿直線,且至少有兩站。給出乘地鐵和步行的不同速度,求一個人從家到學校用到的最小時間。

 

解題思路:構建無向圖,時間為權值,用迪傑斯特拉算法,求家到學校兩個結點最短路徑問題。同一條地鐵線上兩站間的時間先計算出來,輸入結束,沒有權值任意兩以步行的速度計算時間。注意輸入格式,以EOF結束。

補充:Dijkstra算法適用於權值都為正的圖結構,dist[ i ]數組存儲 i 到起點v0 的最短路長度,初始為鄰接矩陣eg[v0][i]值無窮大.遍歷n-1次找出n-1條最短路。s[i ]數組記錄結點是否已確定最小dist[ i ],初始為0,確定後為1,找到的i值賦為u,以u為起點找下一個距離u最近的節點j,更新條件:dist [ j ] = min(dist [ u ]+eg [ v0 ] [ u ],dist [ j ]),保證每個點距離起點的距離最短。如果要記錄路徑,要用到path數組,如果通過u找到了j,path [ j ]=u記錄即可。

 

代碼如下:

 

#include 
#include 
#include 
#define INF 100000000.0;
int const maxn=400;
int s[maxn];
double dist[maxn];
double eg[maxn][maxn];
int num;

struct  A
{
    double x,y;
}stop[maxn];
double lenth(double x1,double y1,double x2,double y2)
{
    double a=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    return sqrt(a);
}

void Dijkstra(int v0)
{
    int i,j;
    for(i=0;i

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved