题目连接:http://poj.org/problem?id=2502
用模拟栈试试。。。
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<stack> //用了模拟栈 5 using namespace std; 6 const int maxn=210; 7 const int inf=0x3f3f3f3f; 8 double x[maxn],y[maxn],dis[maxn]; 9 10 struct edge 11 { 12 double w; 13 int v,nex; 14 }e[maxn*maxn*2]; 15 int head[maxn]; 16 int cnt; 17 int sta[maxn*maxn]; //不知道要开多大 18 int ins[maxn]; 19 20 void add(int u,int v,double w) 21 { 22 e[cnt].v=v; 23 e[cnt].w=w; 24 e[cnt].nex=head[u]; 25 head[u]=cnt++; 26 } 27 28 double getdis(int last,int now) 29 { 30 double dx=x[last]-x[now]; 31 double dy=y[last]-y[now]; 32 return sqrt(dx*dx+dy*dy); 33 } 34 void double_make(int from, int to, double w) 35 { 36 add(from, to, w); 37 add(to, from, w); 38 } 39 void spfa() 40 { 41 42 int tot=0; 43 dis[0]=0; 44 ins[0]=1; 45 sta[tot++]=0; 46 while(tot) 47 { 48 int x=sta[--tot]; 49 ins[x]=0; 50 for(int i=head[x];i!=-1;i=e[i].nex) 51 { 52 int v=e[i].v; 53 double w=e[i].w; 54 if(dis[v]>dis[x]+w) 55 { 56 dis[v]=dis[x]+w; 57 if(!ins[v]) 58 { 59 sta[tot++]=v; 60 ins[v]=1; 61 } 62 } 63 } 64 } 65 } 66 int main() 67 { 68 scanf("%lf%lf%lf%lf",&x[0],&y[0],&x[1],&y[1]); 69 memset(head,-1,sizeof(head)); 70 cnt=0; 71 int n = 2, last = 0; 72 while (scanf("%lf %lf", &x[n], &y[n])!=EOF) 73 { 74 if (x[n] == -1 && y[n] == -1) 75 { 76 last = 0; 77 continue; 78 } 79 if (last) double_make(last, n, getdis(last, n) * 3.0 / 2000.0); 80 last = n++; 81 } 82 83 for(int i=0;i<n;i++) 84 for(int j=i+1;j<n;j++) 85 { 86 double w=getdis(i,j)*3.0/500.0; 87 add(i,j,w); 88 add(j,i,w); 89 } 90 for(int i=0;i<n;i++) 91 { 92 dis[i]=inf; 93 ins[i]=0; 94 } 95 spfa(); 96 printf("%.0f\n",round(dis[1])); 97 98 }
时间: 2024-10-03 18:14:40