zoukankan      html  css  js  c++  java
  • 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)!

  • 相关阅读:
    使用策略模式减少if else
    php 向二维数组中追加元素
    svn update 产生Node remains in conflict的问题
    php对ip地址的处理
    php 对比两个数组中的值是否相等
    jquery 通过attr获取属性只有一个值的解决
    php 一维数组去重
    调整ceph的pg数(pg_num, pgp_num)
    linux-Centos 7下bond与vlan技术的结合[推荐]
    centos 配置vlan
  • 原文地址:https://www.cnblogs.com/universeplayer/p/10508689.html
Copyright © 2011-2022 走看看