大致题意:n*m的非负数矩阵,从(1,1) 仅仅能向四面走,一直走到(n,m)为终点。路径的权就是数的和。输出一条权值最大的路径方案

思路:因为这是非负数,要是有负数就是神题了,要是n,m中有一个是奇数。显然能够遍历。要是有一个偶数。能够绘图发现,把图染成二分图后,(1,1)为黑色,总能有一种构造方式能够仅仅绕过不论什么一个白色的点。然后再遍历其它点。而绕过黑色的点必定还要绕过两个白色点才干遍历所有点,这是绘图发现的。所以找一个权值最小的白色点绕过就能够了,

题解给出了证明:

如果n,mn,m都为偶数,那么讲棋盘黑白染色。如果(1,1)(1,1)和(n,m)(n,m)都为黑色,那么这条路径中黑格个数比白格个数多11,而棋盘中黑白格子个数同样。所以必定有一个白格不会被经过。所以选择白格中权值最小的不经过。


//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define reveach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
#define rep(i,n) for ( int i=0; i< int(n); i++ )
using namespace std;
typedef long long ll;
#define X first
#define Y second
typedef pair<int,int> pii; template <class T>
inline bool RD(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void PT(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) PT(x / 10);
putchar(x % 10 + '0');
} const int N = 123;
int mp[N][N];
int main(){ int n,m;
while(~scanf("%d%d",&n,&m)){
int sum = 0;
memset(mp,0,sizeof(mp));
REP(i,n) REP(j,m) RD(mp[i][j]), sum += mp[i][j];
if( (n&1)||(m&1) ){
PT(sum);puts("");
if( n&1 ){
REP(r,n){
if( r&1 ) REP(i,m-1) putchar('R');
else REP(i,m-1) putchar('L');
if( r != n) putchar('D');
}
}else{
REP(c,m){
if( c&1 ) REP(i,n-1) putchar('D');
else REP(i,n-1) putchar('U');
if( c != m) putchar('R');
}
}
}else{
int minn = 1LL<<30;
int sx,sy;
REP(x,n) REP(y,m){
if( (x+y)&1 ){
if( mp[x][y] < minn) minn = mp[x][y], sx = x,sy = y;
}
}
printf("%d\n",sum-minn);
bool ok = 0;
REP(y,m){
if( (y-1)/2+1 == (sy-1)/2+1){
ok = 1;
bool rgt = 1;
REP(x,n){
if( x == sx) {
if( x != n) putchar('D');
continue;
}
if( rgt) putchar('R');
else putchar('L');
if( x != n) putchar('D');
rgt = !rgt;
}
y++;
}else{
if( ((y&1)&&ok==0) || ((y%2 == 0)&&ok) ){
REP(x,n-1) putchar('D');
}else{
REP(x,n-1) putchar('U');
}
}
if( y != m) putchar('R');
}
}
puts("");
}
}

Travelling Salesman Problem

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

Total Submission(s): 864    Accepted Submission(s): 313

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

HDU 5402 Travelling Salesman Problem (构造)(好题)的更多相关文章

  1. 构造 - HDU 5402 Travelling Salesman Problem

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

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

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

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

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

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

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

  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

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

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

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

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

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

  9. HDOJ 5402 Travelling Salesman Problem 模拟

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

随机推荐

  1. leetcode_894. All Possible Full Binary Trees

    https://leetcode.com/problems/all-possible-full-binary-trees/ 给定节点个数,求所有可能二叉树,该二叉树所有节点要么有0个子节点要么有两个子 ...

  2. C# GDI+ 画坐标(x,y)

    private void button1_Click(object sender, EventArgs e) { Graphics g = this.CreateGraphics(); g.Clear ...

  3. filezilla server FTP 安装报错 "could not load TLS network. Aborting start of administration interface"

    filezilla server FTP 安装报错   "could not load TLS network. Aborting start of administration inter ...

  4. [css或js控制图片自适应]

    [css或js控制图片自适应]图片自动适应大小是一个非常常用的功能,在进行制作的时候为了防止图片撑开容器而对图片的尺寸进行必要的控制,我们可不可以用CSS控制图片使它自适应大小呢?此个人博客想到了一个 ...

  5. loader.js

    /** * @preserve Tiny-Loader: A small loader that load CSS/JS in best way for page performanceIs. * * ...

  6. ionic提供的配色方案

    .light #ffffff .stable #f8f8f8 .positive #387ef5 .calm #11c1f3 .balanced #33cd5f .energized #ffc900 ...

  7. WC2007 石头剪刀布 数学+最小费用最大流

    题面: 有N个人参加一场比赛,赛程规定任意两个人之间都要进行一场比赛:这样总共有N*(N-1)/2场比赛.比赛已经进行了一部分,我们想知道在极端情况下,比赛结束后最多会发生多少剪刀石头布情况.即给出已 ...

  8. 大项目之网上书城(八)——数据库大改&添加图书

    目录 大项目之网上书城(八)--数据库大改&添加图书 主要改动 1.数据库新增表 代码 2.数据库新增触发器 3.其他对BookService和BookDao的修改 代码 4.addBook. ...

  9. 笔试算法题(20):寻找丑数 & 打印1到N位的所有的数

    出题:将只包含2,3,5的因子的数称为丑数(Ugly Number),要求找到前面1500个丑数: 分析: 解法1:依次判断从1开始的每一个整数,2,3,5是因子则整数必须可以被他们其中的一个整除,如 ...

  10. 数据库的ACID 简谈

    一.事务 定义:所谓事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位. 准备工作:为了说明事务的ACID原理,我们使用银行账户及资金管理的案例进行分析. 二.ACI ...