Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
为什么老是RE???AC的哥们帮忙看一下……(内附代码)Dijkstra变种实现……#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #define MAX 20000000 struct STONE { int x,y; }; struct PATH { char vex[300]; double totalweight; double weight; }; char s[250]; int v=0,k; double wm; char ch[2]; PATH path[300]; STONE stone[300]; int n,i,j,test,len; double map[300][300]; double d,max; bool Find(char* ss,char ch) { int i,len; len=strlen(ss); for(i=0;i<len;i++) if(ss[i]==ch) return 1; return 0; } int main() { //freopen("1.txt","r",stdin); for(test=1;;test++) { scanf("%d",&n); if(n==0) break; scanf("%d%d",&stone[0].x,&stone[0].y); scanf("%d%d",&stone[n-1].x,&stone[n-1].y); for(i=1;i<n-1;i++) { scanf("%d%d",&stone[i].x,&stone[i].y); } if(n==2) { d=sqrt(1.0*((stone[0].x-stone[1].x)*(stone[0].x-stone[1].x)+(stone[0].y-stone[1].y)*(stone[0].y-stone[1].y))); printf("Scenario #%d\nFrog Distance = %.3lf\n\n",test,d); } else { for(i=0;i<n;i++) for(j=0;j<n;j++) map[i][j]=MAX; for(i=1;i<n-1;i++) { d=sqrt(1.0*((stone[0].x-stone[i].x)*(stone[0].x-stone[i].x)+(stone[0].y-stone[i].y)*(stone[0].y-stone[i].y))); map[0][i]=d; map[i][0]=d; } for(i=1;i<n-1;i++) { d=sqrt(1.0*((stone[n-1].x-stone[i].x)*(stone[n-1].x-stone[i].x)+(stone[n-1].y-stone[i].y)*(stone[n-1].y-stone[i].y))); map[i][n-1]=d; map[n-1][i]=d; } for(i=1;i<n-1;i++) { for(j=i+1;j<n-1;j++) { d=sqrt(1.0*((stone[j].x-stone[i].x)*(stone[j].x-stone[i].x)+(stone[j].y-stone[i].y)*(stone[j].y-stone[i].y))); map[i][j]=d; map[j][i]=d; } } strcpy(s,""); v=0; ch[1]='\0'; for(i=0;i<n;i++) { path[i].totalweight=map[v][i]; path[i].weight=map[v][i]; if(path[i].totalweight<MAX) { path[i].vex[0]=v+'0'; path[i].vex[1]=i+'0'; path[i].vex[2]='\0'; } else { strcpy(path[i].vex,""); } } s[0]=v+'0'; s[1]='\0'; for(i=0;i<n;i++) { j=0; wm=MAX; for(k=0;k<n;k++) { if((!Find(s,k+'0')) && path[k].weight<wm) { j=k; wm=path[k].weight; } } ch[0]=j+'0'; ch[1]='\0'; strcat(s,ch); len=strlen(path[j].vex); for(k=0;k<n;k++) { if((!Find(s,k+'0')) && map[path[j].vex[len-1]-'0'][k]<path[k].weight) { path[k].weight=map[path[j].vex[len-1]-'0'][k]; strcpy(path[k].vex,path[j].vex); ch[0]=k+'0'; ch[1]='\0'; strcat(path[k].vex,ch); } } if(Find(s,n-1+'0')) break; } d=max=0; len=strlen(path[n-1].vex); for(i=0;i<len-1;i++) { d=map[path[n-1].vex[i]-'0'][path[n-1].vex[i+1]-'0']; if(d>max) max=d; } printf("Scenario #%d\nFrog Distance = %.3lf\n\n",test,max); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator