好久没做图论了,没想到感觉还是一级棒啊!现在觉得自己的状态太好了,希望能保持到下个ACM赛季。
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
#define maxn 1010
#define maxm 100010
#define INF 100000000
struct edge {
int y,next;
double d;
}g[maxm];
struct data {
int x,y;
}a[maxn];
int first[maxn],tot,n,m,i,x,y,d,v[maxn];
double dis[maxn];
double dist(int i,int j) {
int x = a[i].x-a[j].x;
int y = a[i].y-a[j].y;
return sqrt(x*x+y*y*1.0);
}
void add(int x,int y,int v) {
double c = dist(x,y)/(v*1000.0/60);
tot++;
g[tot].y = y;
g[tot].d = c;
g[tot].next = first[x];
first[x] = tot;
tot++;
g[tot].y = x;
g[tot].d = c;
g[tot].next = first[y];
first[y] = tot;
}
double spfa()
{
int x,t,i;
memset(v,0,sizeof(v));
for (i=1;i<=n;i++) dis[i]=INF;
queue <int> q;
q.push(1);
v[1]=true;
dis[1]=0;
while (!q.empty())
{
x=q.front();
v[x]=false;
q.pop();
for (t=first[x];t!=-1;t=g[t].next)
if (dis[g[t].y]>dis[x]+g[t].d)
{
dis[g[t].y]=dis[x]+g[t].d;
if (!v[g[t].y])
{
v[g[t].y]=true;
q.push(g[t].y);
}
}
}
return dis[2];
}
int main() {
tot = 0;
memset(g,0,sizeof(g));
memset(first,-1,sizeof(first));
scanf("%d%d%d%d",&a[1].x,&a[1].y,&a[2].x,&a[2].y);
int p = 3;
n = 2;
while (~scanf("%d%d",&x,&y)) {
if (x==-1 && y==-1) {
for (int i=p;i<n;i++)
add(i,i+1,40);
p = n+1;
continue;
}
n++;
a[n].x = x;
a[n].y = y;
}
for (int i=1;i<=n;i++)
for (int j=i+1;j<=n;j++)
add(i,j,10);
printf("%.0lf\n",spfa());
return 0;
}