HDOJ 题目4568 Hunter(BFS+状压DP,TSP)

本文介绍了一种解决旅行商问题(TSP)的方法,通过构建辅助图并运用动态规划技巧来寻找收集所有宝藏所需的最低成本路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hunter

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1589    Accepted Submission(s): 469


Problem Description
  One day, a hunter named James went to a mysterious area to find the treasures. James wanted to research the area and brought all treasures that he could.
  The area can be represented as a N*M rectangle. Any points of the rectangle is a number means the cost of research it,-1 means James can't cross it, James can start at any place out of the rectangle, and explore point next by next. He will move in the rectangle and bring out all treasures he can take. Of course, he will end at any border to go out of rectangle(James will research every point at anytime he cross because he can't remember whether the point are researched or not).
  Now give you a map of the area, you must calculate the least cost that James bring out all treasures he can take(one point up to only one treasure).Also, if nothing James can get, please output 0.
 

Input
  The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case begins with a line containing 2 integers N M , (1<=N,M<=200), that represents the rectangle. Each of the following N lines contains M numbers(0~9),represent the cost of each point. Next is K(1<=K<=13),and next K lines, each line contains 2 integers x y means the position of the treasures, x means row and start from 0, y means column start from 0 too.
 

Output
  For each test case, you should output only a number means the minimum cost.
 

Sample Input
  
2 3 3 3 2 3 5 4 3 1 4 2 1 1 1 3 3 3 2 3 5 4 3 1 4 2 2 1 1 2 2
 

Sample Output
  
8 11
 

Source
 

Recommend
zhoujiaqi2010   |   We have carefully selected several similar problems for you:   5498  5497  5495  5494  5493 
 TSP问题,
先BFS建下图,然后。。。人家的解释
一个整数i就表示了一个点集;
整数i可以表示一个点集,也可以表示是第i个点。
状态表示:dp[i][j]表示经过点集i中的点恰好一次,不经过其它的点,并且以j点为终点的路径,权值和的最小值,如果这个状态不存在,就是无穷大。

状态转移:
     单点集:状态存在dp[1<<j][j] = 0;否则无穷大。
     非单点集:
     状态存在   dp[i][j] = min(dp[k][s] + w[s][j])
     k表示i集合中去掉了j点的集合,s遍历集合k中的点并且dp[k][s]状态存在, 点s到点j有边存在,w[s][j]表示边的权值。
     状态不存在 dp[i][j]为无穷大。
ac代码
150301102015-10-06 23:03:52Accepted4568140MS3068K2056 BG++Who_you?

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define min(a,b) (a>b?b:a)
using namespace std;
int dp[1<<14][14];
int u[14],v[14];
int map[220][220],vis[220][220],id[220][220],dis[220][220];
int n,m,k;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
struct s
{
	int x,y,step;
	friend bool operator < (s a, s b)
	{
		return a.step>b.step;
	}
}a,temp;
int jud(struct s a)
{
	if(a.x<1||a.x>n||a.y<1||a.y>m)
		return 0;
	return 1;
}
void bfs(int x,int y,int xx)
{
	memset(vis,0,sizeof(vis));
	priority_queue<struct s>q;
	int flag=0;
	a.x=x;
	a.y=y;
	a.step=0;
	q.push(a);
	vis[a.x][a.y]=1;
	while(!q.empty())
	{
		a=q.top();
		q.pop();
		if(id[a.x][a.y]!=-1)
		{
			dis[xx][id[a.x][a.y]]=a.step;
		}
		for(int i=0;i<4;i++)
		{
			temp.x=a.x+dx[i];
			temp.y=a.y+dy[i];
			if(!jud(temp))
			{
				if(!flag)
				{
					flag=1;
					dp[1<<xx][xx]=a.step+map[u[xx]][v[xx]];
				}
				continue;
			}
			if(vis[temp.x][temp.y]||map[temp.x][temp.y]==-1)
				continue;
			temp.step=a.step+map[temp.x][temp.y];
			vis[temp.x][temp.y]=1;
			q.push(temp);
		}
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		//int n,m;
		scanf("%d%d",&n,&m);
		int i,j;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
				scanf("%d",&map[i][j]);
		}
		scanf("%d",&k);
		memset(id,-1,sizeof(id));
		memset(dp,INF,sizeof(dp));
		for(i=0;i<k;i++)
		{
			scanf("%d%d",&u[i],&v[i]);
			u[i]++;
			v[i]++;
			id[u[i]][v[i]]=i;
		}
		for(i=0;i<k;i++)
		{
			bfs(u[i],v[i],i);
		}
		int rt=(1<<k)-1,p;
		for(i=0;i<=rt;i++)
		{
			for(j=0;j<k;j++)
			{
				if(dp[i][j]==INF)
					continue;
				for(p=0;p<k;p++)
				{
					if(p==j||(i&(1<<p)))
						continue;
					int st=i|(1<<p);
					dp[st][p]=min(dp[st][p],dp[i][j]+dis[j][p]);
				}
			}
		}
		int ans=INF;
		for(i=0;i<k;i++)
		{
			ans=min(ans,dp[rt][i]+dp[1<<i][i]-map[u[i]][v[i]]);
		}
		if(ans==INF)
		{
			ans=0;
		}
		printf("%d\n",ans);
	}
}


资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值