luogu2483 【模板】k短路([SDOI2010]魔法猪学院)
模板题
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
int n, m, hea[5005], cnt, uu, vv, ans;
double e, ww, dis[5005];
const double eps=1e-7;
bool vis[5005];
struct Edge{
int too, nxt;
double val;
}edge[400005];
struct Node{
int idx;
double hfc, gfc;
bool operator<(const Node &x)const{
return hfc+gfc>x.hfc+x.gfc;
}
}cr, cc;
queue<int> d;
priority_queue<Node> q;
void add_edge(int fro, int too, double val){
edge[++cnt].nxt = hea[fro];
edge[cnt].too = too;
edge[cnt].val = val;
hea[fro] = cnt;
}
void spfa(){
memset(dis, 127, sizeof(dis));
dis[n] = 0.0;
d.push(n);
vis[n] = true;
while(!d.empty()){
int x=d.front();
d.pop();
vis[x] = false;
for(int i=hea[x]; i; i=edge[i].nxt)
if(i%2==0){
int t=edge[i].too;
if(dis[t]>dis[x]+edge[i].val){
dis[t] = dis[x] + edge[i].val;
if(!vis[t]){
vis[t] = true;
d.push(t);
}
}
}
}
}
void astar(){
cr.idx = 1; cr.hfc = cr.gfc = 0.0;
q.push(cr);
while(!q.empty()){
cc = q.top();
q.pop();
if(cc.hfc+cc.gfc-eps>e) return ;
if(cc.idx==n) ans++, e -= cc.hfc;
for(int i=hea[cc.idx]; i; i=edge[i].nxt)
if(i%2){
cr.idx = edge[i].too;
cr.hfc = cc.hfc + edge[i].val;
cr.gfc = dis[cr.idx];
if(cr.hfc+cr.gfc>e) continue;
q.push(cr);
}
}
}
int main(){
cin>>n>>m>>e;
for(int i=1; i<=m; i++){
scanf("%d %d %lf", &uu, &vv, &ww);
add_edge(uu, vv, ww);
add_edge(vv, uu, ww);
}
spfa();
astar();
cout<<ans<<endl;
return 0;
}
拙いものと思えども、
その手に握る其れこそが、
いつか幻想を生んでいく。