kuangbin_ShortPath L (POJ 2502)
dij部分还是跟模板差不多的 但是这题的难点是处理输入 或者说理解题意
事实上每个点之间都是可以走的......WA了好几发就因为没意识到同一条路线上的各个站点之间居然也可以走得比车子快....
PS: POJ的C++编译器居然没法自动识别sqrt函数用哪个=.=
#include <iostream> #include <string> #include <cstdio> #include <cmath> #include <cstring> #include <queue> #include <map> #include <vector> #include <set> #include <algorithm> #define INF 1e30 using namespace std; typedef pair<double, int> pdi; struct cmp{ bool operator () (const pdi a, const pdi b){ return a.first > b.first; } }; struct Node{ int x, y; }node[210]; //int pre[210]; double val[210][210], dis[210]; double DISTANCE(int a, int b){ return sqrt((double)((node[a].x - node[b].x) * (node[a].x - node[b].x) + (node[a].y - node[b].y) * (node[a].y - node[b].y))); } void dij(int s, int n) { //memset(pre, -1 ,sizeof pre); for(int i = 1; i <= n; i++){ dis[i] = INF; } priority_queue<pdi, vector<pdi>, cmp> q; q.push(make_pair(0, 1)); dis[1] = 0; while(!q.empty()){ pdi u = q.top(); q.pop(); if(u.first > dis[u.second]) continue; for(int i = 1; i <= n; i++){ if(i != u.second && dis[i] > dis[u.second] + val[u.second][i]){ dis[i] = dis[u.second] + val[u.second][i]; //pre[i] = u.second; q.push(make_pair(dis[i], i)); } } } } int main() { int size = 3; for(int i = 1; i < 210; i++){ for(int j = 1; j < 210; j++){ val[i][j] = INF; } } scanf("%d%d%d%d", &node[1].x, &node[1].y, &node[2].x, &node[2].y); val[1][2] = val[2][1] = DISTANCE(1, 2) / 10000 * 60; while(~scanf("%d%d", &node[size].x, &node[size].y)){ int a, b; size++; while(scanf("%d%d", &a, &b), ~a || ~b){ node[size].x = a; node[size].y = b; val[size-1][size] = val[size][size-1] = DISTANCE(size, size-1) / 40000 * 60; size++; } } for(int i = 1; i < size; i++){ for(int j = 1; j < size; j++){ val[i][j] = min(val[i][j], DISTANCE(i, j) / 10000 * 60); } } dij(1, size - 1); printf("%.0lf\n", dis[2]); /*for(int i = 1; i < size; i++){ printf("x%d = %-8d y%d = %-8d\n", i, node[i].x, i, node[i].y); } for(int i = 1; i < size; i++){ for(int j = 1; j < size; j++){ printf("[%d][%d] = %-3.0lf", i, j, val[i][j]); } putchar('\n'); } for(int i = 2; ~i; i = pre[i]){ printf("pre = %d\n", i); }*/ return 0; }