Travelling Salesman Problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 898    Accepted Submission(s): 327

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(1≤n,m≤100,n∗m≥2).



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,y−1),
"R" means you walk to cell (x,y+1),
"U" means you walk to cell (x−1,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
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  

pid=5421" target="_blank">5421 

pid=5420" target="_blank">5420 5419 5418 

pid=5417" target="_blank">5417 

 

当n,m有一个奇数时,‘S形’可全取。

否则至少要少取一个,

假设少取(mx,my) ,当mx+my为偶数时,必定有一个与(mx,my)相邻的不能取

否则必能全取剩下的。(‘S形’+特判2行‘长城形’)

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int n,m;
ll a[MAXN][MAXN];
int main()
{
// freopen("Travelling.in","r",stdin); while(cin>>n>>m) {
ll sum=0,mi=INF;int mx,my;
For(i,n) For(j,m) {
scanf("%lld",&a[i][j]),sum+=a[i][j];
if (mi>a[i][j]&&(i+j)%2==1)
mi=min(mi,a[i][j]),mx=i,my=j;
}
if (n%2==0&&m%2==0)
{
cout<<sum-mi<<endl; if (mx%2==1) {
For(i,mx-1)
{
if (i&1) {For(j,m-1) putchar('R');}
else {For(j,m-1) putchar('L'); }
if (i<n) putchar('D');
} int tx=mx,ty=1;
int p=0;
For(j,m)
{
if (my==j) {if (j<m) putchar('R');continue;}
if (p==0) printf("D");
else printf("U");
p^=1;
if (j<m) putchar('R');
}
Fork(i,mx+2,n)
{
putchar('D');
if ((i&1)^1) {For(j,m-1) putchar('R');}
else {For(j,m-1) putchar('L'); }
}
} if (my%2==1) {
For(i,my-1)
{
if (i&1) {For(j,n-1) putchar('D'); }
else {For(j,n-1) putchar('U'); }
if (i<m) putchar('R');
} int tx=1,ty=my;
int p=0;
For(j,n)
{
if (mx==j) {if (j<n) putchar('D');continue;}
if (p==0) printf("R");
else printf("L");
p^=1;
if (j<n) putchar('D');
} Fork(i,my+2,m)
{
putchar('R');
if ((i&1)^1) {For(j,n-1) putchar('D'); }
else {For(j,n-1) putchar('U'); }
}
} }
else {
cout<<sum<<endl;
if (n%2) {
For(i,n)
{
if (i&1) {For(j,m-1) putchar('R');}
else {For(j,m-1) putchar('L'); }
if (i<n) putchar('D');
}
} else {
For(i,m)
{
if (i&1) {For(j,n-1) putchar('D'); }
else {For(j,n-1) putchar('U'); }
if (i<m) putchar('R');
}
}
}
cout<<endl;
} return 0;
}

HDU 5402(Travelling Salesman Problem-构造矩阵对角最长不相交路径)的更多相关文章

  1. HDU 5402 Travelling Salesman Problem (构造)(好题)

    大致题意:n*m的非负数矩阵,从(1,1) 仅仅能向四面走,一直走到(n,m)为终点.路径的权就是数的和.输出一条权值最大的路径方案 思路:因为这是非负数,要是有负数就是神题了,要是n,m中有一个是奇 ...

  2. 构造 - HDU 5402 Travelling Salesman Problem

    Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...

  3. HDU 5402 Travelling Salesman Problem (模拟 有规律)(左上角到右下角路径权值最大,输出路径)

    Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (J ...

  4. HDU 5402 Travelling Salesman Problem(棋盘染色 构造 多校啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5402 Problem Description Teacher Mai is in a maze wit ...

  5. HDU 5402 Travelling Salesman Problem(多校9 模拟)

    题目链接:pid=5402">http://acm.hdu.edu.cn/showproblem.php?pid=5402 题意:给出一个n×m的矩阵,位置(i.j)有一个非负权值. ...

  6. hdu 5402 Travelling Salesman Problem(大模拟)

    Problem Description Teacher Mai ,) to the bottom right corner (n,m). He can choose one direction and ...

  7. hdu 5402 Travelling Salesman Problem (技巧,未写完)

    题意:给一个n*m的矩阵,每个格子中有一个数字,每个格子仅可以走一次,问从(1,1)走到(n,m) 的路径点权之和. 思路: 想了挺久,就是有个问题不能短时间证明,所以不敢下手. 显然只要n和m其中一 ...

  8. HDU 5402 : Travelling Salesman Problem

    题目大意:n*m的格子,从左上角走到右下角,每个格子只能走一遍,每个格子上有一个非负数,要让途径的数字和最大,最后要输出路径 思路:显然茹果n,m有一个是奇数的话所有格子的数字都能被我吃到,如果都是偶 ...

  9. HDOJ 5402 Travelling Salesman Problem 模拟

    行数或列数为奇数就能够所有走完. 行数和列数都是偶数,能够选择空出一个(x+y)为奇数的点. 假设要空出一个(x+y)为偶数的点,则必须空出其它(x+y)为奇数的点 Travelling Salesm ...

随机推荐

  1. Gh0st整理资料1

    题首 Gh0st是一款开源的远程控制软件.界面友好,性能高效.网上流传很多版本,比如红狼,饭客,败笔,大灰狼版本以及多如牛毛的个人修改的如外星人,Drat等个人修改版本.但内核都是基于Gh0st3.6 ...

  2. c语言中pthread的理解和使用

    在头文件中看到#typedef unsigned long int pthread_t这句话怎么理解,pthread_t是一个什么类型呢? 相当于pthread_t实际是个unsigned long  ...

  3. C#.NET常见问题(FAQ)-如何在系统变量中加入新的环境变量

    比如我要将C:\Windows\Microsoft.NET\Framework\v3.5这个目录加入环境变量 则在系统的环境变量中点击Path,编辑,然后加入一个分号";",然后粘 ...

  4. Asp.net 生成静态页面

    http://www.cnblogs.com/tonycall/archive/2009/07/18/1526079.html Asp.net 生成静态页面(简单用法) 第一次发表,有什么错误,请大家 ...

  5. JavaScript文件引入方式区别

    1.JavaScript文件引入方式 (1)正常引入 <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js&quo ...

  6. 【Oracle】性能优化

    优化原则 1.在select语句中避免使用* 2.使用Truncate清空表 2.1语法 Truncate [table|cluster] shema.[table_name][cluster_nam ...

  7. ASP.NET 性能监控和优化入门

    关键要点: 只有与应用指标相关联,基础设施指标才能最大发挥作用. 高效性能优化的关键在于性能数据. 一些APM工具为ASP.NET提供了开箱即用的支持,这样入门使用ASP.NET仅需最小限度的初始设置 ...

  8. java中相同名字不同返回类型的方法

    这种名字相同返回类型不同的方法,在同一个类中是无法共存的,不论是继承过来的方法,还是多实现过来的方法,在一个类内都无法共存.名字确定了,你能改的只有参数(重载).

  9. spyder python 相关

    1.python开发集成工具Spyder中,如何设置变量成员提示和代码补全呢? 答: pip install rope,安装好rope 就可以了 2.最常用的是:tap的制动补全 (IPython c ...

  10. ocat

    <!DOCTYPE html> <html lang="zh-CN" > <head><meta http-equiv="Con ...