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[];
//int pre[210];
double val[][], dis[]; 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 = ; i <= n; i++){
dis[i] = INF;
}
priority_queue<pdi, vector<pdi>, cmp> q;
q.push(make_pair(, ));
dis[] = ;
while(!q.empty()){
pdi u = q.top();
q.pop();
if(u.first > dis[u.second]) continue;
for(int i = ; 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 = ;
for(int i = ; i < ; i++){
for(int j = ; j < ; j++){
val[i][j] = INF;
}
}
scanf("%d%d%d%d", &node[].x, &node[].y, &node[].x, &node[].y);
val[][] = val[][] = DISTANCE(, ) / * ;
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-][size] = val[size][size-]
= DISTANCE(size, size-) / * ;
size++;
}
}
for(int i = ; i < size; i++){
for(int j = ; j < size; j++){
val[i][j] = min(val[i][j], DISTANCE(i, j) / * );
}
}
dij(, size - );
printf("%.0lf\n", dis[]);
/*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 ;
}
05-02 13:14