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. word 使用宏批量设置表格

    Sub ChangeTable() Application.Browser.Target = wdBrowseTable To ActiveDocument.Tables.Count ActiveDo ...

  2. heapsort

    Introduction to Algorithms Third Edition The (binary) heap data structure is an array object that we ...

  3. data abstractions 数据抽象

    Computer Science An Overview _J. Glenn Brookshear _11th Edition In this chapter we investigate how d ...

  4. Qt系统托盘

    Qt的系统托盘的使用,可比mfc中好多了!他封装了一个专门的QSystemTrayIcon类,建立系统托盘图标.其实在Qt提供的示例程序已经很不错了,$QTDIR\examples\desktop\s ...

  5. EF6 CodeFirst 启用Migration,常用命令

    Enable-Migrations –EnableAutomaticMigrationsAdd-Migration [MigrationName] [-Force]Update-Database –T ...

  6. python入门 2014-3-21

    刚吃完饭,写一会python 准备去上课,哇咔咔! 1.python是动态类型语言,也就是说 不需要预先声明变量的类型. 不支持 自增++ , 自减--

  7. C++经典编程题#1:含k个3的数

    总时间限制:  1000ms 内存限制:  65536kB 描述 输入两个正整数 m 和 k,其中1 < m < 100000,1 < k < 5 ,判断 m 能否被19整除, ...

  8. http和网页设计

    .基本概念: CGI(Common Gate Interface,通用网关接口) HTML均是静态网页,它无法实现一些复杂的功能,而CGI可以为我们实现. get方式提交表单: 当表单被发送到服务器断 ...

  9. HBASE架构解析(二)

    http://www.blogjava.net/DLevin/archive/2015/08/22/426950.html HBase读的实现 通过前文的描述,我们知道在HBase写时,相同Cell( ...

  10. TST fall detection database

    http://www.tlc.dii.univpm.it/blog/databases4kinect#IDFall2