• codevs 1048 石子归并


    题目描述 Description

    有n堆石子排成一列,每堆石子有一个重量w[i], 每次合并可以合并相邻的两堆石子,一次合并的代价为两堆石子的重量和w[i]+w[i+1]。问安排怎样的合并顺序,能够使得总合并代价达到最小。

    输入描述 Input Description

    第一行一个整数n(n<=100)

    第二行n个整数w1,w2...wn  (wi <= 100)

    输出描述 Output Description

    一个整数表示最小合并代价

    样例输入 Sample Input

    4

    4 1 1 4

    样例输出 Sample Output

    18

    /* ***********************************************
    Author        :guanjun
    Created Time  :2016/5/30 9:13:12
    File Name     :codevs1048.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 10010
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    priority_queue<int,vector<int>,greater<int> >pq;
    struct Node{
        int x,y;
    };
    struct cmp{
        bool operator()(Node a,Node b){
            if(a.x==b.x) return a.y> b.y;
            return a.x>b.x;
        }
    };
    
    bool cmp(int a,int b){
        return a>b;
    }
    int a[110];
    int sum[110];
    int dp[110][110];
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int n;
        while(cin>>n){
            sum[0]=0;
            for(int i=1;i<=n;i++){
                cin>>a[i];
                sum[i]=sum[i-1]+a[i];
            }
            //dp[i][j];合并第i到第j堆石子所用的最小花费
            memset(dp,INF,sizeof dp);
            for(int i=1;i<=n;i++)dp[i][i]=0;
            for(int p=1;p<=n;p++){
                for(int i=1;i<=n;i++){
                    int j=p+i;
                    for(int k=i;k<=j;k++){
                        dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1]);
                    }
                }
            }
            cout<<dp[1][n]<<endl;
        }
        return 0;
    }
  • 相关阅读:
    表达式for loop
    用户输入
    字符编码
    变量字符编码
    Python安装
    Python 2 or 3?
    Python解释器
    2017中国大学生程序设计竞赛
    Educational Round 27
    Round #429 (Div.2)
  • 原文地址:https://www.cnblogs.com/pk28/p/5541779.html
Copyright © 2020-2023  润新知