L - Subway POJ - 2502 (dijkstra ++ 建图)
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.
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
原题链接:点击打开链接
做图论的题一定要细心,一个参数的错误,就可能导致wa很多次,希望这个博客给自己提个醒,细心细心,
题目不难简单的dijsktra
ac:代码
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
#define INF 100000000.0
const int maxn = 450;
struct node{
double x,y;
}ss[maxn];
double map[maxn][maxn],dis[maxn];
int vis[maxn];
int cnt;
double get_len(double x1,double y1,double x2,double y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
void dijkstra(int st)
{
for(int i = 0;i<cnt;i++)
{
dis[i] = map[st][i];
}
vis[st] = 1;
dis[st] = 0.0;
int pos = st;
for(int i = 0;i<cnt-1;i++)
{
for(int j = 0;j<cnt;j++)
{
if(!vis[j] && dis[j] > dis[pos] + map[pos][j])
{
dis[j] = dis[pos] + map[pos][j];
}
}
int v;
double minn = INF;
for(int j = 0;j<cnt;j++)
{
if(!vis[j] && minn>dis[j])
{
v = j;
minn = dis[j];
}
}
vis[v] = 1;
pos = v;
}
}
int main()
{
memset(vis,0,sizeof(vis));
memset(map,0,sizeof(map));
double x,y;
scanf("%lf %lf %lf %lf",&ss[0].x,&ss[0].y,&ss[1].x,&ss[1].y);
int tra = 2;
cnt = 2;
while(scanf("%lf %lf",&x,&y)!=EOF)
{
if( x == -1 && y == -1)
{
for(int i = tra;i<cnt-1;i++)
{
double len = get_len(ss[i].x,ss[i].y,ss[i+1].x,ss[i+1].y)/40000.0;
map[i][i+1] = map[i+1][i] = len;
}
tra = cnt;
continue;
}
ss[cnt].x = x;
ss[cnt++].y = y;
}
for(int i = 0;i<cnt;i++)
{
for(int j = i+1;j<cnt;j++)
{
if(map[i][j] == 0){
double len = get_len(ss[i].x,ss[i].y,ss[j].x,ss[j].y)/10000.0;
map[i][j] = map[j][i] = len;
}
}
}
dijkstra(0);
printf("%d",(int)(dis[1] * 60.0 +0.5));
return 0;
}