HDU 5402(Travelling Salesman Problem-构造矩阵对角最长不相交路径)

Posted claireyuancy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 5402(Travelling Salesman Problem-构造矩阵对角最长不相交路径)相关的知识,希望对你有一定的参考价值。

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(1n,m100,n?m2)技术分享.

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 10技术分享4技术分享技术分享.
 

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:  php?

pid=5421" target="_blank">5421 5420 5419 5418 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-构造矩阵对角最长不相交路径)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5402(Travelling Salesman Problem-构造矩阵对角最长不相交路径)

hdu 5402 Travelling Salesman Problem

HDU 5402Travelling Salesman Problem(构造)

HDOJ 5402 Travelling Salesman Problem 模拟

HDU3001 Travelling —— 状压DP(三进制)

hdu3001Travelling