您的位置:首页 > 其它

hdu 4568 Hunter (旅行商问题)

2015-10-03 17:02 393 查看
题目链接

[align=left]Problem Description[/align]
  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.



[align=left]Input[/align]
  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.

[align=left]Output[/align]
  For each test case, you should output only a number means the minimum cost.

[align=left]Sample Input[/align]

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


[align=left]Sample Output[/align]

8
11


[align=left]Source[/align]
2013
ACM-ICPC长沙赛区全国邀请赛——题目重现
题意:给你一张n*m的图,经过每个点都会花费一定的值,当值为-1时代表不能经过,再给你k个宝藏的坐标位置(从0开始的坐标),你可以从这张图边缘任一点作为起始地进入地图,经过所有的宝藏坐标后从边缘任一点出来,问你最小的值,如果所有的宝藏坐标不能走完输出0。
分析:做的时候,我看了下k的大小是13,一想就是旅行商问题,把这13个点走完花费的最小值,但是点与点之间的最小花费需要预处理,由于自己太懒,没有去研究更好的预处理方式,所以预处理太过繁琐,看着就烦,还好A了,预处理时建一个超级起点,然后预处理超级起点到各个宝藏位置的最小值(进去),在预处理所有宝藏到超级起点的最小值(出来),再预处理所有宝藏之间的最小值,最后状压跑就行了。
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
#define nn 220
#define inff 0x3f3f3f
using namespace std;
int a[nn][nn],n,m,K;
int id[nn][nn],ma[15][15];
int p[nn*nn],num;
struct node
{
int en,next,len;
}E[nn*nn*5];
void init()
{
num=0;
memset(p,-1,sizeof(p));
}
void add(int st,int en,int len)
{
E[num].en=en;
E[num].len=len;
E[num].next=p[st];
p[st]=num++;
}
int dis[nn*nn];
bool vis[nn*nn];
struct Node
{
int id,val;
friend bool operator<(Node x,Node y)
{
return x.val>y.val;
}
}sta,tem;
void dij(int st)
{
priority_queue<Node> que;
int i;
for(i=0;i<nn*nn;i++) dis[i]=inff,vis[i]=false;
dis[st]=0;
tem.id=st,tem.val=0;
que.push(tem);
while(que.size())
{
sta=que.top();
que.pop();
if(dis[sta.id]<sta.val) continue;
for(i=p[sta.id];i+1;i=E[i].next)
{
int w=E[i].en;
if(dis[w]>dis[sta.id]+E[i].len)
{
dis[w]=dis[sta.id]+E[i].len;
tem.id=w,tem.val=dis[w];
que.push(tem);
}
}
}
}
int x[15],y[15];
int dp[9000][15];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&a[i][j]);
int k=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
id[i][j]=++k;
init();
for(int i=1;i<=n;i++)
{
if(a[i][1]!=-1)
{
add(0,id[i][1],a[i][1]);
add(id[i][1],0,0);
}
if(a[i][m]!=-1)
{
add(0,id[i][m],a[i][m]);
add(id[i][m],0,0);
}
}
for(int j=2;j<m;j++)
{
if(a[1][j]!=-1)
{
add(0,id[1][j],a[1][j]);
add(id[1][j],0,0);
}
if(a
[j]!=-1)
{
add(0,id
[j],a
[j]);
add(id
[j],0,0);
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]==-1) continue;
if(j>1&&a[i][j-1]!=-1) add(id[i][j],id[i][j-1],a[i][j-1]);
if(j<m&&a[i][j+1]!=-1) add(id[i][j],id[i][j+1],a[i][j+1]);
if(i>1&&a[i-1][j]!=-1) add(id[i][j],id[i-1][j],a[i-1][j]);
if(i<n&&a[i+1][j]!=-1) add(id[i][j],id[i+1][j],a[i+1][j]);
}
}
dij(0);
scanf("%d",&K);
for(int i=0;i<=K;i++)
for(int j=0;j<=K;j++)
ma[i][j]=inff;
for(int i=1;i<=K;i++)
{
scanf("%d%d",&x[i],&y[i]);
x[i]++,y[i]++;
}
for(int i=1;i<=K;i++)
ma[0][i]=dis[id[x[i]][y[i]]];
for(int i=1;i<=K;i++)
{
dij(id[x[i]][y[i]]);
ma[i][0]=dis[0];
for(int j=1;j<=K;j++)
{
ma[i][j]=dis[id[x[j]][y[j]]];
}
}
init();///重新建图是因为超级起点会影响点与点直接的距离(只是我这个本方法有会影响)
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]==-1) continue;
if(j>1&&a[i][j-1]!=-1) add(id[i][j],id[i][j-1],a[i][j-1]);
if(j<m&&a[i][j+1]!=-1) add(id[i][j],id[i][j+1],a[i][j+1]);
if(i>1&&a[i-1][j]!=-1) add(id[i][j],id[i-1][j],a[i-1][j]);
if(i<n&&a[i+1][j]!=-1) add(id[i][j],id[i+1][j],a[i+1][j]);
}
}
for(int i=1;i<=K;i++)
{
dij(id[x[i]][y[i]]);
for(int j=1;j<=K;j++)
{
ma[i][j]=dis[id[x[j]][y[j]]];
}
}
int N=(1<<K);
for(int i=0;i<N;i++)
for(int j=0;j<K;j++)
dp[i][j]=inff;
for(int i=0;i<K;i++) dp[(1<<i)][i]=ma[0][i+1];
int ans=inff;
for(int i=1;i<N;i++)
{
int flog=1;
for(int j=0;j<K;j++)
{
if(!(i&(1<<j)))
{
flog=0;
continue;
}
for(int k=0;k<K;k++)
{
if(j==k||!(i&(1<<k))) continue;
dp[i][j]=min(dp[i][j],dp[i^(1<<j)][k]+ma[k+1][j+1]);
}
}
if(flog)
{
for(int j=0;j<K;j++) ans=min(ans,dp[i][j]+ma[j+1][0]);
}
}
if(ans==inff) puts("0");
else printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: