社区讨论
ask:关于时间复杂度
P11860[CCC 2025 Senior] 熔岩路 / Floor is Lava参与者 4已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @mhjcwxti
- 此快照首次捕获于
- 2025/11/04 00:30 4 个月前
- 此快照最后确认于
- 2025/11/04 00:30 4 个月前
这份代码的时间复杂度有问题,极限情况可被卡至 (根据测试结果:https://www.luogu.com.cn/record/233255415 ,感觉和 SPFA 是差不多的,在菊花图上会被卡掉)
求为什么以及时间复杂度证明。
C#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define int long long
const int N=4e5+12;
const ll inf=0x3f3f3f3f3f3f3f3f;
int n,m;
int head[N],to[N<<1],nex[N<<1],tp[N<<1],tot=0;//tp:temperature
inline void mkr(int u,int v,int w){
to[++tot]=v,nex[tot]=head[u],tp[tot]=w,head[u]=tot;
}
struct node_edge1{
int fr,to;
bool operator < (const node_edge1 &oth) const{
if(fr==oth.fr){
return to<oth.to;
}
return fr<oth.fr;
}
};
map<node_edge1,ll> dis;
map<node_edge1,bool> vis;
int a,b,c;
struct node{
int fr,to,tp;ll dis;
bool operator < (const node &oth) const{
return dis>oth.dis;
}
node(){}
node(int _fr,int _to,int _tp,int _dis){
fr=_fr,to=_to,tp=_tp,dis=_dis;
}
};
priority_queue<node> q;
signed main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>a>>b>>c;
dis[{a,b}]=dis[{b,a}]=inf;
mkr(a,b,c),mkr(b,a,c);
}
for(int f=head[1];f;f=nex[f]){
int t=to[f],v=tp[f];
// cout<<"bg 1 "<<t<<' '<<v<<endl;
dis[{1,t}]=v;
q.emplace(1,t,v,v);
}
// for(auto ff:dis){
// cout<<"edge "<<ff.first.fr<<' '<<ff.first.to<<' '<<ff.second<<endl;
// }
while(!q.empty()){
node now=q.top();q.pop();
if(vis[{now.fr,now.to}])continue;
vis[{now.fr,now.to}]=1;
for(int f=head[now.to];f;f=nex[f]){
int t=to[f],v=abs(now.tp-tp[f]);
if(dis[{now.to,t}]>now.dis+v){
dis[{now.to,t}]=now.dis+v;
q.emplace(now.to,t,tp[f],now.dis+v);
}
}
}
ll ans=inf;
// for(auto ff:dis){
// cout<<"edge "<<ff.first.fr<<' '<<ff.first.to<<' '<<ff.second<<endl;
// }
for(int f=head[n];f;f=nex[f]){
ans=min(ans,dis[{to[f],n}]);
}
cout<<ans<<endl;
return 0;
}
回复
共 5 条回复,欢迎继续交流。
正在加载回复...