POJ 2502 Subway / NBUT 1440 Subway / SCU 2186 Subway（图论，最短距离）

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

21

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<stack>
#include<cmath>
using namespace std;

const int maxN=300;
const int maxM=maxN*maxN;
const double v1=10000.0/60.0;//人的速度，均统一转换成m/min单位
const double v2=40000.0/60.0;//地铁的速度
const int inf=2147483647;

class Pos//坐标的结构体
{
public:
int x,y;
};

int n,m;
class Graph//图
{
private:
int cnt;
int Next[maxM];
int V[maxM];
double W[maxM];
bool instack[maxN];
stack<int> S;//听说spfa+stack快于spfa+queue
public:
double Dist[maxN];//距离
void init()
{
cnt=0;
memset(Next,-1,sizeof(Next));
}
void Add_Edge(int u,int v,double w)
{
cnt++;
V[cnt]=v;
W[cnt]=w;
}
void spfa(int s)//Spfa
{
memset(Dist,127,sizeof(Dist));
memset(instack,0,sizeof(instack));
Dist[s]=0;
instack[s]=1;
S.push(s);
do
{
int u=S.top();
S.pop();
instack[u]=0;
{
if (Dist[V[i]]>Dist[u]+W[i])
{
Dist[V[i]]=Dist[u]+W[i];
if (instack[V[i]]==0)
{
instack[V[i]]=1;
S.push(V[i]);
}
}
}
}
while (!S.empty());
}
void OutEdge()//为了方便检查输出的边
{
for (int i=1;i<=n;i++)
{
{
cout<<i<<"->"<<V[j]<<' '<<W[j]<<endl;
}
cout<<endl;
}
}
};

Pos P[maxN];
Graph G;

inline double Dist(Pos A,Pos B);
istream &operator >> (istream &is,Pos &p)//方便读入，重载一下输入运算符
{
is>>p.x>>p.y;
return is;
}

int main()
{
cin>>P[1]>>P[2];
G.init();
n=2;
while (cin>>P[n+1])//输入处理，有些麻烦
{
n++;
int now=n;
Pos input;
for (int i=1;i<=n;i++)
{
double dist=Dist(P[n],P[i])/v1;
}
while (cin>>input)
{
if (input.x==-1)
break;
n++;
P[n]=input;
double dist=Dist(P[n],P[n-1])/v2;
for (int i=1;i<=n;i++)
{
double dist2=Dist(P[n],P[i])/v1;
}
}
}
//G.OutEdge();
G.spfa(1);
printf("%.0f\n",G.Dist[2]);
return 0;
}

{
int x=0;
int k=1;
char ch=getchar();
while (((ch>'9')||(ch<'0'))&&(ch!='-'))
ch=getchar();
if (ch=='-')
{
k=-1;
ch=getchar();
}
while ((ch>='0')&&(ch<='9'))
{
x=x*10+ch-48;
ch=getchar();
}
return x*k;
}

inline double Dist(Pos A,Pos B)
{
return sqrt((double)((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)));
}

posted @ 2017-07-24 17:03 SYCstudio 阅读(...) 评论(...) 编辑 收藏