首页 > 搜索 > DFS搜索 > HDU 1619 Unidirectional TSP-记忆化搜索-[解题报告] C++
2013
12-16

HDU 1619 Unidirectional TSP-记忆化搜索-[解题报告] C++

Unidirectional TSP

问题描述 :

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.

输入:

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

输出:

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.

样例输入:

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

样例输出:

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

贴几道记忆化搜索题:

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1208

思路:记忆话搜索,不过有一个trick就是如果map[i][j]==0并且不是终点,就直接返回0了。如果map[ i ][ j ]表示跳几格   dp[ i ][ j ] 表示有几种条法 , 其实就是一个子状态继承问题,如果map[ i ][ j ]为k, 那么 dp [ i+k ][ j ] 和dp[i][j+k] 就可以增加 dp[ i ][ j ]种跳跃方法了。

#include<iostream>
 #include<cstdio>
 #include<cstring>
 using namespace std;
 typedef long long ll;
 #define MAXN 44
 int map[MAXN][MAXN];
 char str[MAXN];
 ll dp[MAXN][MAXN];
 int n;
 
 ll dfs(int x,int y){
     if(x==n&&y==n){
         return (ll)1;
     }
     if(map[x][y]==0)return 0;
     if(dp[x][y])return dp[x][y];
     for(int flag=0;flag<=1;flag++){
         int xx,yy;
         if(flag){xx=x,yy=y+map[x][y];}
         else {xx=x+map[x][y],yy=y;}
         if(xx>=1&&xx<=n&&yy>=1&&yy<=n){
             dp[x][y]+=dfs(xx,yy);
         }
     }
     return dp[x][y];
 }
 
 
 int main(){
     while(~scanf("%d",&n)&&n!=-1){
         for(int i=1;i<=n;i++){
             scanf("%s",str+1);
             for(int j=1;j<=n;j++){
                 map[i][j]=str[j]-'0';
             }
         }
         memset(dp,0,sizeof(dp));
         ll ans=dfs(1,1);
         printf("%I64d\n",ans);
     }
     return 0;
 }

 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1619

思路:搜索结果取三个方向的最小值即可,最后打印行号比较麻烦,就从头开始找,如果有dp[x][y]==dp[i][y+1]+map[x][y],说明是下一个行号,而i(0-n-1)保证了字典序。

#include<iostream>
 #include<cstdio>
 #include<cstring>
 using namespace std;
 #define MAXN 222
 #define inf 1<<30
 int map[MAXN][MAXN];
 int dp[MAXN][MAXN];
 int n,m,len;
 int path[MAXN];
 
 
 int dfs(int x,int y){
     if(dp[x][y]!=inf)return dp[x][y];
     if(y==m-1)return dp[x][y]=map[x][y];
     int ans=min(min(dfs((x-1+n)%n,y+1),dfs(x,y+1)),dfs((x+1)%n,y+1));
     return dp[x][y]=ans+map[x][y];
 }
 
 
 void Solve(int x,int y){
     path[len++]=x;
     if(y==m-1)return ;
     int x1=(x-1+n)%n,x2=x,x3=(x+1)%n;
     for(int i=0;i<n;i++){
         if(dp[x][y]==dp[i][y+1]+map[x][y]&&(i==x1||i==x2||i==x3)){
             Solve(i,y+1);
             return ;
         }
     }
 }
 
 int main(){
     while(~scanf("%d%d",&n,&m)){
         memset(dp,0,sizeof(dp));
         for(int i=0;i<=n;i++)
             for(int j=0;j<=m;j++)
                 dp[i][j]=inf;
         for(int i=0;i<n;i++)
             for(int j=0;j<m;j++)
                 scanf("%d",&map[i][j]);    
         int ans=inf,pos;
         for(int i=0;i<n;i++){
             if(dfs(i,0)<ans){ans=dp[i][0],pos=i;}
         }
         len=0;
         Solve(pos,0);//从第一列的第pos行往后找路径(dp[pos][0]即为最小值)
         printf("%d",path[0]+1);
         for(int i=1;i<len;i++){
             printf(" %d",path[i]+1);
         }
         printf("\n%d\n",ans);
     }
     return 0;
 }

 

解题报告转自:http://www.cnblogs.com/wally/archive/2013/05/11/3072393.html


  1. #include <cstdio>
    #include <algorithm>

    struct LWPair{
    int l,w;
    };

    int main() {
    //freopen("input.txt","r",stdin);
    const int MAXSIZE=5000, MAXVAL=10000;
    LWPair sticks[MAXSIZE];
    int store[MAXSIZE];
    int ncase, nstick, length,width, tmp, time, i,j;
    if(scanf("%d",&ncase)!=1) return -1;
    while(ncase– && scanf("%d",&nstick)==1) {
    for(i=0;i<nstick;++i) scanf("%d%d",&sticks .l,&sticks .w);
    std::sort(sticks,sticks+nstick,[](const LWPair &lhs, const LWPair &rhs) { return lhs.l>rhs.l || lhs.l==rhs.l && lhs.w>rhs.w; });
    for(time=-1,i=0;i<nstick;++i) {
    tmp=sticks .w;
    for(j=time;j>=0 && store >=tmp;–j) ; // search from right to left
    if(j==time) { store[++time]=tmp; }
    else { store[j+1]=tmp; }
    }
    printf("%dn",time+1);
    }
    return 0;
    }

  2. 博主您好,这是一个内容十分优秀的博客,而且界面也非常漂亮。但是为什么博客的响应速度这么慢,虽然博客的主机在国外,但是我开启VPN还是经常响应很久,再者打开某些页面经常会出现数据库连接出错的提示