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

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

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int maxn = + , maxm = + ;
  6. const int INF = 0x3f3f3f3f;
  7.  
  8. int n, m;
  9. int val[maxm][maxn], dp[maxm][maxn];
  10. int Next[maxm][maxn];
  11.  
  12. int read() {
  13. int q = , f = ; char ch = ' ';
  14. while (ch<'' || ch>'') {
  15. if (ch == '-') f = -;
  16. ch = getchar();
  17. }
  18. while ('' <= ch && ch <= '') {
  19. q = q * + ch - '';
  20. ch = getchar();
  21. }
  22. return q * f;
  23. }
  24.  
  25. int main()
  26. {
  27. //freopen("input.txt", "r", stdin);
  28. while (~scanf("%d%d", &m, &n)) {
  29. for (int i = ; i < m; i++) {
  30. for (int j = ; j < n; j++) {
  31. val[i][j] = read();
  32. }
  33. }
  34. //memset(dp, INF, sizeof(dp));
  35. int ans = INF, first = -;
  36.  
  37. for (int j = n - ; j >= ; j--) {
  38. for (int i = ; i < m; i++) {
  39. if (j == n - ) {
  40. dp[i][j] = val[i][j];
  41. }
  42. else {
  43. int row[] = { (i - + m) % m,i,(i + ) % m };
  44. sort(row, row + );
  45. dp[i][j] = INF;
  46. for (int k = ; k < ; k++) {
  47. if (dp[i][j] > dp[row[k]][j + ] + val[i][j]) {
  48. dp[i][j] = dp[row[k]][j + ] + val[i][j];
  49. Next[i][j] = row[k];
  50. }
  51. }
  52. }
  53. if (j == && dp[i][j] < ans) {
  54. ans = dp[i][j];
  55. first = i;
  56. }
  57. }
  58. }
  59.  
  60. printf("%d", first + );
  61. for (int i = Next[first][], j = ; j < n; i = Next[i][j], j++) {
  62. printf(" %d", i + );
  63. }
  64. printf("\n%d\n", ans);
  65. }
  66. return ;
  67. }

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. JavaScript中Map和ForEach的区别

    译者按: 惯用Haskell的我更爱map. 原文: JavaScript — Map vs. ForEach - What’s the difference between Map and ForE ...

  2. TP5.0 PHPExcel 数据表格导出导入(引)

    TP5.0 PHPExcel 数据表格导出导入(引) 今天看的是PHPExcel这个扩展库,Comporse 下载不下来,最后只能自己去github里面手动下载,但有一个问题就是下载下来的PHPExc ...

  3. jQuery插件开发进阶

    jQuery插件开发模式 软件开发过程中是需要一定的设计模式来指导开发的,有了模式,我们就能更好地组织我们的代码,并且从这些前人总结出来的模式中学到很多好的实践. 根据<jQuery高级编程&g ...

  4. POJ1743 Musical Theme(后缀数组 二分)

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 33462   Accepted: 11124 Description A m ...

  5. 【20190223】HTTP-知识点整理:HTTPS

    HTTPS:添加了加密及认证机制的HTTP HTTPS 并非是应用层的一种新协议.只是 HTTP 通信接口部分用SSL(Secure Socket Layer)和 TLS(Transport Laye ...

  6. 华为P20无线投屏到电脑 绝地求生投射电脑

    如今出门在外,必不可少的就是手机,如果没有了手机,每个人都会感觉没有安全感,感觉和世界失去了联系,我们每天每个人都在使用手机,但是作为华为手机用户的你,了解华为P20无线投屏到电脑是怎么操作的吗? 使 ...

  7. SuperMap-iServer过滤请求返回值

    目的: iServer发布的arcgis地图服务中,由于tileinfo参数为null,导致用arcgis-ios客户端开发的APP闪退.通过过滤器将get请求的返回值修改 代码: package c ...

  8. JVM调优日志解析分析

    一.调优参数设置 JVM的GC日志的主要参数包括如下几个: -XX:+PrintGC 输出GC日志 -XX:+PrintGCDetails 输出GC的详细日志 -XX:+PrintGCTimeStam ...

  9. Apktool(3)——Apktool的使用

    一.apktool的作用 安卓应用apk文件不仅仅是包含有resource和编译的java代码的zip文件,如果你尝试用解压工具(如好压)解压后,你将会获得classes.dex和resource.a ...

  10. Flume 1.7.0单机版安装

    下载解压到/usr/local/flume 配置环境变量 export FLUME_HOME=/usr/local/flume export FLUME_CONF_DIR=$FLUME_HOME/co ...