题目链接:http://poj.org/problem?id=2502
所有点用走路的速度连接,相邻站点用地铁速度连接,跑最短路。
注意输出用 %.0f,取其他精度会WA
#include #include #include #include #include #include #include #include using namespace std; const int Maxn = 210+10; const int INF = 0x3f3f3f3f; const double esp = 1e-6; struct Edge { int v, next; double w; } edge[Maxn*Maxn+2*Maxn]; int edge_cnt, h[Maxn]; double dis[Maxn]; bool vis[Maxn]; vector > node[Maxn]; void add(int u, int v, double w) { edge[edge_cnt].v = v; edge[edge_cnt].w = w; edge[edge_cnt].next = h[u]; h[u] = edge_cnt++; } void spfa(int n) { memset(vis, false, sizeof(vis)); for(int i = 0; i <= n; ++i) dis[i] = INF; dis[1] = 0; vis[1] = true; stack qu; qu.push(1); while(!qu.empty()) { int u = qu.top(); qu.pop(); vis[u] = false; for(int i = h[u]; i != -1; i = edge[i].next) { Edge &e = edge[i]; if(fabs(dis[e.v]-(dis[u]+e.w)) > esp && dis[e.v] > dis[u]+e.w) { dis[e.v] = dis[u]+e.w; if(!vis[e.v]) { vis[e.v] = true; qu.push(e.v); } } } } } double dist(int x, int y, int xx, int yy) { return sqrt((double)(x-xx)*(x-xx)+(y-yy)*(y-yy)); } bool input(int &cnt, int &n) { int x, y; while(1) { if(scanf("%d%d", &x, &y) == EOF) return false; if(x == -1 && y == -1) return true; node[cnt].push_back(make_pair(x, y)); n++; } } int main(void) { int m = 0, x1, x2, y1, y2, n = 2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); while(input(m, n)) m++; map, int> num; num[make_pair(x1, y1)] = 1; num[make_pair(x2, y2)] = n; memset(h, -1, sizeof(h)); edge_cnt = 0; int u, v, N = 2, x, y, xx, yy; for(int i = 0; i < m; ++i) { for(int j = 0; j < node[i].size(); ++j) { for(int k = 0; k < m; ++k) { for(int l = 0; l < node[k].size(); ++l) { if(i == k && l == j) continue; x = node[i][j].first; y = node[i][j].second; xx = node[k][l].first; yy = node[k][l].second; if(!num[make_pair(x, y)]) num[make_pair(x, y)] = N++; if(!num[make_pair(xx, yy)]) num[make_pair(xx, yy)] = N++; u = num[make_pair(x, y)]; v = num[make_pair(xx, yy)]; double d = dist(x, y, xx, yy); if(i == k && j+1 == l) { add(u, v, d*60/40000.0); add(v, u, d*60/40000.0); } else add(u, v, d*60/10000.0); } } } } for(int i = 0; i < m; ++i) { for(int j = 0; j < node[i].size(); ++j) { x = node[i][j].first; y = node[i][j].second; double d = dist(x1, y1, x, y); v = num[make_pair(x, y)]; add(1, v, d*60/10000.0); add(v, 1, d*60/10000.0); d = dist(x2, y2, x, y); add(n, v, d*60/10000.0); add(v, n, d*60/10000.0); } } double d = dist(x1, y1, x2, y2); add(1, n, d*60/10000.0); add(n, 1, d*60/10000.0); spfa(n); printf("%.0f\n", dis[n]); return 0; }