拓扑序……好些玄妙

Description

Mr. F. wants to get a document be signed by a minister. A minister signs a document only if it is approved by his ministry. The ministry is an M-floor building with floors numbered from 1 to M, 1<=M<=100. Each floor has N rooms (1<=N<=500) also numbered from 1 to N. In each room there is one (and only one) official. 
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

The first line of an input file contains two integers, separated by space. The first integer M represents the number of floors in the building, and the second integer N represents the number of rooms per floor. Each of the next M lines contains N integers separated with spaces that describe fees (the k-th integer at l-th line is the fee required by the official working in the k-th room at the l-th floor).

Output

You should print the numbers of rooms (one per line) in the order they should be visited to approve the document in the cheapest way. If there are more than one way leading to the cheapest cost you may print an any of them.

Sample Input

3 4
10 10 1 10
2 2 2 10
1 10 10 10

Sample Output

3
3
2
1
1

Hint

You can assume that for each official there always exists a way to get the approval of a document (from the 1st floor to this official inclusively) paying no more than 10^9. 
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

题目大意

有一个带权矩阵,可以从上面任意一点进入,从下面任意一点走出;问路径上权值和的最小值。

题目分析

题目很简单,就是普通的dp做两次……

只不过想记录一下这个dp拓扑序的问题。

对于点$(x,y)$需要先从上面转移,再从两边转移。虽然看上去随便怎么样好像都一样、会根据最优解覆盖,但是实际上是要考虑这个dp的拓扑序的……

=

 #pragma GCC optimize(2)
#include<cstring>
#include<cctype>
#include<cstdio>
const int maxn = ; int f[maxn][maxn],a[maxn][maxn];
int n,m,g[maxn][maxn],cnt; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch = getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch = getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
void dfs(int layer, int x)
{
if (layer!=&&!g[layer][x]) dfs(layer-, x);
else if (g[layer][x]) dfs(layer, x+g[layer][x]);
printf("%d\n",x);
}
int main()
{
register int i,j,tt = ;
n = read(), m = read();
for (i=; i<=n; i++)
for (j=; j<=m; j++)
a[i][j] = read(), f[i][j] = 2e9;
f[n][] = 2e9;
for (i=; i<=m; i++)
f[][i] = a[][i];
for (i=; i<=n; i++)
{
for (j=; j<=m; j++)
{
if (f[i][j] > f[i-][j]+a[i][j]){
f[i][j] = f[i-][j]+a[i][j];
g[i][j] = ;
}
if (j!=&&f[i][j] > f[i][j-]+a[i][j]){
f[i][j] = f[i][j-]+a[i][j];
g[i][j] = -;
}
}
for (j=m-; j>=; j--)
{
if (f[i][j] > f[i][j+]+a[i][j]){
f[i][j] = f[i][j+]+a[i][j];
g[i][j] = ;
}
}
}
for (i=; i<=m; i++)
if (f[n][tt] > f[n][i]) tt = i;
dfs(n, tt);
return ;
}

END

最新文章

  1. C#面向对象设计模式纵横谈——5.Factory Method 工厂方法模式(创建型模式)
  2. log4j.properties配置
  3. jQuery的常用函数扩展
  4. C++学习3
  5. 帝国cms无法注册登录快速解决方法 附路径
  6. Windows环境下安装Redis
  7. 自己动手实现Queue
  8. 【转】AFNetworking之于https认证
  9. Output Limit Exceed是什么情况引起的
  10. 我的three.js学习记录(二)
  11. springboot+thymeleaf(2)
  12. VS中自定义类模版
  13. 全文检索-Elasticsearch (二) CURD
  14. 富文本编辑器 kindeditor
  15. PHP中$_POST和$_GET的用法
  16. NFine中权限判断出错的问题
  17. MYSQL如何解决幻读
  18. tp5 migrate数据库迁移工具
  19. Android——远程存储器存储:JDK方式和Volley框架的get和post
  20. Linux nc命令用法收集

热门文章

  1. GYM 101673G(dp)
  2. python基本的数据类型
  3. Storm编程入门API系列之Storm的Topology多个tasks数目控制实现
  4. kafka java api生产者
  5. vue样式操作与事件绑定
  6. this,call,apply,bind浅析
  7. 关于js对象中的,属性的增删改查问题
  8. Ubuntu下HTTPS配置
  9. DVWA之跨站请求伪造(CSRF)
  10. H5移动端图片裁剪(base64)