传送门
I think
解法如题
主要觉得手打堆太优美
Code
#include<cstdio>
#include<queue>
#define s second
using namespace std;
typedef pair<double,int> pdi;
const int sm = 5e3+5;
const int sn = 2e5+5;
const double Inf = 1e17;
struct node {
double g,f; int id;
bool operator < (const node&a) const {
return f<a.f;
}
};
int N,M,tot,Ans;
double E;
int to[sn],nxt[sn],hd[sm];
int _to[sn],_nxt[sn],_hd[sm];
double w[sn], _w[sn];
double d[sm];
bool vis[sm];
void read(int &x) {
char ch=getchar(); x=0;
while(ch>'9'||ch<'0') ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
}
void Add(int u,int v,double c) {
to[++tot]=v,nxt[tot]=hd[u],hd[u]=tot,w[tot]=c;
_to[tot]=u,_nxt[tot]=_hd[v],_hd[v]=tot,_w[tot]=c;
}
void Dijkstra(int St) {
for(int i=1;i<=N;++i) d[i]=Inf;
pdi t; d[St]=0;
priority_queue<pdi,vector<pdi>,greater<pdi> > Q;
Q.push(make_pair(d[St],St));
while(!Q.empty()) {
t=Q.top(),Q.pop();
if(vis[t.s]) continue;
vis[t.s]=1;
for(int i=_hd[t.s];i;i=_nxt[i])
if(!vis[_to[i]]&&d[_to[i]]>d[t.s]+_w[i]) {
d[_to[i]]=d[t.s]+_w[i];
Q.push(make_pair(d[_to[i]],_to[i]));
}
}
}
int size=0;
node Que[2000000];
void Swap(node &x,node &y) {node t=x;x=y;y=t; }
void Push(node a) {
int now,next;
Que[++size] = a;
now=size;
while(now>1) {
next=now>>1;
if(Que[next]<Que[now])break;
Swap(Que[next],Que[now]);
now=next;
}
}
node Pop() {
node ret = Que[1];
if(size-1) Que[1] = Que[size];
else Que[1] = (node){0,0,0};
--size;
int now=1,next;
while((now<<1)<=size) {
next=now<<1;
if(next<size&&Que[next+1]<Que[next])++next;
if(Que[now]<Que[next]) break;
Swap(Que[now],Que[next]);
now=next;
}
return ret;
}
void Astar(int u,int v) {
node p,q;
p = (node) {0,d[u],u};
Push(p);
while(size) {
q = Pop();
for(int i=hd[q.id];i;i=nxt[i]) {
p = (node) { q.g+w[i],q.g+w[i]+d[to[i]],to[i] };
Push(p);
}
if(q.id==v) {
E-=q.g;
if(E<0) break;
Ans++;
}
}
}
int main() {
int u,v; double c;
scanf("%d%d%lf",&N,&M,&E);
for(int i=1;i<=M;++i) {
scanf("%d%d%lf",&u,&v,&c);
Add(u,v,c);
}
Dijkstra(N);
Astar(1,N);
printf("%d\n",Ans);
return 0;
}