zoukankan      html  css  js  c++  java
  • POJ 1252 Euro Efficiency

    背包 要么 BFS


    意大利是说给你几个基本的货币,组成 1~100 所有货币,使用基本上的货币量以最小的。

    出口 用法概率。和最大使用量。


    能够BFS 有可能 。

    只是记得数组开大点。 可能会出现 100 = 99+99 -98 的情况。


    背包是先做一个全然背包,求得最少可能由多少相加。

    然后做一个 01背包,看是否能被 减。


    背包:

    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<map>
    #include<stack>
    #include<iostream>
    #include<list>
    #include<set>
    #include<cmath>
    #define INF 0x7fffffff
    #define eps 1e-6
    #define LL long long
    using namespace std;
    int dp[10002];
    
    int value[7];
    
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int m=10001;
            for(int i=0; i<6; i++)
                scanf("%d",&value[i]);
            for(int i=1; i<m; i++)
                dp[i]=10001;
            dp[0]=0;
            for(int i=0; i<6; i++)
            {
                for(int j=value[i]; j+value[i]<m; j++)
                {
                    dp[j]=min(dp[j-value[i]]+1,dp[j]);
    //                printf("%d :%d==
    ",j,dp[j]);
                }
    
            }
    
            for(int i=0; i<6; i++)
            {
                for(int j=m-value[i]; j>0; j--)
                {
                    dp[j]=min(dp[j+value[i]]+1,dp[j]);
    //                printf("%d :%d==
    ",j,dp[j]);
                }
    
            }
    
            double ans=0;
            int maxn=0;
            for(int i=1; i<=100; i++)
            {
    //            printf("%d : %d
    ",i,dp[i]);
                ans+=dp[i];
                maxn=max(maxn,dp[i]);
            }
            printf("%.2f %d
    ",ans/100.0,maxn);
        }
    }
    



    BFS:


    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<map>
    #include<stack>
    #include<iostream>
    #include<list>
    #include<set>
    #include<cmath>
    #define INF 0x7fffffff
    #define eps 1e-6
    #define LL long long
    using namespace std;
    struct lx
    {
        int ans,lv;
    };
    int ans[2051];
    int value[7];
    void bfs()
    {
        queue<lx>q;
        bool vis[2051];
        memset(vis,0,sizeof(vis));
        lx now,tmp;
        tmp.ans=0,tmp.lv=0;
        q.push(tmp);
        vis[0]=1;
        while(!q.empty())
        {
            tmp=q.front();q.pop();
            ans[tmp.ans]=tmp.lv;
            for(int i=0;i<6;i++)
            {
                int num1=tmp.ans+value[i];
                int num2=tmp.ans-value[i];
                now.lv=tmp.lv+1;
                if(!vis[num1]&&num1>0&&num1<2000)
                {
                    vis[num1]=1;
                    now.ans=num1;
                    q.push(now);
                }
                if(!vis[num2]&&num2>0&&num2<2000)
                {
                    vis[num2]=1;
                    now.ans=num2;
                    q.push(now);
                }
            }
        }
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            for(int i=0; i<6; i++)
                scanf("%d",&value[i]);
    
            bfs();
            double an=0;
            int maxn=0;
            for(int i=1;i<=100;i++)
            {
                an+=ans[i];
                maxn=max(maxn,ans[i]);
    //            printf("%d : %d
    ",i,ans[i]);
            }
            printf("%.2f %d
    ",an/100,maxn);
        }
    }
    


    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    [51nod] 1088 最长回文子串 #Hash+二分
    [51nod] 1378 夹克老爷的愤怒 #树形DP
    [BZOJ] 2456: mode #众数计数法
    [51nod] 1199 Money out of Thin Air #线段树+DFS序
    [51nod] 1494 选举拉票 #算法设计策略
    [51nod] 1463 找朋友 #离线+扫描线
    [BZOJ] 2733: [HNOI2012]永无乡 #线段树合并+并查集
    [BZOJ] 1012: [JSOI2008]最大数maxnumber
    [Codeforces] E. Lomsat gelral #DSU on Tree
    [BZOJ] 4756: [Usaco2017 Jan]Promotion Counting #线段树合并+权值线段树
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4730736.html
Copyright © 2011-2022 走看看