http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1867
1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 const int maxn = 102; 5 const int INF=1<<28; 6 double dis[maxn][maxn]; 7 struct node 8 { 9 double x; 10 double y; 11 } f[maxn];//储存每个点的坐标 12 int n,m; 13 void init() 14 { 15 for (int i = 0; i <= n; i ++) 16 { 17 for (int j = 0; j <= n; j ++) 18 { 19 dis[i][j] = INF; 20 } 21 dis[i][i] = 0; 22 } 23 } 24 void floyd() 25 { 26 int i,j,k; 27 for (k = 1; k <= n; k ++) 28 { 29 for (i = 1; i <= n; i ++) 30 { 31 for (j = 1; j <= n; j ++) 32 { 33 if(dis[i][j] > dis[i][k] + dis[k][j])//更新距离 34 dis[i][j] = dis[i][k] + dis[k][j]; 35 } 36 } 37 } 38 } 39 int main() 40 { 41 scanf("%d",&n); 42 init(); 43 for (int i = 1; i <= n; i ++) 44 { 45 scanf("%lf%lf",&f[i].x,&f[i].y); 46 } 47 scanf("%d",&m); 48 int u,v; 49 while(m--) 50 { 51 scanf("%d%d",&u,&v); 52 double d = sqrt((f[u].x-f[v].x)*(f[u].x-f[v].x)+(f[u].y-f[v].y)*(f[u].y-f[v].y));//计算u、v 之间的距离 53 if (dis[u][v] > d) // 判断重边 54 { 55 dis[u][v] = d; 56 dis[v][u] = d; 57 } 58 } 59 int s,e; 60 scanf("%d%d",&s,&e); 61 floyd(); 62 printf("%.2f ",dis[s][e]); 63 return 0; 64 }