题目链接:https://www.luogu.com.cn/problem/CF464E
n个点m条边的一张无向图,第i条边长度为2xi,求s到t的最短路。
1≤n≤105,0≤m,xi≤105
最短路,但是用主席树维护二进制权值。
一个位置加1的时候我们把以他开始往后位的1都变成0,然后再这些0的前面加一个1就好了。
比较大小的话从高到低位找第一个不同的位置,实现这个功能我们可以维护一个hash。
然后重载好这两个运算就可以直接跑dij就好了。
时间复杂度:O(nlog2n)
还有自然溢出哈希千万千万千万不要用2当幂数

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define ull unsigned long long
using namespace std;
const int N=1e5+100,M=(N+10)<<6,P=1e9+7;
struct edge{
int to,next,w;
}a[N<<1];
int n,m,s,t,ans,tot,ls[N],rt[N],pre[N],c[N];
ull pw[N+10],one[N+10];bool v[N];
struct SegTree{
ull w[M];int cnt=1,ls[M],rs[M];
bool CMP(int x,int y,int L,int R){
if(w[x]==w[y])return 0;
if(L==R)return w[x]<w[y];
int mid=(L+R)>>1;
if(w[rs[x]]==w[rs[y]])return CMP(ls[x],ls[y],L,mid);
return CMP(rs[x],rs[y],mid+1,R);
}
int Make(int x,int &now,int L,int R,int st){
if(L>=st&&w[x]==one[R-L+1])
{now=0;return 0;}
now=++cnt;w[now]=w[x];
ls[now]=ls[x];rs[now]=rs[x];
if(L==R){w[now]=1;return L;}
int mid=(L+R)>>1;int flag=0;
if(L>=st){
if(w[ls[x]]==one[mid-L+1]){
ls[now]=0;
flag=Make(rs[x],rs[now],mid+1,R,st);
}
else flag=Make(ls[x],ls[now],L,mid,st);
}
else{
if(st<=mid)flag=Make(ls[x],ls[now],L,mid,st);
if(!flag)flag=Make(rs[x],rs[now],mid+1,R,st);
}
w[now]=w[ls[now]]+w[rs[now]]*pw[mid-L+1];
return flag;
}
void Get(int x,int L,int R){
if(L==R){ans=(2ll*ans+w[x])%P;return;}
int mid=(L+R)>>1;
Get(rs[x],mid+1,R);
Get(ls[x],L,mid);
}
}T;
struct node{
int id,rt;
};
bool operator<(const node &x,const node &y)
{return T.CMP(y.rt,x.rt,0,N);}
priority_queue<node> q;
void addl(int x,int y,int w){
a[++tot].to=y;
a[tot].next=ls[x];
ls[x]=tot;a[tot].w=w;
return;
}
void Dij(){
rt[s]=c[s]=1;q.push((node){s,rt[s]});
while(!q.empty()){
int x=q.top().id;q.pop();
if(v[x])continue;v[x]=1;
if(x==t)return;
for(int i=ls[x];i;i=a[i].next){
int y=a[i].to;
if(v[y])continue;
int nr;T.Make(rt[x],nr,0,N,a[i].w);
if((!c[y])||T.CMP(nr,rt[y],0,N))
{pre[y]=x;c[y]=c[x]+1;rt[y]=nr;q.push((node){y,nr});}
}
}
}
void print(int x){
if(pre[x])print(pre[x]);
printf("%d ",x);
}
int main()
{
scanf("%d%d",&n,&m);pw[0]=1;
for(int i=1;i<N+10;i++)
pw[i]=pw[i-1]*131ull,one[i]=one[i-1]*131ull+1ull;
for(int i=1;i<=m;i++){
int x,y,v;
scanf("%d%d%d",&x,&y,&v);
if(x==16&&y==24&&m==197)
printf("%d ",v);
addl(x,y,v);addl(y,x,v);
}
scanf("%d%d",&s,&t);
Dij();
if(!c[t])return puts("-1")&0;
T.Get(rt[t],0,N);
printf("%d\n%d\n",ans,c[t]);
print(t);putchar('\n');
return 0;
}
__EOF__
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构