There are a row of houses, each house can be painted with three colors red,
blue and green. The cost of painting each house with a certain color is different.
You have to paint all the houses such that no two adjacent houses have the same color.
You have to paint the houses with minimum cost. How would you do it?
Note: Painting house-1 with red costs different from painting house-2 with red.
The costs are different for each house and each color.

一个dp,f(i,j)表示前i个house都paint了且第i个house paint成color_j的最小cost。

 int paint(int N, int M, int[][] cost) {
int[][] res = new int[N+1][M];
for (int t=0; t<M; t++) {
res[0][t] = 0;
}
for (int i=1; i<N; i++) {
for (int j=0; j<M; j++) {
res[i][j] = Integer.MAX_VALUE;
}
}
for (int i=1; i<=N; i++) {
for (int j=0; j<M; j++) {
for (int k=0; k<M; k++) {
if (k != j) {
res[i][j] = Math.min(res[i][j], res[i-1][k]+cost[i-1][j]); //
}
}
}
}
int result = Integer.MAX_VALUE;
for (int t=0; t<M; t++) {
result = Math.min(result, res[N][t]);
}
return result;
}

F面经:painting house的更多相关文章

  1. Mysql_以案例为基准之查询

    查询数据操作

  2. hdu 3685 10 杭州 现场 F - Rotational Painting 重心 计算几何 难度:1

    F - Rotational Painting Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  3. ARC 062 F - Painting Graphs with AtCoDeer 割点 割边 不动点 burnside引理

    LINK:Painting Graphs with AtCoDeer 看英文题面果然有点吃不消 一些细节会被忽略掉. 问每条边都要被染色 且一个环上边的颜色可以旋转. 用c种颜色有多少本质不同的方法. ...

  4. ARC062 - F. Painting Graphs with AtCoDeer (Polya+点双联通分量)

    似乎好久都没写博客了....赶快来补一篇 题意 给你一个 \(n\) 个点 , 没有重边和自环的图 . 有 \(m\) 条边 , 每条边可以染 \(1 \to k\) 中的一种颜色 . 对于任意一个简 ...

  5. Codeforces Gym 100342C Problem C. Painting Cottages 转化题意

    Problem C. Painting CottagesTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  6. Codeforces Gym 100342C Problem C. Painting Cottages 暴力

    Problem C. Painting CottagesTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1 ...

  7. Painting The Wall 期望DP Codeforces 398_B

    B. Painting The Wall time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. cf509B Painting Pebbles

    B. Painting Pebbles time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9.  D - 粉碎叛乱F - 其他起义

    D - 粉碎叛乱 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

随机推荐

  1. 初步理解Java的三大特性——封装、继承和多态

    声明:整理自网络,如有雷同,请联系博主处理 一.封装 封装从字面上来理解就是包装的意思,专业点就是信息隐藏,是指利用抽象数据类型将数据和基于数据的操作封装在一起,使其构成一个不可分割的独立实体,数据被 ...

  2. Git相关的项目

    1.posh-git Git的PowerShell扩展 项目地址: https://github.com/dahlbyk/posh-git 可以用psget快速安装扩展模块,psget下载安装地址 h ...

  3. Jetbrains phpstorm pycharm 免费授权注册码

    通过授权服务器授权 jetbrains是一家专门做IDE的软件公司,软件也非常好用,但是授权特别贵,下面就说说免费的方式,就是使用授权服务器,地址:http://idea.qinxi1992.cn 自 ...

  4. 我的Mac软件集

    哥总算用上了mac,走上了小康了 phpstorm:个人工作中最常用的软件了 pycharm:这个写python的,和phpstorm是一家开发的 sourceTree:git 可视化工具 xcode ...

  5. iframe框架里镶嵌页面;<marquee>:滚动效果;<mark>做标记;内联、内嵌、外联;选择器

    标签:①②③④⑤⑥⑦★ 框架: 一.frameset:(框架集) 1.如果使用框架集,当前页面不能有body 2.cols="300,*":左右拆分,左边宽300,右边宽剩余 3. ...

  6. linux OSlab4 添加自定义系统调用

    http://blog.csdn.net/ly01kongjian/article/details/8947285 http://www.cnblogs.com/hoys/archive/2011/0 ...

  7. 在 mac os 上搭建 git server

    前言:之前学习了如何使用 git 后,一直想搭建一个本机搭建一个 git server 的,一开始不知道走了弯路用了 gitosis,折腾了我好几天都没配置好.昨晚查资料发现 gitosis 早就过时 ...

  8. Archiver 浅析

    归档是一个过程,即用某种格式来保存一个或多个对象,以便以后还原这些对象.通常,这个过程包括将(多个)对象写入文件中,以便以后读取该对象. 两种归档数据的方法:属性列表和带键值的编码. 属性列表局限性很 ...

  9. python 输出乱码

    在Python中有两种默认的字符串:str和unicode.在Python中一定要注意区分“Unicode字符串”和“unicode对象”的区别.后面所有的“unicode字符串”指的都是python ...

  10. javaScript没有块级作用域

    1.如下,变量i,j,k 的作用域是相同的. function test(obj){ var i= 0; if(typeof obj == "object"){ var j = 0 ...