BestCoder Round #14 B 称号 Harry And Dig Machine 【TSP】
哈哈 最终涨边粉色了,不easy呀。顺便写一道题解吧
题意:给一个m*n的矩阵,然后当中最多由10个有值,求总左上角把全部的值都拿上回到左上角的最小步数。
标准的TSP回到原点问题,须要先预处理出图来。然后TSP就可以。
AC代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 15;
int mp[N][N];
struct Node
{
int x,y;
};
vector<Node> vv;
int n,m;
int dp[1<<N][N];
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++){
int x;
scanf("%d",&x);
if(x)
vv.push_back((Node){i,j});
}
}
int okk = 0;
for(int i=0;i<vv.size();i++)
{
if(vv[i].x==0 && vv[i].y==0)
{
okk=1;
continue;
}
}
if(okk==0)
vv.push_back((Node){0,0});
for(int i=0;i<vv.size();i++)
{
for(int j=0;j<vv.size();j++)
{
mp[i][j] = 0;
if(i==j)
continue;
mp[i][j] = abs(vv[i].x-vv[j].x) + abs(vv[i].y-vv[j].y);
}
}
int len = vv.size();
n = len;
for(int st=0;st<(1<<n);st++) //TSP
{
for(int i=0;i<n;i++)
{
if((st&(1<<i))==0) //Ϊ0
continue;
if(st==(1<<i)){
dp[st][i]=mp[0][i];continue;
}
dp[st][i]=inf;
for(int j=0;j<n;j++)
{
if((st&(1<<j)) && i!=j)//Ϊ1
{
dp[st][i]=min(dp[st&~(1<<i)][j]+mp[j][i],dp[st][i]);
}
}
}
}
int ans=inf;
for(int i=0;i<n;i++){
ans=min(ans,dp[(1<<n)-1][i]+mp[i][0]);
}
printf("%d\n",ans);
vv.clear();
}
return 0;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
BestCoder Round #14 B 称号 Harry And Dig Machine 【TSP】的更多相关文章
- BestCoder Round #14
Harry And Physical Teacher Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- hdu 5066 Harry And Physical Teacher(Bestcoder Round #14)
Harry And Physical Teacher Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- hdu5067Harry And Dig Machine(TSP旅行商问题)
题目链接: huangjing 题意:给出一幅图.图中有一些点,然后从第1个点出发,然后途径全部有石头的点.最后回到原点,然后求最小距离.当初作比赛的时候不知道这就是旅行商经典问题.回来学了一下. 思 ...
- TSP+Floyd BestCoder Round #52 (div.2) 1002 Victor and Machine
题目传送门 题意:有中文版的 分析:(出题人的解题报告)我们首先需要预处理出任意两个国家之间的最短距离,因为数据范围很小,所以直接用Floyd就行了.之后,我们用f[S][i]表示访问国家的情况为S, ...
- 构造 BestCoder Round #52 (div.2) 1001 Victor and Machine
题目传送门 题意:有中文版的 分析:首先要知道机器关闭后,w是清零的.所以一次(x + y)的循环弹出的小球个数是固定的,为x / w + 1,那么在边界时讨论一下就行了 收获:这种题目不难,理解清楚 ...
- BestCoder Round #11 (Div. 2) 题解
HDOJ5054 Alice and Bob Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- 简单几何(水)BestCoder Round #50 (div.2) 1002 Run
题目传送门 /* 好吧,我不是地球人,这题只要判断正方形就行了,正三角形和正五边形和正六边形都不可能(点是整数). 但是,如果不是整数,那么该怎么做呢?是否就此开启计算几何专题了呢 */ /***** ...
- HDU 5228 ZCC loves straight flush( BestCoder Round #41)
题目链接:pid=5228">ZCC loves straight flush pid=5228">题面: pid=5228"> ZCC loves s ...
- BestCoder Round #89 02单调队列优化dp
1.BestCoder Round #89 2.总结:4个题,只能做A.B,全都靠hack上分.. 01 HDU 5944 水 1.题意:一个字符串,求有多少组字符y,r,x的下标能组成等比数列 ...
随机推荐
- js记录用户行为浏览记录和停留时间(转)
演示地址:http://weber.pub/demo/160902/test.html 测试源码下载:http://pan.baidu.com/s/1nvPKbSP 密码:r147 解决问题所使用的知 ...
- easyui-combobox绑定json数据
用的C#语言 后台取数据,就不用废话了,先看看序列化json数据 /// <summary> /// 对象转JSON /// </summary> /// <param ...
- jquery 处理密码输入框(input type="password" ) 模仿placeholder
html <form method="post" action=""> <ul> <li> <span>邮箱&l ...
- 在SQL SERVER中批量替换字符串
update [表名] set [字段名]=replace([字段名],'被替换原内容','替换后内容')
- jQuery_easyUI 合并单元格 (DataGrid 数据表格)
<table id="dg" style="height:350px;z-index:-5555; " class="easyui-datagr ...
- WCF入门教程系列二
一.概述 WCF能够建立一个跨平台的安全.可信赖.事务性的解决方案,是一个WebService,.Net Remoting,Enterprise Service,WSE,MSMQ的并集,有一副很经典的 ...
- java中的native关键字
参照下面的链接http://blog.163.com/yueyemaitian@126/blog/static/21475796200701491621267/
- HTML5学习笔记一:新增主体结构元素
Dreamweaver快捷键: 属性面板:Ctrl+F3 新建文档:Ctrl+N 选择用网页查看:F12 新增的主体结构元素: section元素(例子如下): <!DOCTYPE HTML&g ...
- Spyder调试错误-"TypeError: decoding Unicode is not supported"
这是Spyder 2.7.4版本的一个Bug,升级到最新版本(2.7.9)即可. pip install --upgrade spyder Reference: https://github.com/ ...
- re模块
Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,和 Perl 脚本的正则表达式功能类似,使用这一内嵌于 Python 的语言工具,尽管不能 ...