Dijkstra, water problem. Debugging took less than an hour, not skilled.
When submitting, I found that there were problems with the last three test points, thinking that there was a problem with the program. It turns out that the upper limit is not large enough.
All in all, it is inexperienced. Pay attention next time.
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cmath> 5 6 using namespace std; 7 const int Maxn = 10050; 8 const double Inf = 100000050; 9 10 struct Pic{ 11 int x,y; 12 }c[Maxn]; 13 14 struct Dijkstra{ 15 double d; 16 bool Known; 17 }Dij[Maxn]; 18 19 double p[Maxn][Maxn]; 20 int k,m,n,x,y,s,t,i,j; 21 22 23 24 25 void dijkstar(int x) 26 { 27 int a,b,i=x,k; 28 int t; 29 double Min ; 30 31 for(k=1;k<n;k++) 32 { 33 Min = Inf; 34 t = 0; 35 for(a=1;a<=n;a++) 36 if( Min > Dij[a].d && !Dij[a].Known ) 37 { 38 Min = Dij[a].d; 39 t = a; 40 } 41 if(t==0) break; 42 Dij[t].Known = true; 43 44 for(a=1;a<=n;a++) 45 if(p[i][a] ) 46 if(Dij[a].d > Dij[i].d + p[i][a] ) 47 Dij[a].d = Dij[i].d + p[i][a]; 48 49 i = t; 50 } 51 } 52 53 double dis(int i,int j) 54 { 55 return sqrt((double)pow((c[i].x - c[j].x),2) + pow((double)(c[i].y - c[j].y),2)); 56 } 57 58 int main() 59 { 60 // FILE *f = fopen("data.txt","r"); 61 scanf("%d",&n); 62 // fscanf(f, "%d", &n); 63 for(k=1;k<=n;k++) 64 { 65 Dij[k].d =Inf; 66 Dij[k].Known = false; 67 } 68 for(k=1;k<=n;k++) 69 { 70 scanf("%d%d",&x,&y); 71 // fscanf(f,"%d%d",&x,&y); 72 c[k].x = x; 73 c[k].y = y; 74 } 75 76 scanf("%d",&m); 77 // fscanf(f,"%d",&m); 78 for(k=1;k<=m;k++) 79 { 80 scanf("%d%d",&i,&j); 81 // fscanf(f,"%d%d",&i,&j); 82 p[i][j] = p[j][i] = dis(i,j); 83 } 84 85 scanf("%d%d",&s,&t); 86 // fscanf(f,"%d%d",&s,&t); 87 Dij[s].d = 0; 88 dijkstar(s); 89 90 printf("%.2f",Dij[t].d); 91 return 0; 92 }