zoukankan      html  css  js  c++  java
  • HOJ 2317 Pimp My Ride(状态压缩DP)

    Pimp My Ride
    My Tags (Edit)
    Source : TUD 2005
    Time limit : 3 sec Memory limit : 64 M
    Submitted : 63, Accepted : 34
    Today, there are quite a few cars, motorcycles, trucks and other vehicles out there on the streets that would seriously need some refurbishment. You have taken on this job, ripping off a few dollars from a major TV station along the way.
    Of course, there’s a lot of work to do, and you have decided that it’s getting too much. Therefore you want to have the various jobs like painting, interior decoration and so on done by garages. Unfortunately, those garages are very specialized, so you need different garages for different jobs. More so, they tend to charge you the more the better the overall appearance of the car is. That is, a painter might charge more for a car whose interior is all leather. As those “surcharges” depend on what job is done and which jobs have been done before, you are currently trying to save money by finding an optimal order for those jobs.
    Problem
    Individual jobs are numbered 1 through n. Given the base price p for each job and a surcharge s (in US)foreverypairofjobs(i,j)withi!=j,meaningthatyouhavetopayadditionals for job i, if and only if job j was completed before, you are to compute the minimum total costs needed to finish all jobs.
    Input
    The first line contains the number of scenarios. For each scenario, an integer number of jobs n, 1 <= n <= 14, is given. Then follow n lines, each containing exactly n integers. The i-th line contains the surcharges that have to be paid in garage number i for the i-th job and the base price for job i. More precisely, on the i-th line, the i-th integer is the base price for job i and the j-th integer (j != i) is the surcharge for job i that applies if job j has been done before. The prices will be non-negative integers smaller than or equal to 100000.
    Output
    The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line: “You have officially been pimped for only p” with p being the minimum total price. Terminate the output for the scenario with a blank line.  
    Sample Input  
    2  
    2  
    10 10  
    9000 10  
    3  
    14 23 0  
    0 14 0  
    1000 9500 14  
    Sample Output  
    Scenario #1:  
    You have officially been pimped for only
    30

    Scenario #2:
    You have officially been pimped for only $42

    注意题目:每次做一个job都要把之前做过的所有job额外的费用加上

    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <algorithm>
    #include <math.h>
    #include <stdio.h>
    
    using namespace std;
    #define MAX 100000000
    int dp[1<<15][15];
    int a[15][15];
    int ans;
    int n;
    int main()
    {
        int t;
        scanf("%d",&t);
        int cas=0;
        while(t--)
        {
            scanf("%d",&n);
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    scanf("%d",&a[i][j]);
                }
            }
            int state=(1<<n)-1;
            for(int i=0;i<=state;i++)
                for(int j=0;j<n;j++)
                    dp[i][j]=MAX;
            for(int i=0;i<n;i++)
                dp[1<<i][i]=a[i][i];
            for(int i=1;i<=state;i++)
            {
                for(int j=0;j<n;j++)
                {
                    if((1<<j)&i)
                    {
                        int num=a[j][j];
                        for(int k=0;k<n;k++)
                        {
                            if(k!=j&&((1<<k)&i))
                                num+=a[j][k];
                        }
                        for(int p=0;p<n;p++)
                        {
                            if(p!=j&&((1<<p)&i))
                                dp[i][j]=min(dp[i][j],num+dp[i-(1<<j)][p]);
                        }
                    }
    
                }
            }
            ans=MAX;
            for(int i=0;i<n;i++)
                ans=min(ans,dp[state][i]);
            printf("Scenario #%d:
    ",++cas);
            printf("You have officially been pimped for only $%d
    
    ",ans);
    
        }
        return 0;
    }
  • 相关阅读:
    奇怪的肚疼
    惊喜:vs2005 和 msdn 中文版 已经提供Subscriber 下载,MSDN全球订户可以下中文版爽了
    英语构语法(前、后缀部分)
    TSQL中的递归 作者:Alexander Kozak
    筹划向 Visual Studio 2005 导航控件的迁移 作者:Dave Donaldson Steven DeWalt
    Atlas客服端文件介绍
    Chinese lunar calendar for www.live.com
    帮助解决网页和JS文件中的中文编码问题的小工具
    ADO.NET 2.0 功能一览 作者:Bob Beauchemin
    Prototype.js 1.4中文使用手册PDF版下载
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228758.html
Copyright © 2011-2022 走看看