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. memcached使用详解

    不错的文章 http://www.ttlsa.com/memcache/memcached-description/

  2. Python操作MySQL以及中文乱码的问题

    Python操作MySQL需要安装Python-MySQL可以从网上搜索一下,和一般的Python包一样安装 安装好之后,模块名字叫做MySQLdb ,在Window和Linux环境下都可以使用,试验 ...

  3. JAVA基础讲义

    一.安装JDK 第一步:双击JDK的exe文件. JDK(Java开发包),JRE(Java运行环境) 第二步:配置 path:jdk的根目录,jdk下的bin目录(两个目录之间记得用分号隔开) cl ...

  4. 一个比较轻巧好用的js分页插件,可ajax可url

    var pageNav = pageNav || {}; pageNav.fn = null; pageNav.pre = "pre"; pageNav.next = " ...

  5. php项目域名绑定和替换

    ---恢复内容开始--- 在apache的httpd.conf里面找到 DocumentRoot "E:/xampp/htdocs"<Directory "E:/x ...

  6. jfinal框架教程-学习笔记(二)

    上一节介绍了jfinal框架的简单搭建,这节通过一个小例子了解jfinal的结构和特点 先上图 1.建数据库(我用的是oracle数据库,其他的相对也差不多) -- Create table crea ...

  7. Mongo中的数据类型

    一.null null用于表示空值或者不存在的字段 {"X" : null} 二.布尔型 布尔类型有两个值true和false {"x" : true} 三.数 ...

  8. 1014 C语言文法

    <程序> -> <外部声明> | <程序> <外部声明> <外部声明> -> <函数定义> | <声明> ...

  9. json 增删改 加 排序

    <script type="text/javascript"> var json = { "age":24, "name":&q ...

  10. MVC HTML辅助类常用方法记录

    (1)@Html.DisplayNameFor(model => model.Title)是显示列名, (2)@Html.DisplayFor(modelItem => item.Titl ...