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. max_allowed_packet

    http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_in SELECT val1 FROM tbl1 W ...

  2. Windows 下 Nginx + PHP + Xdebug + PHPStorm 调试环境配置

    前期条件:安装好 Nginx.PHP.PHPStorm,使得可以正常访问 一.为 PHP 安装 Xdebug 到 Xdebug 的官网(http://xdebug.org/download.php)下 ...

  3. 最有用的Linux命令行使用技巧集锦

    最近在Quora上看到一个问答题目,关于在高效率Linux用户节省时间Tips.将该题目的回答进行学习总结,加上自己的一些经验,记录如下,方便自己和大家参考. 下面介绍的都是一些命令行工具,这些工具在 ...

  4. membership 在web.config中配置信息

    <?xml version="1.0" encoding="utf-8"?><configuration> <configSect ...

  5. Python学习笔记——1——基础知识

    1.1.变量和算法 python语言很类似人类语言,变量不需要定义类型.比如: 整型 字符串类型 数组 Java int a=12 String s="test" String[] ...

  6. Erlang ERTS的Trap机制的设计及其用途

    出处:http://mryufeng.iteye.com/blog/334744 erlang的trap机制在实现中用的很多,在费时的BIF操作中基本上都可以看到.它的实现需要erl vm的配合.它的 ...

  7. Magento Error – The directory is not writable by server.

    When trying to use the insert image functionality in Magento if you receive an error saying: “The di ...

  8. Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)

    题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...

  9. JQ实现accordion(可折叠)效果

    先看效果--这个就是手风琴的效果:          原理:首先默认section1下面的dd可见,其他的全部隐藏:当点击某个obj时候,  快速隐藏全部的dd,然后只有obj.NEXT().show ...

  10. http://blog.csdn.net/tiantiandjava/article/details/46777051

    http://blog.csdn.net/tiantiandjava/article/details/46777051