HDOJ 题目1619 Unidirectional TSP(DP输出路径啊)
来源:互联网 发布:淘宝lol代练 编辑:程序博客网 时间:2024/06/10 22:14
Unidirectional TSP
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 427 Accepted Submission(s): 212
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.
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
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 63 4 1 2 8 66 1 8 2 7 45 9 3 9 9 58 4 1 3 2 63 7 2 8 6 45 63 4 1 2 8 66 1 8 2 7 45 9 3 9 9 58 4 1 3 2 63 7 2 1 2 32 29 10 9 10
Sample Output
1 2 3 4 4 5161 2 1 5 4 5111 119
Source
UVA
Recommend
8600 | We have carefully selected several similar problems for you: 1591 1702 1683 3337 1111
题目大意:每列一个数,从左走到右的走过的最小的和,可以有三个方向走如图上,注意当在第1行可以向上走到底n行,第n行可以向下走到第1行,输出路径,如果有多个路径输出最小的字典序的,
周赛题,HDU的数据比较水,我的姿势HDU可过,UVA一直过不了= =,先留着代码。。。
正资:http://www.myexception.cn/program/1393700.html
ac代码
#include<stdio.h>#include<iostream>#include<stdlib.h>#include<algorithm>#include<string.h>#include<math.h>#define INF 0x3f3f3f3f3f3f3f#define LL long longusing namespace std;#define min(a,b) (a<b?a:b)LL map[1010][1010];struct s{ int x,y;}pre[1010][1010];LL dp[1010][1010];int ans[1010];int n,m;void DP(){ int i,j; /*dp[1][1]=map[1][1]; dp[1][2]=map[1][2]+dp[1][1]; pre[1][2].x=1; pre[1][2].y=1; dp[2][2]=map[2][2]+dp[1][1]; pre[2][2].x=1; pre[2][2].y=1; dp[n][2]=map[n][2]+dp[1][1]; pre[n][2].x=1; pre[n][2].y=1;*/ for(i=1;i<=n;i++) dp[i][1]=map[i][1]; for(j=2;j<=m;j++) { for(i=1;i<=n;i++) { LL aa,bb,cc; if(i==1) { aa=dp[n][j-1]; } else aa=dp[i-1][j-1]; bb=dp[i][j-1]; if(i==n) cc=dp[1][j-1]; else cc=dp[i+1][j-1]; LL temp; temp=min(aa,min(bb,cc)); dp[i][j]=temp+map[i][j]; int pa,pb,pc; pa=pb=pc=INF; if(temp==aa) { if(i==1) { pa=n; } else pa=i-1; } if(temp==cc) { if(i==n) pc=1; else { pc=i+1; } } if(temp==bb) { pb=i; } pre[i][j].x=min(pa,min(pb,pc)); pre[i][j].y=j-1; } }}void GET(){ int i,j; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { scanf("%lld",&map[i][j]); dp[i][j]=INF; pre[i][j].x=-1; pre[i][j].y=-1; } }}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { GET(); DP(); int flag=0; LL minn=INF; int i,j; for(i=1;i<=n;i++) { if(dp[i][m]<minn) { minn=dp[i][m]; flag=i; } } int x=flag,y=m; memset(ans,0,sizeof(ans)); while(1) { int tx,ty; ans[y]=x; if(y==1) break; tx=pre[x][y].x; ty=pre[x][y].y; x=tx; y=ty; } printf("%d",ans[1]); for(i=2;i<=m;i++) { printf(" %d",ans[i]); } printf("\n"); printf("%lld\n",minn); }}
0 0
- HDOJ 题目1619 Unidirectional TSP(DP输出路径啊)
- uva 116 Unidirectional TSP 单向TSP 问题,经典dP(路径输出注意规划方向)
- Unidirectional TSP(dp)
- HDU 1619 Unidirectional TSP(dp)
- UVA - 116 - Unidirectional TSP (简单DP + 打印路径)
- HDU-1619 Unidirectional TSP dp
- hdu-1619 Unidirectional TSP(dp)
- 116 - Unidirectional TSP(DP)
- UVa116 Unidirectional TSP(DP)
- uva116 unidirectional TSP 单向TSP (DP)
- uva 116 Unidirectional TSP dp + 打印路径
- HDU 1619:Unidirectional TSP【dfs & 回忆路径】
- HDU 1619 Unidirectional TSP (dp,dfs)
- hdu 1619 Unidirectional TSP【记忆化dp】
- uva 116(Unidirectional TSP)(DP)
- uva116 - Unidirectional TSP(简单DP)
- uva 116 Unidirectional TSP (dp)
- UVa 116 Unidirectional TSP(DP)
- UVA 题目624 CD(01背包输出路径啊)
- 深度学习:对实际问题解决者的简要指导
- Android中View 绘制机制
- VS修改项目解决方案名称
- 《java并发编程实战》读书笔记——线程池
- HDOJ 题目1619 Unidirectional TSP(DP输出路径啊)
- 6大设计原则(一)---单一职责原则
- OC基础篇:day01&day02.
- 属性访问器
- pstore 从oops发生到保存dmesg的过程
- C-052.static与extern对全局变量的作用
- Template(模板)设计模式
- 卫星输入任务的语义解析
- os之进程管理学习记录