标签:
1 #include<queue> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int duin,N,M,next[200010],point[5010],v[200010],next2[200010],point2[5010],v2[200010]; 7 double c[200010],c2[200010],dist[5010],duif[4000010],E,sum=0; 8 int cnt=0,cnt2=0,duiu[4000010]; 9 bool vis[5010]; 10 void insect(int x,int y,double z){cnt++;next[cnt]=point[x];point[x]=cnt;v[cnt]=y;c[cnt]=z;} 11 void insect2(int x,int y,double z){cnt2++;next2[cnt2]=point2[x];point2[x]=cnt2;v2[cnt2]=y;c2[cnt2]=z;} 12 void swapp(double &x,double &y){double z=x;x=y;y=z;} 13 void swapp2(int &x,int &y){int z=x;x=y;y=z;} 14 void SPFA() 15 { 16 int i,u; queue<int>q; 17 for (i=1;i<N;++i) dist[i]=1e150; memset(vis,0,sizeof(vis)); 18 dist[N]=0; vis[N]=1; q.push(N); 19 while (!q.empty()) 20 { 21 u=q.front(); q.pop(); 22 for (i=point2[u];i!=0;i=next2[i]) 23 if (dist[v2[i]]>dist[u]+c2[i]) 24 { 25 dist[v2[i]]=dist[u]+c2[i]; 26 if (vis[v2[i]]==0) {vis[v2[i]]=1;q.push(v2[i]);} 27 } 28 } 29 } 30 void weihu() 31 { 32 duiu[1]=duiu[duin];duif[1]=duif[duin];duin--; 33 int i=1; 34 while (i<duin) 35 { 36 if (((i*2+1>duin)||(duif[i]<duif[i*2+1]))&&((i*2>duin)||(duif[i]<duif[i*2]))) 37 return; 38 if (i*2+1<=duin) 39 if (duif[i*2+1]<duif[i*2]) 40 { 41 swapp(duif[i*2+1],duif[i]); 42 swapp2(duiu[i*2+1],duiu[i]); i=i*2+1; 43 } 44 else 45 { 46 swapp(duif[i*2],duif[i]); 47 swapp2(duiu[i*2],duiu[i]); i=i*2; 48 } 49 else 50 { 51 swapp(duif[i*2],duif[i]); 52 swapp2(duiu[i*2],duiu[i]); i=i*2; 53 } 54 } 55 } 56 void indui(int u,double f) 57 { 58 duin++;duiu[duin]=u;duif[duin]=f; 59 int i=duin; 60 while (i>1) 61 { 62 if (duif[i]>duif[i/2]) return; 63 else {swapp(duif[i],duif[i/2]);swapp2(duiu[i],duiu[i/2]); i=i/2;} 64 } 65 } 66 void astar() 67 { 68 int u,i,u2; double f,g,f2,g2; cnt=0; 69 duin=1; duiu[1]=1; duif[1]=dist[1]; 70 while (duin>0) 71 { 72 u=duiu[1];f=duif[1];g=f-dist[u]; weihu(); 73 if (u==N){sum+=f; if (sum<=E) {cnt++;continue;} else return;} 74 for (i=point[u];i!=0;i=next[i]) 75 { 76 u2=v[i]; g2=g+c[i]; f2=g2+dist[u2]; 77 indui(u2,f2); 78 } 79 } 80 } 81 int main() 82 { 83 scanf("%d %d %lf\n",&N,&M,&E); 84 int i,x,y;double z; 85 for (i=1;i<=M;++i) 86 { 87 scanf("%d %d %lf\n",&x,&y,&z); 88 insect(x,y,z); insect2(y,x,z); 89 } 90 SPFA(); 91 astar(); 92 printf("%d\n",cnt); 93 return 0; 94 }
标签:
原文地址:http://www.cnblogs.com/abclzr/p/5049182.html