POJ 2502 Subway 关键在建图

Subway
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9718   Accepted: 3146

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

题意:给定家和学校坐标,中间可以步行和坐地铁,下面几行分别是几条地铁线。

思路:关键就是建图,总共才200个站,Foyld和Dij都可以过了。建图,一条地铁站路线是一条直线,从坐标也可以看出,只能相邻点到达,用路程除以地铁时间,然后步行全图建边,建边直接原边比较,取最小值,所以初始化很重要。

然后结果打印出最短路程是四舍五入的int型才能过。

#include <cstdio>
#include <cmath>
#include <iostream>

using namespace std;
const int maxn = 200+5;
const int inf = 0x3f3f3f3f;


struct Point{
	int x,y;
};

Point point[maxn],a;
int n,num[maxn];
double map[maxn][maxn];
int vis[maxn];

double lenth(Point a,Point b) {
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    }

void dijkstra(){
    double Min,dis[maxn];
    int u;
    for(int i = 1; i < n; i++) {
        dis[i] = map[1][i];
        vis[i] = 0;
    }

    for(int i = 1; i < n; i++) {
     Min = inf;
        for(int j = 1; j < n; j++) {
            if(!vis[j]&&Min>dis[j]) {
                Min = dis[j];
                u = j;
            }
        }
        vis[u] = 1;
        for(int i = 1; i < n; i++) {
            if(!vis[i]&&dis[i]>dis[u]+map[u][i])
                dis[i] = dis[u] + map[u][i];
        }
    }

    // printf("%d
",(int)(d[2]+0.5));
    int ans = round(dis[2]);
	printf("%d
",ans);//保留0位小数
}

int main(){
    freopen("in.txt","r",stdin);
	int cnt;
	int i,j;
	for(i=1;i<maxn;i++)
		for(j=1;j<maxn;j++)
			if(i==j) map[i][j]=0;
			else map[i][j]=inf;

	scanf("%d%d%d%d",&point[1].x,&point[1].y,&point[2].x,&point[2].y);
    n = 3;
	while(~scanf("%d%d",&a.x,&a.y)){
		cnt = 0;
		point[n] = a;
		num[cnt] = n;
        cnt++;
        n++;
		while(scanf("%d%d",&a.x,&a.y) && (a.x!=-1 && a.y!=-1)){
			point[n] = a;
			num[cnt] = n;
			cnt++;
			n++;
		}
		for(i=1;i<cnt;i++)		//地铁的站的时间只能是相邻的站点能到,不能从站点1直接到站点n
			map[num[i]][num[i-1]]=map[num[i-1]][num[i]]=lenth(point[num[i]],point[num[i-1]])*3.0/2000.0;
	}
	for(i=1;i<n;i++)
		for(j=1;j<n;j++) {
			map[i][j]=map[j][i]=min(map[i][j],lenth(point[i],point[j])*3.0/500.0);
		}
	dijkstra();
	return 0;
}


原文地址:https://www.cnblogs.com/zhangmingzhao/p/7256632.html