hdu 4308 Saving Princess claire_

http://acm.hdu.edu.cn/showproblem.php?pid=4308
题解: 最短路,将p看成一个点 dij()硬是tle 我就奇怪了 最多就5000
*5000啊,;最后该的SPFA()1A 。。。。。。。 #include<stdio.h> #include<string.h> #include<iostream> #include<vector> using namespace std; #define maxn 5001 #define inf 999999 #define CL(V,num) memset(V,num,sizeof(V)); char str[maxn][maxn]; int r,c,cost,max1,p,s,e; int dis[maxn],vis[maxn]; int find(int x,int y) { return x*c+y; } struct node { int k; int l; }; vector<node>g[maxn]; int queue[maxn*200+10]; void build() { int i,j; int x,y; node a; for(i=0;i<r;i++) { for(j=0;j<c;j++) { if(str[i][j]!='#') { if(str[i][j]=='P') { if(p==-1) p=find(i,j); x=p; } else x=find(i,j); max1=max1>x?max1:x; if(str[i][j]=='Y')s=x; if(str[i][j]=='C')e=x; if(i-1>=0&&str[i-1][j]!='#') { if(str[i-1][j]=='P'&&p==-1)p=find(i-1,j);//将p看成一个点 if(str[i-1][j]=='P'&&p!=-1)y=p; else y=find(i-1,j); a.k=y; if(str[i-1][j]=='*') a.l=cost; else a.l=0; g[x].push_back(a); } if(j+1<c&&str[i][j+1]!='#') { if(str[i][j+1]=='P'&&p==-1)p=find(i,j+1); if(str[i][j+1]=='P'&&p!=-1)y=p; else y=find(i,j+1); a.k=y; if(str[i][j+1]=='*') a.l=cost; else a.l=0; g[x].push_back(a); } if(i+1<r&&str[i+1][j]!='#') { if(str[i+1][j]=='P'&&p==-1)p=find(i+1,j); if(str[i+1][j]=='P'&&p!=-1)y=p; else y=find(i+1,j); a.k=y; if(str[i+1][j]=='*') a.l=cost; else a.l=0; g[x].push_back(a); } if(j-1>=0&&str[i][j-1]!='#') { if(str[i][j-1]=='P'&&p==-1)p=find(i,j-1); if(str[i][j-1]=='P'&&p!=-1)y=p; else y=find(i,j-1); a.k=y; if(str[i][j-1]=='*') a.l=cost; else a.l=0; g[x].push_back(a); } } } } } int SPFA() { int i,j,k,min; node a; memset(vis,0,sizeof(vis)); for(i=0;i<=max1;i++)dis[i]=inf; dis[s]=0; vis[s]=1; int head=0,tail=0; queue[head]=s; tail++; while(head!=tail) { int k=queue[head]; for(i=0;i<g[k].size();i++) { node a=g[k][i]; int b=a.k; if(dis[k]+a.l<dis[b]) { dis[b]=dis[k]+a.l; if(!vis[b]) { vis[b]=1; queue[tail]=b; tail++; if(tail==maxn*200)tail=0; } } } head++; if(head==maxn*200)head=0; } return dis[e]; } int main() { int i,j; while(scanf("%d%d%d",&r,&c,&cost)!=EOF) { for(i=0;i<=r*c;i++)g[i].clear(); max1=-1; p=-1; for(i=0;i<r;i++) { scanf("%s",str[i]); } build(); int ans=dij(); if(ans==inf)printf("Damn teoy!\n"); else printf("%d\n",ans); } }

你可能感兴趣的:(AIR)