洛谷 [P2483] [模板] k短路
人生中的第一道黑题。。。
其实就是k短路模板
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <queue>
using namespace std;
const int MAXN=400005;
int init(){
int rv=0,fh=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') fh=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
rv=(rv<<1)+(rv<<3)+c-'0';
c=getchar();
}
return rv*fh;
}
int m,n,head1[MAXN],nume,head[MAXN];
double tot,dis[MAXN];
struct edge{
int to,nxt;
double dis;
}e[MAXN],e1[MAXN];
void adde1(int from,int to,double dis){
e1[++nume].to=to;
e1[nume].dis=dis;
e1[nume].nxt=head1[from];
head1[from]=nume;
}
void adde(int from,int to,double dis){
e[++nume].to=to;
e[nume].dis=dis;
e[nume].nxt=head[from];
head[from]=nume;
}
struct cmp{
bool operator ()(const int &a,const int &b) const{
return dis[a]>dis[b];
}
};
void dij(){
priority_queue <int,vector<int> ,cmp> q;
q.push(n);
memset(dis,0x7f,sizeof(dis));
dis[n]=0;
while(!q.empty()){
int u=q.top();q.pop();
for(int i=head1[u];i;i=e1[i].nxt){
int v=e1[i].to;
if(dis[v]>dis[u]+e1[i].dis){
dis[v]=dis[u]+e1[i].dis;
q.push(v);
}
}
}
}
struct node{
int v;
double f,g;
bool operator < (const node &a)const{
if(this->f==a.f) return (this->g)>a.g;
return (this->f)>a.f;
}
};
void A_star(){
node s;
int cnt=0;
s.v=1;s.f=dis[1];s.g=0.0;
priority_queue <node>q;
q.push(s);
while(!q.empty()){
node u=q.top();q.pop();//cout<<u.v<<" "<<u.f<<" "<<u.g<<endl;
if(u.v==n){
//cout<<tot<<endl;
if(tot>=u.g) tot-=u.g,cnt++;
else{
cout<<cnt<<endl;
return;
}
}
for(int i=head[u.v];i;i=e[i].nxt){
node v;
v.v=e[i].to;
v.g=u.g+e[i].dis;
v.f=v.g+dis[e[i].to];
//if(v.v==n) cout<<v.f<<endl;
q.push(v);
}
}
cout<<1<<endl;
}
int main(){
n=init();m=init();scanf("%lf",&tot);
for(int i=1;i<=m;i++){
int u=init(),v=init();
double di;
scanf("%lf",&di);
adde1(v,u,di);
}
dij();
// cout<<dis[1]<<endl;
nume=0;
for(int i=1;i<=n;i++){
for(int j=head1[i];j;j=e1[j].nxt){
int v=e1[j].to;
adde(v,i,e1[j].dis);
}
}
//cout<<e[1].dis<<endl;
// for(int i=head[1];i;i=e[i].nxt) cout<<e[i].to<<endl;
A_star();
return 0;
}