• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
mengxm
博客园    首页    新随笔    联系   管理    订阅  订阅

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 }



posted @ 2012-02-14 00:52  mengxm  阅读(346)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3