poj2502
好吧,我承认我水了一道题,除边计算较麻烦外。。。。==裸地dijkstra
View Code
1 #include <stdio.h>
2 #include <math.h>
3 #define MAXN 210
4 #define INF (1<<29)
5 double map[MAXN][MAXN];
6 double dist[MAXN];
7 bool v[MAXN];
8 int cnt;
9 struct node
10 {
11 double x,y;
12 }p[MAXN];
13 double lenth(node a,node b)
14 {
15 return sqrt(pow(a.x-b.x,2.0)+pow(a.y-b.y,2.0));
16 }
17 void dij()
18 {
19 for(int i=1;i<=cnt;++i)
20 dist[i]=INF,v[i]=false;
21 dist[1]=0;
22 int f=-1,k;
23 while(f!=INF)
24 {
25 f=INF;
26 for(int i=1;i<=cnt;++i)
27 if(!v[i]&&f>dist[i])
28 f=dist[i],k=i;
29 v[k]=true;
30 for(int i=1;i<=cnt;++i)
31 if(!v[i]&&dist[i]>dist[k]+map[k][i])
32 dist[i]=dist[k]+map[k][i];
33 }
34 }
35 int main()
36 {
37 cnt=1;
38 scanf("%lf %lf %lf %lf",&p[cnt].x,&p[cnt].y,&p[cnt+1].x,&p[cnt+1].y);
39 map[1][2]=map[2][1]=lenth(p[cnt],p[cnt+1])/500*3;
40 ++cnt;
41 while(scanf("%lf %lf",&p[cnt+1].x,&p[cnt+1].y)!=EOF)
42 {
43 ++cnt;
44 for(int i=1;i<cnt;++i)
45 map[cnt][i]=map[i][cnt]=lenth(p[cnt],p[i])/500*3;
46 while(scanf("%lf %lf",&p[cnt+1].x,&p[cnt+1].y),p[cnt+1].x!=-1&&p[cnt+1].y!=-1)
47 {
48 map[cnt][cnt+1]=map[cnt+1][cnt]=lenth(p[cnt],p[cnt+1])/2000*3;
49 for(int i=1;i<cnt;++i)
50 map[cnt+1][i]=map[i][cnt+1]=lenth(p[cnt+1],p[i])/500*3;
51 ++cnt;
52 }
53 }
54 dij();
55 printf("%.0lf\n",dist[2]);
56 return 0;
57 }