Poj--2502(图论,最短路,建图)
2014-09-10 22:53:55
思路:裸的Dij,最短路,建图的时候有点trick,地铁只能一站一站地开,还有就是各个点的坐标最好用double存,存int可能会出问题。
1 /************************************************************************* 2 > File Name: 2502.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Wed 10 Sep 2014 08:55:55 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <vector> 13 #include <queue> 14 #include <iostream> 15 #include <algorithm> 16 using namespace std; 17 typedef long long ll; 18 const int INF = 1 << 30; 19 20 int cnt; 21 double x[205],y[205]; 22 double g[205][205]; 23 double d[205]; 24 25 double Dist(double x1,double y1,double x2,double y2){ 26 return sqrt((x2 - x1)*(x2 - x1) + 1.0*(y2 - y1)*(y2 - y1)); 27 } 28 29 void Dij(){ 30 int vis[205]; 31 memset(vis,0,sizeof(vis)); 32 for(int i = 0; i < cnt; ++i) d[i] = INF; 33 d[0] = 0; 34 for(int i = 0; i < cnt; ++i){ 35 int p; 36 double tmin = (double)INF; 37 for(int j = 0; j < cnt; ++j) if(!vis[j] && d[j] < tmin) 38 tmin = d[p = j]; 39 vis[p] = 1; 40 for(int j = 0; j < cnt; ++j) if(d[j] > d[p] + g[p][j]) 41 d[j] = d[p] + g[p][j]; 42 } 43 } 44 45 void Init(){ 46 for(int i = 0; i < 205; ++i){ 47 for(int j = 0; j < 205; ++j){ 48 g[i][j] = INF; 49 } 50 g[i][i] = 0; 51 } 52 } 53 54 int main(){ 55 int x1,y1,x2,y2; 56 Init(); 57 cnt = 2; 58 scanf("%lf%lf%lf%lf",&x[0],&y[0],&x[1],&y[1]); 59 while(scanf("%lf%lf",&x[cnt],&y[cnt]) != EOF){ 60 ++cnt; 61 while(scanf("%lf%lf",&x[cnt],&y[cnt]) != EOF){ 62 if(x[cnt] == -1 && y[cnt] == -1) break; 63 ++cnt; 64 g[cnt-2][cnt-1] = g[cnt-1][cnt-2] 65 = Dist(x[cnt-2],y[cnt-2],x[cnt-1],y[cnt-1]) / 40000.0; 66 } 67 } 68 for(int i = 0; i < cnt; ++i) 69 for(int j = i + 1; j < cnt; ++j) 70 g[i][j] = g[j][i] = min(g[i][j],Dist(x[i],y[i],x[j],y[j]) / 10000.0); 71 Dij(); 72 printf("%.0lf\n",d[1] * 60.0); 73 return 0; 74 }

浙公网安备 33010602011771号