POJ - 1135 自己构造图,最短路dijkstar模板求解
2017-10-03 14:29
411 查看
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
Sample Output
题目大意:首先给你两个坐标,一个是你家里的坐标,一个是你学校的坐标,然后接下来有若干条地铁线,每条地铁线上有若干个站点,给出每个站点的坐标,有这些点,这些点当中有距离,这个距离的单位是米,现在告诉你走路是10km/h,做地铁的话是40km/h,问你从家里到学校所花费的最短时间(分钟)
求解:只要是地铁相邻的站就用40km/h的速度求解,其他的都用10km/h的求解;
代码:
#include<stdio.h>
#include<string.h>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
#include<math.h>
#define Max 500
const double pi = 9999999.00;
struct node
{
double x,y;
}stu[1100];
double e[Max][Max];
double dis[Max];
int book[Max];
void init()
{
for(int i=0;i<=300;i++)
for(int j=0;j<=300;j++)
{
if(i==j) e[i][j] = 0;
else e[i][j] = pi;
}
}
void dijkstar(int s,int n)
{
memset(book,0,sizeof(book));
int i,j;
for(i=1;i<=n;i++)
dis[i]=e[s][i];
book[s]=1;
for(i=1;i<=n-1;i++)
{
double tt=pi;
for(j=1;j<=n;j++)
{
if(!book[j]&&tt>dis[j])
{
tt=dis[j];
s=j;
}
}
book[s]=1;
for(j=1;j<=n;j++)
{
if(!book[j]&&e[s][j]<pi&&dis[j]>dis[s]+e[s][j])
{
dis[j]=dis[s]+e[s][j];
}
}
}
}
int main()
{
int i,j,k;
while(~scanf("%lf%lf%lf%lf",&stu[1].x,&stu[1].y,&stu[2].x,&stu[2].y))
{
double x,y;
int tt=3,f=0;
init();
while(~scanf("%lf%lf",&x,&y))
{
if(x==-1&&y==-1)
4000
{
f=0; // f=0 一定要放上面;让我改了一上午的错,原来是f=0放continue
//下面了,以后一定要认真打代码了;
continue;
}
stu[tt].x=x;
stu[tt].y=y;
f++;
if(f>=2)
{
k=tt-1;
double ss=(stu[tt].x-stu[k].x)*(stu[tt].x-stu[k].x)+(stu[tt].y-stu[k].y)*(stu[tt].y-stu[k].y);
double q=(sqrt(ss))/40000.0;
e[k][tt]=q;
e[tt][k]=q;
}
tt++;
}
for(i=1;i<tt;i++)
{
for(j=1;j<tt;j++)
{
if(i!=j&&e[i][j]==pi)
{
double ss=(stu[i].x-stu[j].x)*(stu[i].x-stu[j].x)+(stu[i].y-stu[j].y)*(stu[i].y-stu[j].y);
double q=(sqrt(ss))/10000.0;
e[i][j]=q;
e[j][i]=q;
//printf("e[%d][%d]==%lf\n",i,j,q);
}
}
}
dijkstar(1,tt-1);
printf("%.0lf\n",dis[2]*60);
}
return 0;
}
dijkstar 模板;
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
题目大意:首先给你两个坐标,一个是你家里的坐标,一个是你学校的坐标,然后接下来有若干条地铁线,每条地铁线上有若干个站点,给出每个站点的坐标,有这些点,这些点当中有距离,这个距离的单位是米,现在告诉你走路是10km/h,做地铁的话是40km/h,问你从家里到学校所花费的最短时间(分钟)
求解:只要是地铁相邻的站就用40km/h的速度求解,其他的都用10km/h的求解;
代码:
#include<stdio.h>
#include<string.h>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
#include<math.h>
#define Max 500
const double pi = 9999999.00;
struct node
{
double x,y;
}stu[1100];
double e[Max][Max];
double dis[Max];
int book[Max];
void init()
{
for(int i=0;i<=300;i++)
for(int j=0;j<=300;j++)
{
if(i==j) e[i][j] = 0;
else e[i][j] = pi;
}
}
void dijkstar(int s,int n)
{
memset(book,0,sizeof(book));
int i,j;
for(i=1;i<=n;i++)
dis[i]=e[s][i];
book[s]=1;
for(i=1;i<=n-1;i++)
{
double tt=pi;
for(j=1;j<=n;j++)
{
if(!book[j]&&tt>dis[j])
{
tt=dis[j];
s=j;
}
}
book[s]=1;
for(j=1;j<=n;j++)
{
if(!book[j]&&e[s][j]<pi&&dis[j]>dis[s]+e[s][j])
{
dis[j]=dis[s]+e[s][j];
}
}
}
}
int main()
{
int i,j,k;
while(~scanf("%lf%lf%lf%lf",&stu[1].x,&stu[1].y,&stu[2].x,&stu[2].y))
{
double x,y;
int tt=3,f=0;
init();
while(~scanf("%lf%lf",&x,&y))
{
if(x==-1&&y==-1)
4000
{
f=0; // f=0 一定要放上面;让我改了一上午的错,原来是f=0放continue
//下面了,以后一定要认真打代码了;
continue;
}
stu[tt].x=x;
stu[tt].y=y;
f++;
if(f>=2)
{
k=tt-1;
double ss=(stu[tt].x-stu[k].x)*(stu[tt].x-stu[k].x)+(stu[tt].y-stu[k].y)*(stu[tt].y-stu[k].y);
double q=(sqrt(ss))/40000.0;
e[k][tt]=q;
e[tt][k]=q;
}
tt++;
}
for(i=1;i<tt;i++)
{
for(j=1;j<tt;j++)
{
if(i!=j&&e[i][j]==pi)
{
double ss=(stu[i].x-stu[j].x)*(stu[i].x-stu[j].x)+(stu[i].y-stu[j].y)*(stu[i].y-stu[j].y);
double q=(sqrt(ss))/10000.0;
e[i][j]=q;
e[j][i]=q;
//printf("e[%d][%d]==%lf\n",i,j,q);
}
}
}
dijkstar(1,tt-1);
printf("%.0lf\n",dis[2]*60);
}
return 0;
}
dijkstar 模板;
相关文章推荐
- 【最短路各种方法求解一题(模板题)】POJ 2387 Til the Cows Come Home
- POJ 2502 Subway 单源点最短路模板
- poj 2449 k短路--模板
- Matrix Power Series(POJ 3233 构造新矩阵求解+ 快速矩阵幂)
- Dijkstra算法(求解单源最短路)详解 + 变形 之 poj 1860 Currency Exchange
- POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
- poj 2449 A* +Dijkstra求第K短路(模板题)
- 【模板】POJ 2449 K短路(A*+dijkstra)
- POJ 1135 最短路
- POJ 1874 畅通工程续(最短路模板题)
- poj 1135 最短路(优先队列堆优化Dijkstra实现)
- POJ_Fibonacci POJ_3070(矩阵快速幂入门题,附上自己写的矩阵模板)
- <A*算法模板>poj 2449 第k短路
- Poj 1135 Domino Effect【很有意识的最短路+SPFA】
- 最短路练习12/poj/2502 Subway dijkstar,spfa,floyd都可以解决
- POJ 3255 Roadblocks (次短路模板)
- POJ1135 Domino Effect(最短路)
- poj 1125 Stockbroker Grapevine (Floyd最短路)【模板】
- POJ 3255 Roadblocks(次短路模板题)
- poj 2387 最短路模板题