bfs优化

层次单调性

走地图

双重bfs

1.模块性

2.方案:外层bfs逆推,内层bfs重新跑

 

A.每次代价0/1:双端队列bfs

B.每次代价任意数值:优先队列bfs(dijikstra)、迭代(SPFA)

 

UVA11367 Full Tank?

https://www.luogu.org/problemnew/show/UVA11367

 1 const int N=1010,M=10010,C=105,INF=99999999;
 2 struct node
 3 {
 4   int u,cc,co;
 5   node(int U,int CC,int CO){u=U;cc=CC;co=CO;}
 6 };
 7 bool operator < (node x,node y){return x.co>y.co;}
 8 int f[N][C];
 9 int pr[N],u[M<<1],v[M<<1],w[M<<1],fir[N],nxt[M<<1];
10 int n,m,tot,c,s,e;
11 priority_queue<node> q;
12 
13 void bfs()
14 {
15   while(!q.empty()) q.pop();
16   q.push(node(s,0,0));
17   f[s][0]=0;
18   while(!q.empty())
19     {
20       node p=q.top();q.pop();
21       //cout<<p.u<<" "<<p.cc<<" "<<p.co<<endl;
22       if(p.u==e) {cout<<p.co<<endl;return;}
23       if(p.cc<c && p.co+pr[p.u]<f[p.u][p.cc+1])
24     {
25       f[p.u][p.cc+1]=p.co+pr[p.u];
26       q.push(node(p.u,p.cc+1,p.co+pr[p.u]));
27     }
28       qxx(i,p.u)
29     {
30       if(p.cc>=w[i] && f[v[i]][p.cc-w[i]]>=p.co)
31         {
32           f[v[i]][p.cc-w[i]]=p.co;
33           q.push(node(v[i],p.cc-w[i],p.co));
34         }
35     }
36     }
37   cout<<"impossible"<<endl;
38 }
39 
40 int main()
41 {
42   n=rd(),m=rd();
43   FOR(i,1,n) pr[i]=rd();
44   while(tot<(m<<1))
45     {
46       u[++tot]=rd(),v[tot]=rd(),w[tot]=rd();
47       ++u[tot],++v[tot];
48       nxt[tot]=fir[u[tot]],fir[u[tot]]=tot;
49       ++tot;u[tot]=v[tot-1],v[tot]=u[tot-1],w[tot]=w[tot-1];
50       nxt[tot]=fir[u[tot]],fir[u[tot]]=tot;
51     }
52   int qq=rd();
53   FOR(i,1,qq)
54     {
55       memset(f,8,sizeof(f));
56       c=rd(),s=rd(),e=rd();
57       ++s,++e;
58       bfs();
59     }
60   return 0;
61 }

 

 

 

A*

带估价函数的bfs

一定有f[i]<=g[i]

越接近效率越高

 

康托展开

排列->序数

∑(i=1->n) i后面比i小的数 * (n-i)!

 

转载于:https://www.cnblogs.com/universeplayer/p/10508689.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值