Unidirectional TSP

Problem Description
Problems that require minimum paths through some domain appear in many different areas of computer science. For example, one of the constraints in VLSI routing problems is minimizing wire length. The Traveling Salesperson Problem (TSP) -- finding whether all the cities in a salesperson's route can be visited exactly once with a specified limit on travel time -- is one of the canonical examples of an NP-complete problem; solutions appear to require an inordinate amount of time to generate, but are simple to check.

This problem deals with finding a minimal path through a grid of points while traveling only from left to right.

Given an m*n matrix of integers, you are to write a program that computes a path of minimal weight. A path starts anywhere in column 1 (the first column) and consists of a sequence of steps terminating in column n (the last column). A step consists of traveling from column i to column i+1 in an adjacent (horizontal or diagonal) row. The first and last rows (rows 1 and m) of a matrix are considered adjacent, i.e., the matrix ``wraps'' so that it represents a horizontal cylinder. Legal steps are illustrated below.

The weight of a path is the sum of the integers in each of the n cells of the matrix that are visited.

For example, two slightly different 5*6 matrices are shown below (the only difference is the numbers in the bottom row).

The minimal path is illustrated for each matrix. Note that the path for the matrix on the right takes advantage of the adjacency property of the first and last rows.

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 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.

For each specification the number of rows will be between 1 and 10 inclusive; the number of columns 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-weight path, and the second line is the cost of a minimal path. The path consists of a sequence of n integers (separated by one or more spaces) representing the rows that constitute the minimal path. If there is more than one path of minimal weight the path that is lexicographically smallest should be output.

 
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
 
题目大意:给定一个n*m的矩阵,从第一列的任何一个位置出发,到最后一列的任何一个位置终止,每一次只能往下一列走,走直线或者对角线,求出最小权值,以及打印字典序最小路径。
 
分析:状态转移很简单,就是取右上,右,右下的最小值。但因为要打印路径,从前往后打印,所以最左边应该是最终结果,就要从右往左循环,这样状态转移也就变成了取左上,左,左下的最小值。具体见代码。
 
代码如下:
 # include<stdio.h>
# include<string.h>
# include<iostream>
using namespace std;
# define inf 0xffffff
int map[][];
int p[][];
int s[];
int main()
{
int m,n,i,j;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(p,-,sizeof(p));
for(i=; i<=m; i++)
for(j=; j<=n; j++)
scanf("%d",&map[i][j]);
for(j=n-; j>=; j--) //从后往前计算,因为要打印路径
for(i=; i<=m; i++)
{
int b=i,a,c,d,e=-; //a,b,c分别为向左上,向左,向左下的坐标
if(i>=) a=i-;
else a=m;
if(i<m) c=i+;
else c=;
d = min(min(map[a][j+],map[b][j+]),map[c][j+]); //取最小值
map[i][j] += d;
if(d==map[a][j+]) e=a;
if(d==map[b][j+]&&(e==-||(e!=-&&b<e))) e=b; //e的作用当两行的数字相同时去小的一个
if(d==map[c][j+]&&(e==-||(e!=-&&c<e))) e=c;
p[i][j] = e; //记录路径
}
int flag = ;
int sum =inf;
for(i=; i<=m; i++)
if(map[i][] < sum)
{
sum = map[i][];
flag=i;
}
printf("%d",flag);
flag = p[flag][];
for(i=; i<=n && flag != -; i++)
{
printf(" %d",flag);
flag = p[flag][i];
}
printf("\n%d\n",sum);
}
return ;
}

HDU 1619 Unidirectional TSP(单向TSP + 路径打印)的更多相关文章

  1. UVA116 Unidirectional TSP 单向TSP

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

  2. UVa116 (单向TSP,多决策问题)

    /*----UVa1347 单向TSP 用d(i,j)表示从格子(i,j)出发到最后一列的最小开销 则在(i,j)处有三种决策,d(i,j)转移到d(i-1,j+1),d(i,j+1),d(i+1,j ...

  3. ZOJ 1076 Gene Assembly(LIS+路径打印 贪心)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=76 题目大意:在一个DNA上,给定许多基因的起始位置和结束位置,求出这 ...

  4. L2-001 紧急救援 (25 分) (最短路+路径打印)

    链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805073643683840 题目: 作为一个城市的应急救援队伍的负 ...

  5. HDU 6311 Cover (无向图最小路径覆盖)

    HDU 6311 Cover (无向图最小路径覆盖) Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...

  6. Minimum Transport Cost HDU1385(路径打印)

    最短路的路径打印问题 同时路径要是最小字典序 字典序用floyd方便很多 学会了两种打印路径的方法!!! #include <stdio.h> #include <string.h& ...

  7. POJ 3984 迷宫问题(简单bfs+路径打印)

    传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  8. 代码实现:从键盘输入接收一个文件夹路径,打印出该文件夹下所有的.java文件名

    package com.loaderman.test; import java.io.File; import java.io.FileReader; import java.util.Scanner ...

  9. uva 116 Unidirectional TSP【号码塔+打印路径】

    主题: uva 116 Unidirectional TSP 意甲冠军:给定一个矩阵,当前格儿童值三个方向回格最小值和当前的和,就第一列的最小值并打印路径(同样则去字典序最小的). 分析:刚開始想错了 ...

随机推荐

  1. leetcode—Populating Next Right Pointers in Each Node

    1.题目描述 Given a binary tree   struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLink ...

  2. DOCTYPE与浏览器模式详解(标准模式&混杂模式)

    关于渲染模式: 在多年以前(IE6诞生以前),各浏览器都处于各自比较封闭的发展中(基本没有兼容性可谈).随着WEB的发展,兼容性问题的解决越来 越显得迫切,随即,各浏览器厂商发布了按照标准模式(遵循各 ...

  3. iOS网络编程(三) 异步加载及缓存图片---->SDWebImage

    @SDWebImage提供一个UIImageView的类别以支持加载来自网络的远程图片.具有缓存管理.异步下载.同一个URL下载次数控制和优化等特征. @SDWebImage的导入1.https:// ...

  4. Unity3D 导入贴图、模型等资源文件时自动设置参数

    脚本继承至AssetPostprocessor, 存放在Editor目录下! using UnityEngine; using System.Collections; using UnityEdito ...

  5. ZooKeeper场景实践:(6)集群监控和Master选举

    1. 集群机器监控 这通经常使用于那种对集群中机器状态,机器在线率有较高要求的场景,可以高速对集群中机器变化作出响应.这种场景中,往往有一个监控系统,实时检測集群机器是否存活. 利用ZooKeeper ...

  6. HDU 3259 Wormholes

    题意:就是给你一个n,m,t   n代表有多少个点.m代表有多少个双向的边  t代表的是虫洞.如今要你判读是否还能够穿越到过去的点 虫洞的意思是给你的边是单向的,而且是负权值(输入的时候是正数) 思路 ...

  7. 嵌入式Qt4.7.1安装详解

    嵌入式Qt 4.7.1安装移植过程详解环境:Ubuntu 12.04VMware 9.0 qt软件包:qt-everywhere-opensource-src-4.7.1.tar.gz (飞凌自带) ...

  8. [转] Understanding JavaScript’s async await

    PS:Promise的用处是异步调用,这个对象使用的时候,call then函数,传一个处理函数进去,处理异步调用后的结果 Promise<Action>这样的对象呢,异步调用后的结果是一 ...

  9. Linux环境下搭建Android开发环境

    最近在折腾linux.因为咱是搞安卓开发的,所以少不了需要搭建Android开发环境,就此小记,希望能给向我一样的开发者一点帮助!开干! 1.安装JDK 下载JDK包,得到的是类似于jdk-8u65- ...

  10. Objective-C语法快速参考(C# 和 Objective-C 语法的比较)

    大部分有一点其他平台开发基础的初学者看到XCode ,第一感想是磨拳擦掌,看到 Interface  Builder 之后,第一感想是跃跃欲试,而看到Objective-C 的语法,第一感想就变成就望 ...