zoukankan      html  css  js  c++  java
  • 2015 Multi-University Training Contest 9 1007

    Travelling Salesman Problem

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 477    Accepted Submission(s): 159
    Special Judge


    Problem Description
    Teacher Mai is in a maze with n rows and m columns. There is a non-negative number in each cell. Teacher Mai wants to walk from the top left corner (1,1) to the bottom right corner (n,m). He can choose one direction and walk to this adjacent cell. However, he can't go out of the maze, and he can't visit a cell more than once.

    Teacher Mai wants to maximize the sum of numbers in his path. And you need to print this path.
     
    Input
    There are multiple test cases.

    For each test case, the first line contains two numbers n,m(1n,m100,nm2).

    In following n lines, each line contains m numbers. The j-th number in the i-th line means the number in the cell (i,j). Every number in the cell is not more than 104.
     
    Output
    For each test case, in the first line, you should print the maximum sum.

    In the next line you should print a string consisting of "L","R","U" and "D", which represents the path you find. If you are in the cell (x,y), "L" means you walk to cell (x,y1), "R" means you walk to cell (x,y+1), "U" means you walk to cell (x1,y), "D" means you walk to cell (x+1,y).
     
    Sample Input
    3
    3 2 3
    3 3 3
    3 3 3 2
     
    Sample Output
    25
    RRDLLDRR
     
    Author
    xudyh
     
    Source
     
    题意:一个矩阵NxM,起点为(1,1),终点为(N,M),每经过一个点,就加上那个点的值,求到终点时总和最大值是多少,并打印路径,矩阵中不会出现负数。
    分析:矩阵中不会出现负数,搜索会超时,所以模拟。
    那么最好的情况肯定是全部都得到,这种情况分析一下发现只有N或者M至少有为奇数是才能实现。
    剩下的情况就是N和M全为偶数的情况,官方题解:

    如果N,M都为偶数,那么讲棋盘黑白染色,假设(1,1)(n,m)都为黑色,那么这条路径中黑格个数比白格个数多1

    而棋盘中黑白格子个数相同,所以必然有一个白格不会被经过,所以选择白格中权值最小的不经过。

    构造方法是这样,首先RRRRDLLLLD这样的路径走到这个格子所在行或者上一行,然后DRUR这样走到这个格子的所在列或者前一列,然后绕过这个格子。

    然后走完这两行,接着按LLLLDRRRR这样的路径往下走。

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<string>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<vector>
    #include<map>
    #include<stdlib.h>
    #include<algorithm>
    #define LL __int64
    #define FIN freopen("in.txt","r",stdin)
    using namespace std;
    const int MAXN=100+5;
    const int INF=0x3f3f3f3f;
    int a[MAXN][MAXN];
    int n,m,ans,minn;
    int x,y;
    
    int main()
    {
        //FIN;
        while(scanf("%d %d",&n,&m)!=EOF)
        {
            memset(a,0,sizeof(a));
            ans=0,minn=INF;
    
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    scanf("%d",&a[i][j]);
                    ans+=a[i][j];
                    if(((i+j)&1) && a[i][j]<minn)
                    {
                        minn=a[i][j];
                        x=i,y=j;
                    }
                }
            }
            if(n&1)
            {
                printf("%d
    ",ans);
                int inv=1;
                for(int i=1;i<=n;i++)
                {
                    if(inv)
                    {
                        for(int j=1;j<m;j++) printf("R");
                        inv=!inv;
                    }
                    else
                    {
                        for(int j=1;j<m;j++) printf("L");
                        inv=!inv;
                    }
                    if(i!=n) printf("D");
                }
                printf("
    ");
            }
            else if(m&1)
            {
                printf("%d
    ",ans);
                int inv=1;
                for(int i=1;i<=m;i++)
                {
                    if(inv)
                    {
                        for(int j=1;j<n;j++) printf("D");
                        inv=!inv;
                    }
                    else
                    {
                        for(int j=1;j<n;j++) printf("U");
                        inv=!inv;
                    }
                    if(i!=m) printf("R");
                }
                printf("
    ");
            }
            else
            {
                printf("%d
    ",ans-minn);
                for(int i=1;i<=n;i+=2)
                {
                    if(i==x || i+1==x)
                    {
                        int inv=1;
                        for(int j=1;j<y;j++)
                        {
                            if(inv)
                            {
                                printf("D");
                                inv=!inv;
                            }
                            else
                            {
                                printf("U");
                                inv=!inv;
                            }
                            printf("R");
                        }
                        if(y<m) printf("R");
                        for(int j=y+1;j<=m;j++)
                        {
                            if(inv)
                            {
                                printf("D");
                                inv=!inv;
                            }
                            else
                            {
                                printf("U");
                                inv=!inv;
                            }
                            if(j<m) printf("R");
                        }
                        if(i<n-1) printf("D");
                    }
                    else if(x<i)
                    {
                        int inv=1;
                        for(int j=1;j<m;j++) printf("L");
                        printf("D");
                        for(int j=1;j<m;j++) printf("R");
                        if(i<n-1) printf("D");
                    }
                    else
                    {
                        for(int j=1;j<m;j++) printf("R");
                        printf("D");
                        for(int j=1;j<m;j++) printf("L");
                        printf("D");
                    }
                }
                printf("
    ");
            }
        }
        return 0;
    }
    View Code
     
  • 相关阅读:
    BZOJ_1221_ [HNOI2001]_软件开发(最小费用流,网络流24题#10)
    POJ_1269_Intersecting_Lines_(计算几何基础)
    BZOJ_2049_[Sdoi_2008]_Cave_洞穴勘测_(LCT/并查集)
    BZOJ_2002_弹飞绵羊_(LCT)
    BZOJ_3282_Tree_(LCT)
    CodeForces_#354_Div.2_2016.5.25(A+B+C)
    BZOJ_1609_[Usaco2008_Feb]_Eating_Together_麻烦的聚餐_(动态规划,LIS)
    BZOJ_1607_ [Usaco2008_Dec]_Patting_Heads_轻拍牛头_(筛数)
    BZOJ_1606_ [Usaco2008_Dec]_Hay_For_Sale _购买干草_(背包)
    2010多校第一题 hdu3440House Man 差分约束系统
  • 原文地址:https://www.cnblogs.com/clliff/p/4740949.html
Copyright © 2011-2022 走看看