Codeforces 710E Generate a String(dp或bfs)

0n+11x2y
bfsspfaTu>2umin(y,xu)2unnbfs


dpxx1
xdp[i]=min(dp[i1]+x,dp[(i+1)/2]+x+y)
i+3x1x
xdp[i]=min(dp[i1]+x,dp[i/2]+y)


代码1:

#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <bitset>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")

using namespace std;
#define   MAX           10000005
#define   MAXN          1000005
#define   maxnode       205
#define   sigma_size    26
#define   lson          l,m,rt<<1
#define   rson          m+1,r,rt<<1|1
#define   lrt           rt<<1
#define   rrt           rt<<1|1
#define   middle        int m=(r+l)>>1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   pii           pair<int,int>
#define   bits(a)       __builtin_popcount(a)
#define   mk            make_pair
#define   limit         10000

//const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 0x3f3f;
const double pi    = acos(-1.0);
const double inf   = 1e18;
const double eps   = 1e-4;
const LL    mod    = 1e9+7;
const ull    mx    = 133333331;

/*****************************************************/
inline void RI(int &x) {
      char c;
      while((c=getchar())<'0' || c>'9');
      x=c-'0';
      while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
 }
/*****************************************************/ 

LL dp[MAX];
int vis[MAX];
int main(){
    int n;
    LL x,y;
    while(cin>>n>>x>>y){
        for(int i=1;i<=n;i++) dp[i]=1e18;
        mem(vis,0);
        queue<int> q;
        dp[1]=x;
        q.push(1);
        vis[1]=1;
        while(!q.empty()){
            int u=q.front();q.pop();
            vis[u]=0;
            if(u-1>0&&dp[u-1]>dp[u]+x){
                dp[u-1]=dp[u]+x;
                if(!vis[u-1]){
                    q.push(u-1);
                    vis[u-1]=1;
                }       
            }
            if(u+1<=n&&dp[u+1]>dp[u]+x){
                dp[u+1]=dp[u]+x;
                if(!vis[u+1]){
                    q.push(u+1);
                    vis[u+1]=1;
                }
            }
            LL cost=min(y,x*u);
            if(2*u<=n&&dp[2*u]>dp[u]+cost){
                dp[2*u]=dp[u]+cost;
                if(!vis[2*u]){
                    q.push(2*u);
                    vis[2*u]=1;
                }
            }
            if(2*u>n&&dp[n]>dp[u]+y+(2*u-n)*x){
                dp[n]=dp[u]+y+(2*u-n)*x;
                if(!vis[n]){
                    vis[n]=1;
                    q.push(n);
                }
            }
        }
        cout<<dp[n]<<endl;
    }
    return 0;
}

代码2:

#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <bitset>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")

using namespace std;
#define   MAX           10000005
#define   MAXN          1000005
#define   maxnode       205
#define   sigma_size    26
#define   lson          l,m,rt<<1
#define   rson          m+1,r,rt<<1|1
#define   lrt           rt<<1
#define   rrt           rt<<1|1
#define   middle        int m=(r+l)>>1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   pii           pair<int,int>
#define   bits(a)       __builtin_popcount(a)
#define   mk            make_pair
#define   limit         10000

//const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 0x3f3f;
const double pi    = acos(-1.0);
const double inf   = 1e18;
const double eps   = 1e-4;
const LL    mod    = 1e9+7;
const ull    mx    = 133333331;

/*****************************************************/
inline void RI(int &x) {
      char c;
      while((c=getchar())<'0' || c>'9');
      x=c-'0';
      while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
 }
/*****************************************************/ 

LL dp[MAX];
int main(){
    int n;
    LL x,y;
    while(cin>>n>>x>>y){
        dp[0]=0;
        for(int i=1;i<=n;i++){
            dp[i]=1e18;
            if(i&1){
                dp[i]=min(dp[i-1]+x,dp[(i+1)/2]+x+y);
            }
            else dp[i]=min(dp[i-1]+x,dp[i/2]+y);
            //cout<<dp[i]<<endl;
        }
        cout<<dp[n]<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值