当前位置:网站首页>Niuke monthly race 22- collect pieces of paper
Niuke monthly race 22- collect pieces of paper
2022-07-01 09:13:00 【Lovely and beautiful girl】
The question :
It's just one. n*m The matrix of ,n and m all <=20, And then there's the most 10 There's a piece of paper somewhere , Now let's take you from your initial position , The shortest distance to return to the original position after picking up all the pieces of paper .
reflection :
At that time, I saw n Very small , Just want to press . I've seen a lot of things recently . Then I don't think it's easy to write , In fact dfs Enumeration method or next_permutation Just go . I chose dfs Because I thought this would happen , From the initial point to a The paper returns to its original point , Until then b Paper, etc , It is the case that the middle will return to the initial point first , But this topic will not be like this , Because it must be a straight line , The straight line between two points is the shortest , If it's a picture, it's not necessarily . The main thing is to look at the pressure dp How is it written .
Code :
dfs( So when the title is a picture ):
int T,n,m,k;
int minn;
PII va[N];
int vb[N];
int vis[M];
void dfs(int now,int sum,int cnt)
{
if(cnt==k+1)
{
sum += abs(va[0].fi-va[now].fi)+abs(va[0].se-va[now].se);
minn = min(minn,sum);
return ;
}
for(int i=0;i<=k;i++)
{
if(now==i) continue;
if(vis[i]==0)
{
if(i!=0) vis[i] = 1;
dfs(i,sum+abs(va[i].fi-va[now].fi)+abs(va[i].se-va[now].se),cnt+(i!=0));
vis[i] = 0;
}
}
}
signed main()
{
IOS;
cin>>T;
while(T--)
{
int a,b;
cin>>n>>m>>a>>b>>k;
for(int i=1;i<=k;i++) cin>>va[i].fi>>va[i].se;
va[0] = {
a,b};
for(int i=0;i<=k;i++) vis[i] = 0;
minn = inf;
dfs(0,0,1);
cout<<"The shortest path has length "<<minn<<"\n";
}
return 0;
}
Pressure dp:
int T,n,m,k;
PII va[N];
int vb[M][M];
int dp[(1ll<<10)][15];
signed main()
{
IOS;
cin>>T;
while(T--)
{
cin>>n>>m>>va[0].fi>>va[0].se>>k;
for(int i=1;i<=k;i++) cin>>va[i].fi>>va[i].se;
for(int i=0;i<=k;i++)
{
for(int j=0;j<=k;j++)
vb[i][j] = abs(va[i].fi-va[j].fi)+abs(va[i].se-va[j].se);
}
for(int i=0;i<(1ll<<n);i++)
{
for(int j=0;j<=n;j++)
dp[i][j] = inf; // All initialization inf
}
for(int i=1;i<=n;i++) dp[1ll<<(i-1)][i] = vb[0][i]; // When the last point is i, And the state of all points 1ll<<(i-1) The weight of the time . such as i==3, that dp[100][1] This state is like this .
for(int i=0;i<(1ll<<n);i++)
{
for(int j=1;j<=n;j++)
{
if((i>>(j-1)&1)) continue;
for(int k=1;k<=n;k++)
{
if((i>>(k-1)&1)==0) continue; //j yes 0, from k Transfer of ,k by 1.
dp[i+(1ll<<(j-1))][j] = min(dp[i+(1ll<<(j-1))][j],dp[i][j]+vb[k][j]);
}
}
}
int minn = inf;
for(int i=1;i<=k;i++) minn = min(minn,dp[i][(1ll<<n)-1]+vb[i][0]); // When all are achieved , And then finally i A weight .
cout<<"The shortest path has length "<<minn<<"\n";
}
return 0;
}
General section :
Attention to detail , Think more , A lot of summary .
边栏推荐
- How to solve the problem of fixed assets management and inventory?
- Promise异步编程
- 日常办公耗材管理解决方案
- Summary of reptile knowledge points
- 【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
- tensorrt yolov5_ trt. Py comments
- [ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DHT11 +nodejs local service + MySQL database
- Leetcode daily question brushing record --540 A single element in an ordered array
- 2.4 激活函数
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DHT11 +NodeJs本地服务+ MySQL数据库
猜你喜欢

【检测技术课案】简易数显电子秤的设计与制作

集团公司固定资产管理的痛点和解决方案

Installing Oracle EE

3D打印Arduino 四轴飞行器

2.2 【pytorch】torchvision. transforms

Which method is good for the management of fixed assets of small and medium-sized enterprises?
![[interview brush 101] linked list](/img/52/d159bc66c0dbc44c1282a96cf6b2fd.png)
[interview brush 101] linked list

Why is the Ltd independent station a Web3.0 website!

TV size and viewing distance

Preparing for the Blue Bridge Cup -- bit operation
随机推荐
NoSQL数据库的安装和使用
Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
Pain points and solutions of equipment management in large factories
Reproduced Xray - cve-2017-7921 (unauthorized access by Hikvision)
Differences among tasks, threads and processes
Shell script - special variables: shell $, $*, [email protected], $$$
What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?
jeecg 重启报40001
Set the type of the input tag to number, and remove the up and down arrows
How to manage fixed assets well? Easy to point and move to provide intelligent solutions
FAQ | FAQ for building applications for large screen devices
Shell script -read command: read data entered from the keyboard
Ape anthropology topic 20 (the topic will be updated from time to time)
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
Shell script - definition, assignment and deletion of variables
【pytorch】softmax函数
Principles of Microcomputer - internal and external structure of microprocessor
足球篮球体育比赛比分直播平台源码/app开发建设项目
【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder