A*模板题,结果打挂了。。。样例都没过。
#include<bits/stdc++.h>
using namespace std;
int n,m,ans;
double e,dis[5010],sum;
bool vis[5010];
vector<pair<int,double> > a[5010],b[5010];
queue<int> q;
struct node{
int t;
double S,F;
void init(int a,double b){
t=a;
b=S;
F=b+dis[a];
}
friend bool operator<(const node &x,const node &y){
return x.F<y.F;
}
};
priority_queue<node> sq;
void SPFA(int start){
memset(dis,127,sizeof(dis));
vis[start]=1;
dis[start]=0;
q.push(start);
while(!q.empty()){
int now=q.front();
vis[now]=0;
for(int i=0;i<b[now].size();i++){
double s=dis[now]+b[now][i].second;
int t=b[now][i].first;
if(s<dis[t]){
dis[t]=s;
if(!vis[t]){
vis[t]=1;
q.push(t);
}
}
}
q.pop();
}
}
int main(){
scanf("%d%d%lf",&n,&m,&e);
while(m--){
int u,v;
double w;
scanf("%d%d%lf",&u,&v,&w);
a[u].push_back(make_pair(v,w));
b[v].push_back(make_pair(u,w));
}
SPFA(n);
node start;
start.init(1,0);
sq.push(start);
while(!sq.empty()){
node now=sq.top();
sq.pop();
int t=now.t;
double S=now.S;
if(t==n){
sum+=S;
if(sum>e) break;
ans++;
}else{
for(int i=0;i<a[t].size();i++){
int p=a[t][i].first;
double g=S+a[t][i].second;
if(g+dis[p]>e) continue;
node o;
o.init(p,g);
sq.push(o);
}
}
}
printf("%d",ans);
return 0;
}