标签:
SPFA时点出队后一定要把在队内的标记置为false!SPFA时点出队后一定要把在队内的标记置为false!SPFA时点出队后一定要把在队内的标记置为false!
我因为这个卡了一个月大家信吗?测得时候总是60分,丢情况,总之我太弱了
一个月前傻傻的手写堆o(>﹏<)o,现在写Yveh教的我的stl大法的优先队列用于A*爽的飞起\(≧▽≦)/
#include<queue> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int point[5003],next[200003],v[200003],point2[5003],next2[200003],v2[200003],cnt=0,cnt2=0,N,M; double c[200003],c2[200003],dist[5003],E; bool p[5003]; struct datatype{ double g; int isk; bool operator < (const datatype &a) const { return g+dist[isk]>a.g+dist[a.isk]; } }; void insect(int x,int y,double z){next[cnt]=point[x];point[x]=cnt;v[cnt]=y;c[cnt]=z;cnt++;} void insect2(int x,int y,double z){next2[cnt2]=point2[x];point2[x]=cnt2;v2[cnt2]=y;c2[cnt2]=z;cnt2++;} void SPFA() { p[N]=1; queue<int>Q;Q.push(N); dist[N]=0.0; int i,now; while (!Q.empty()) { now=Q.front(); Q.pop(); p[now]=0; for (i=point2[now];i!=-1;i=next2[i]) if (dist[v2[i]]>dist[now]+c2[i]) {dist[v2[i]]=dist[now]+c2[i];if (p[v2[i]]==0){p[v2[i]]=1;Q.push(v2[i]);}} } } void Astar() { priority_queue<datatype>q; cnt=0; int i,now; double nowg; datatype A; A.g=0.0; A.isk=1; q.push(A); while ((E>0)&&(!q.empty())) { now=q.top().isk; nowg=q.top().g; q.pop(); if (now==N) {E=E-nowg; if (E>=0) {cnt++;continue;} else break;} for (i=point[now];i!=-1;i=next[i]) {A.isk=v[i];A.g=nowg+c[i];q.push(A);} }printf("%d\n",cnt); } int main() { memset(point,-1,sizeof(point)); memset(point2,-1,sizeof(point2)); memset(next,-1,sizeof(next)); memset(next2,-1,sizeof(next2)); memset(v,0,sizeof(v)); memset(v2,0,sizeof(v2)); memset(c,0,sizeof(c)); memset(c2,0,sizeof(c2)); memset(dist,0x43,sizeof(dist)); memset(p,0,sizeof(p)); scanf("%d %d %lf\n",&N,&M,&E); int i,x,y; double z; for (i=1;i<=M;++i) { scanf("%d %d %lf\n",&x,&y,&z); insect(x,y,z); insect2(y,x,z); }SPFA(); Astar(); return 0; }
codevs 1835 魔法猪学院 A*寻k短路做了一个月卡死在spfa那了/(ㄒoㄒ)/~~
标签:
原文地址:http://www.cnblogs.com/abclzr/p/5117052.html