Problem UVA116-Unidirectional TSP

Accept: 7167  Submit: 56893
Time Limit: 3000 mSec

Problem Description

Input

The input consists of a sequence of matrix specifications. Each matrix specification consists of the row and column dimensions in that order on a line followed by m·n integers where m is the row dimension and n is the column dimension. The integers appear in the input in row major order, i.e., the first n integers constitute the first row of the matrix, the second n integers constitute the second row and so on. The integers on a line will be separated from other integers by one or more spaces. Note: integers are not restricted to being positive. There will be one or more matrix specifications in an input file. Input is terminated by end-of-file. Foreachspecificationthenumberofrowswillbebetween1and10inclusive; thenumberofcolumns will be between 1 and 100 inclusive. No path’s weight will exceed integer values representable using 30 bits.

 Output

Two lines should be output for each matrix specification in the input file, the first line represents a minimal-weightpath, andthesecondlineisthecostofaminimalpath. Thepathconsistsofasequence of n integers(separatedbyoneormorespaces)representingtherowsthatconstitutetheminimalpath. If there is more than one path of minimal weight the path that is lexicographically smallest should be output. Note: Lexicographically means the natural order on sequences induced by the order on their elements.
 

 Sample Input

5 6
3 4 1 2 8 6
6 1 8 2 7 4
5 9 3 9 9 5
8 4 1 3 2 6
3 7 2 8 6 4
5 6
3 4 1 2 8 6
6 1 8 2 7 4
5 9 3 9 9 5
8 4 1 3 2 6
3 7 2 1 2 3
2 2
9 10
9 10
 

Sample Output

1 2 3 4 4 5
16
1 2 1 5 4 5
11
1 1
19

题解:和数字三角形一样,水题。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn =  + , maxm =  + ;
const int INF = 0x3f3f3f3f; int n, m;
int val[maxm][maxn], dp[maxm][maxn];
int Next[maxm][maxn]; int read() {
int q = , f = ; char ch = ' ';
while (ch<'' || ch>'') {
if (ch == '-') f = -;
ch = getchar();
}
while ('' <= ch && ch <= '') {
q = q * + ch - '';
ch = getchar();
}
return q * f;
} int main()
{
//freopen("input.txt", "r", stdin);
while (~scanf("%d%d", &m, &n)) {
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
val[i][j] = read();
}
}
//memset(dp, INF, sizeof(dp));
int ans = INF, first = -; for (int j = n - ; j >= ; j--) {
for (int i = ; i < m; i++) {
if (j == n - ) {
dp[i][j] = val[i][j];
}
else {
int row[] = { (i - + m) % m,i,(i + ) % m };
sort(row, row + );
dp[i][j] = INF;
for (int k = ; k < ; k++) {
if (dp[i][j] > dp[row[k]][j + ] + val[i][j]) {
dp[i][j] = dp[row[k]][j + ] + val[i][j];
Next[i][j] = row[k];
}
}
}
if (j == && dp[i][j] < ans) {
ans = dp[i][j];
first = i;
}
}
} printf("%d", first + );
for (int i = Next[first][], j = ; j < n; i = Next[i][j], j++) {
printf(" %d", i + );
}
printf("\n%d\n", ans);
}
return ;
}

UVA116-Unidirectional TSP(动态规划基础)的更多相关文章

  1. Uva116 Unidirectional TSP

    https://odzkskevi.qnssl.com/292ca2c84ab5bd27a2a91d66827dd320?v=1508162936 https://vjudge.net/problem ...

  2. UVa-116 Unidirectional TSP 单向旅行商

    题目 https://vjudge.net/problem/uva-116 分析 设d[i][j]为从(i,j)到最后一列的最小开销,则d[i][j]=a[i][j]+max(d[i+1][j+1], ...

  3. uva 116 - Unidirectional TSP (动态规划)

    第一次做动规题目,下面均为个人理解以及个人方法,状态转移方程以及状态的定义也是依据个人理解.请过路大神不吝赐教. 状态:每一列的每个数[ i ][ j ]都是一个状态: 然后定义状态[ i ][ j ...

  4. UVA116 Unidirectional TSP 单向TSP

    分阶段的DAG,注意字典序的处理和路径的保存. 定义状态d[i][j]为从i,j 出发到最后一列的最小花费,转移的时候只有三种,向上,向下,或平移. #include<bits/stdc++.h ...

  5. HDU 1619 Unidirectional TSP(单向TSP + 路径打印)

    Unidirectional TSP Problem Description Problems that require minimum paths through some domain appea ...

  6. uva 116 Unidirectional TSP (DP)

    uva 116 Unidirectional TSP Background Problems that require minimum paths through some domain appear ...

  7. nyist oj 79 拦截导弹 (动态规划基础题)

    拦截导弹 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述 某国为了防御敌国的导弹突击.发展中一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以 ...

  8. UVA 116 Unidirectional TSP(dp + 数塔问题)

     Unidirectional TSP  Background Problems that require minimum paths through some domain appear in ma ...

  9. Problem C: 动态规划基础题目之数字三角形

    Problem C: 动态规划基础题目之数字三角形 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 208  Solved: 139[Submit][Sta ...

  10. UVA 116 Unidirectional TSP(DP最短路字典序)

    Description    Unidirectional TSP  Background Problems that require minimum paths through some domai ...

随机推荐

  1. Tri Tiling(hdu1143)

    Tri Tiling Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. css控制文字自动换行

    自动换行问题,正常字符的换行是比较合理的,而连续的数字和英文字符常常将容器撑大,挺让人头疼,下面介绍的是CSS如何实现换 行的方法 对于div,p等块级元素正常文字的换行(亚洲文字和非亚洲文字)元素拥 ...

  3. 读书笔记--Android Gradle权威指南(上)

    本篇文章已授权微信公众号 dasu_Android(大苏)独家发布 最近看了一本书<Android Gradle 权威指南>,对于 Gradle 理解又更深了,但不想过段时间就又忘光了,所 ...

  4. 小tips:Hbuilder编辑器开启less自动编译为css的方法

    1.首先,依次打开菜单栏->工具->预编译器设置,打开后是这样的: 2.然后点击新建. 3.文件后缀为.less触发命令地址就是lessc.cmd所在的地址,先用npm全局安装less, ...

  5. HDU1846 Brave Game

    Brave Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. ABP问题速查表

    如果你领导要让你一夜之间掌握ABP,并且用ABP撸一个项目出来,你很可能很快速的过了一遍ABP文档就马上动手干活了.那么这篇文章就很适合你. 这篇文章列出了很多ABP新手问的问题和解答.注:有些同学问 ...

  7. 安装docker17.06.0版本报错和解决方法

    本人在自己电脑的虚拟机里安装docker ce 17.06.0版本的时候报如下错误: [root@manager2 yum.repos.d]# yum install docker-ce-17.06. ...

  8. Linux 线程实现模型

    1.Linux 线程的调度实现可以有两种模型, 一种是完全由进程负责,进程内启动一个线程调度器,由进程内的线程调度器完成调度. 缺点是:(1)各个线程自己加主动释放cpu的流程 (2)进程可能阻塞,达 ...

  9. Spark应用【根据新df更新旧df】

    // 主键字段保持不变,再转换回来 var columnMap:Map[String, String] = Map() for(key <- keysOpt){ columnMap += (ke ...

  10. 监控mysql主从同步

    1,昨天看到shell一道面试题,需求如下: 监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员.提示:如果没主从同步环境,可以用下面文本放到文件里读取来模拟:阶段1:开发一个守护进 ...