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 only30
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:\n",++cas);
printf("You have officially been pimped for only $%d\n\n",ans);
}
return 0;
}
HOJ 2317 Pimp My Ride(状态压缩DP)的更多相关文章
- HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))
Cleaning Robot Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4264 Accepted: 1713 Descri ...
- 学习笔记:状态压缩DP
我们知道,用DP解决一个问题的时候很重要的一环就是状态的表示,一般来说,一个数组即可保存状态.但是有这样的一些题 目,它们具有DP问题的特性,但是状态中所包含的信息过多,如果要用数组来保存状态的话需要 ...
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- [知识点]状态压缩DP
// 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...
- HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP
题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...
- DP大作战—状态压缩dp
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
- 状态压缩dp问题
问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...
- BZOJ-1226 学校食堂Dining 状态压缩DP
1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...
随机推荐
- QWidget:Must construct a QApplication before a QWidget。
异常描述: 用PyQt开发的界面程序,再新增加了几个module后, 在eric6开发环境下启动后什么都没出现,什么错误提示也都没有, 在控制台下:python XXXX.py 后显示 QWidg ...
- python 脚本检测python 版本
通过sys 模块的sys_info可以返回当前python 的版本信息, 其返回值是一个元组, 比如(2, 6, 6, 'final', 0); 表示当前版本为2.6.6 , 我们可以利用这个变量的值 ...
- MetaSploit Pro 下载地址
Windows: https://downloads.metasploit.com/data/releases/metasploit-latest-windows-installer.exe Linu ...
- poj 3414 Pots(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...
- centos7 systemctl
下机为systemctl指令systemctl enable *.service #开机运行服务systemctl disable *.service #取消开机运行systemctl start * ...
- Java精选笔记_JDBC
JDBC 概述 什么是JDBC JDBC全称是Java数据库连接(Java Database Connectivity),应用程序可通过这套API连接到关系数据库,并使用SQL语句来完成对数据库中数据 ...
- Leetcode: Best Time to Buy and Sell Stock I, II
思路: 1. 算法导论讲 divide and conquer 时, 讲到过这个例子. 书中的做法是先让 price 数组减去一个值, 然后求解最大连续子数组的和. 分治算法的复杂度为 o(nlogn ...
- python处理文本文件
在测试任务过程中都或多或少遇到自己处理文本文件的情况. 举个栗子: 客户端测试从异常日志中收集有用信息. 后端测试需要创建各种规则的压力的词表. ... 这里给大家分享一个使用python脚本处理文本 ...
- linux上如何快速删除一个目录
在linux中删除一个目录很简单,很多人还是习惯用rmdir,不过一旦目录非空,就陷入深深的苦恼之中,现在使用rm -rf命令即可解决.直接rm就可以了,不过要加两个参数-rf 即:rm -rf ...
- SPI接口功能描述