跳到正文

题解

题解:P2483 【模板】k 短路 / [SDOI2010] 魔法猪学院

bingdongcode约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年6月4日
篇幅
约 4 分钟
题目
P2483

版本

  1. v1当前

只能说现在还在学习中,这道题暂时先用启发式搜索来做,将来会进行优化的。 #include<bits/stdc++.h> using namespace std; const double inf=10000005; int N,M; double E; int s,t; double e; int head1[5005],head2[5005],ne[400005],to[400005]; double magic[400005]; int tot=0; double dis[5005]; bool flag[5005]; int sum=0; priority_queue<pair<double,int>,vector<pair<double,int>>,greater<pair<double,int>>> pq; priority_queue<pair<double,int>,vector<pair<double,int>>,greater<pair<double,int>>> ppqq; void add1(int x,int y,double z) { ne[++tot]=head1[x]; to[tot]=y; magic[tot]=z; head1[x]=tot; } void add2(int x,int y,double z) { ne[++tot]=head2[x]; to[tot]=y; magic[tot]=z; head2[x]=tot; } int main() { cin>>N>>M; cin>>E; int i,j; for(i=1;i<=M;i++) { cin>>s>>t; cin>>e; add1(s,t,e); add2(t,s,e); } for(i=1;i<=N;i++) { dis[i]=inf; } dis[N]=0; pq.push(make_pair(0,N)); while(!pq.empty()) { pair<double,int> cur=pq.top(); pq.pop(); int b=cur.second; if(flag[b]==true) { continue; } flag[b]=true; for(int j=head2[b];j;j=ne[j]) { int k=to[j]; if(dis[k]>dis[b]+magic[j]) { dis[k]=dis[b]+magic[j]; pq.push(make_pair(dis[k],k)); } } } ppqq.push(make_pair(dis[1],1)); while(!ppqq.empty()) { pair<double,int> car=ppqq.top(); ppqq.pop(); double l=car.first; int r=car.second; if(r==N) { E-=l; if(E>=0) { sum++; } else { break; } continue; } for(int j=head1[r];j;j=ne[j]) { int u=to[j]; ppqq.push(make_pair(l-dis[r]+dis[u]+magic[j],u)); } } cout<<sum; return 0; }

原文