POJ 2353 Ministry
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 4220 | Accepted: 1348 | Special Judge |
Description
A document is approved by the ministry only if it is signed by at least one official from the M-th floor. An official signs a document only if at least one of the following conditions is satisfied:
a. the official works on the 1st floor;
b. the document is signed by the official working in the room with the same number but situated one floor below;
c. the document is signed by an official working in a neighbouring room (rooms are neighbouring if they are situated on the same floor and their numbers differ by one).
Each official collects a fee for signing a document. The fee is a positive integer not exceeding 10^9.
You should find the cheapest way to approve the document.
Input
Output
Sample Input
3 4
10 10 1 10
2 2 2 10
1 10 10 10
Sample Output
3
3
2
1
1
题目大意:有一栋m层的房子,每层n个房间,如果该房间在第一层或者该房间的上一层的房间或者左右任意一个房间被访问才能访问该房间,问从第一层到最后一层走过的房间的权值和最小的路径。
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std; int fee[][];
int dp[][];
int path[][];
int ans[]; int main()
{
int m, n;
int s;
while(scanf("%d%d", &m, &n) != EOF)
{
memset(dp, , sizeof(dp));
memset(path, , sizeof(path));
memset(ans, , sizeof(ans));
for (int i = ; i <= m; i++)
{
for (int j = ; j <= n; j++)
{
scanf("%d", &fee[i][j]);
}
}
for (int i = ; i <= n; i++)
{
dp[][i] = fee[][i];
path[][i] = i;
}
for (int i = ; i <= m; i++)
{
dp[i][] = dp[i - ][] + fee[i][];
path[i][] = ;
for (int j = ; j <= n; j++)
{
if (dp[i - ][j] < dp[i][j - ])
{
dp[i][j] = fee[i][j] + dp[i - ][j];
path[i][j] = j;
}
else
{
dp[i][j] = fee[i][j] + dp[i][j- ];
path[i][j] = j - ;
}
}
for (int j = n - ; j > ; j--)
{
if (dp[i][j + ] + fee[i][j] < dp[i][j])
{
dp[i][j] = dp[i][j + ] + fee[i][j];
path[i][j] = j + ;
}
}
}
int temp = 0x7fffffff;
for (int i = ; i <= n; i++)
{
if (temp > dp[m][i])
{
temp = dp[m][i];
s = i;
}
}
int nCount = ;
int x = m;
ans[] = s;
while (x != )
{
nCount++;
ans[nCount] = path[x][ans[nCount - ]];
if (ans[nCount] == ans[nCount - ])
{
x--;
}
}
for (int i = nCount; i >=; i--)
{
printf("%d\n", ans[i]);
}
}
return ;
}
POJ 2353 Ministry的更多相关文章
- POJ 2353 DP
双向DP+记录路径. // by SiriusRen #include <stack> #include <cstdio> #include <cstring> u ...
- poj 动态规划题目列表及总结
此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...
- poj动态规划列表
[1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...
- POJ 动态规划题目列表
]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...
- poj 动态规划的主题列表和总结
此文转载别人,希望自己可以做完这些题目. 1.POJ动态规划题目列表 easy:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, ...
- [转] POJ DP问题
列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...
- POJ动态规划题目列表
列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...
- dp题目列表
此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...
- DP题目列表/弟屁专题
声明: 1.这份列表不是我原创的,放到这里便于自己浏览和查找题目. ※最近更新:Poj斜率优化题目 1180,2018,3709 列表一:经典题目题号:容易: 1018, 1050, 1083, 10 ...
随机推荐
- hdu 3861 The King’s Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- 在2017年,如何将你的小米4刷上Windows 10 mobile?(后附大量图赏)
众多攻略集大成者!资深软粉亲测有效! 参考教程: http://bbs.xiaomi.cn/t-11814358 http://bbs.xiaomi.cn/t-11736827 问:刷机前,我需要做什 ...
- BZOJ 2851: 极限满月 虚树 or 树链的并
2851: 极限满月 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 170 Solved: 82[Submit][Status][Discuss] ...
- thinkphp 跳转
1 $this -> redirect('index',array('type'=>2,'id'=>0)); //直接跳转 2 $this->success('提交失 ...
- MySql查询时间段的方法
本文实例讲述了MySql查询时间段的方法.分享给大家供大家参考.具体方法如下: MySql查询时间段的方法未必人人都会,下面为您介绍两种MySql查询时间段的方法,供大家参考. MySql的时间字段有 ...
- mysq--索引模块
问题:为什么要 使用索引? --->快速查询数据,但是仅仅这么回答,就是不专业的!!! 应该要分为数据量少的时候,不适用索引,走全表扫描的话,查询速率也是很快的 数据量大的话,使用索引,查询速率 ...
- (转发)IOS高级开发~Runtime(三)
11.系统类的方法实现部分替换 - (void) methodExchange { Method m1 = class_getInstanceMethod([NSStringclass],@selec ...
- ubuntu 16.04 连接 wifi
我的电脑是win10+ubuntu16.04双系统.在ubuntu下无法连接wifi,一直用usb连接的手机流量,不太方便.现在来用安装无线驱动,顺便翻个墙. https://blog.csdn.ne ...
- HDU-4848-Such Conquering
这题就是深搜加剪枝,有一个很明显的剪枝,因为题目中给出了一个deadline,所以我们一定要用这个deadline来进行剪枝. 题目的意思是求到达每个点的时间总和,当时把题看错了,卡了好久. 剪枝一: ...
- 不依赖Hibernate的万能BaseDao---模仿了Hibernate底层的原理
今天写了个万能的BaseDao:有了这个BaseDao以后的Dao层直接继承这个BaseDao就能直接操作数据库了,增删改查,这是一个简易的Hibernate模型.写这个BaseDao的原因是最近在学 ...