HDU 4568 Hunter ( TSP + 状态压缩 )

来源:互联网 发布:粘土淘宝 编辑:程序博客网 时间:2024/04/30 05:34

HDU 4568 Hunter ( TSP + 状态压缩 )

分类: 图论 普通dp 272人阅读 评论(0) 收藏 举报
图论 状态压缩 TSP 松弛操作

Hunter

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


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
23 33 2 35 4 31 4 211 13 33 2 35 4 31 4 221 12 2

Sample Output
811

Source
2013 ACM-ICPC长沙赛区全国邀请赛——题目重现

Recommend
zhoujiaqi2010

题意:
一个迷宫,里面有一些格子有宝藏。每个格子都有一个值,代表访问这个格子的代价,如果是-1,则表示
不能访问。可以从任意一个格子的外面进和出一次。问带出所有宝藏的最小代价。如果他什么都拿不到,
输出-1.
每个格子可以访问任意次数,因为他不记得自己是否走过。

分析:
就是用松弛操作找出所有宝藏两两之间的最小代价 和 从每个宝藏出边界的最小代价。
然后用动态规划来更新状态和找指定状态。

感想:
虽然敲出来了,但是对dis[u]的更新过程还是理解的很模糊。。。。
虽然这题和poj 3229蛮像的,但是好难理解。因为有宝藏间的最短距离  和 宝藏和边界的最短距离。

代码:
[html] view plaincopy
  1. #include<cstdio>  
  2. #include<iostream>  
  3. #include<algorithm>  
  4. #include<cstring>  
  5. #include<queue>  
  6. #define INF 1e9  
  7. using namespace std;  
  8.   
  9. struct Node  
  10. {  
  11.     int id,dis;  
  12.     Node(){}  
  13.     Node(int a,int b)  
  14.     {  
  15.         id=a,dis=b;  
  16.     }  
  17.     bool operator <(const Node &dd) const  
  18.     {  
  19.         return dis>dd.dis;  
  20.     }  
  21. }t1;  
  22. int map[230][230],g[32][32],dp[20][1<<16];  
  23. int dx[4]={-1,1,0,0};  
  24. int dy[4]={0,0,-1,1};  
  25. int node[32],sum,tre[232][232];  
  26. int n,m,cos[32],vis[210*210],dis[210*210];  
  27.   
  28. void dij(int st,int pos)  
  29. {  
  30.     int i;  
  31.     memset(vis,0,sizeof(vis));  
  32.     priority_queue<Node> Q;  
  33.     for(i=0;i<sum;i++)  
  34.         dis[i]=INF;  
  35.     dis[st]=0;  
  36.     Q.push(Node(st,0));  
  37.     Node t1;  
  38.     while(!Q.empty())  
  39.     {  
  40.         t1=Q.top();  
  41.         Q.pop();  
  42.         int u=t1.id;  
  43.         if(vis[u]) continue;  
  44.         vis[u]=1;  
  45.         int x=u/m,y=u%m;  
  46.         if(tre[x][y]!=-1)    //如果是其他宝藏,更新当前宝藏到这个宝藏间的距离  
  47.             g[pos][tre[x][y]]=dis[u];  
  48.         if(x==n-1||x==0||y==m-1||y==0)    //如果是边界,更新宝藏到边界的距离  
  49.         {  
  50.             cos[pos]=min(cos[pos],dis[u]);  
  51.         }  
  52.         for(int i=0;i<4;i++)  
  53.         {  
  54.             int xx=x+dx[i];  
  55.             int yy=y+dy[i];  
  56.             if(map[xx][yy]==-1) continue;  
  57.             if(xx<0||xx>=n||yy<0||yy>=m) continue;  
  58.             int uu=xx*m+yy;  
  59.             if(dis[uu]>dis[u]+map[xx][yy])  
  60.             {  
  61.                 dis[uu]=dis[u]+map[xx][yy];  
  62.                 Q.push(Node(uu,dis[uu]));  
  63.             }  
  64.         }  
  65.     }  
  66. }  
  67.   
  68. int main()  
  69. {  
  70.     int T,i,j,k,p,ans,x,y;  
  71.     scanf("%d",&T);  
  72.     while(T--)  
  73.     {  
  74.        scanf("%d%d",&n,&m);  
  75.        sum=n*m;  
  76.        memset(tre,-1,sizeof(tre));  
  77.        memset(dp,0x3f,sizeof(dp));  
  78.        for(i=0;i<n;i++)  
  79.        {  
  80.            for(j=0;j<m;j++)  
  81.             scanf("%d",&map[i][j]);  
  82.        }  
  83.        scanf("%d",&k);  
  84.        for(i=0;i<k;i++)  
  85.        {  
  86.            scanf("%d%d",&x,&y);  
  87.            tre[x][y]=i;  
  88.            node[i]=x*m+y;  
  89.        }  
  90.        memset(g,0x3f,sizeof(g));  
  91.        for(i=0;i<k;i++)  
  92.        {  
  93.            g[i][i]=0;  
  94.            cos[i]=INF;  
  95.            dij(node[i],i);  
  96.        }  
  97.        for(i=0;i<k;i++)  
  98.        {  
  99.            int x1=node[i]/m;  
  100.            int y1=node[i]%m;  
  101.            dp[i][1<<i]=map[x1][y1]+cos[i];    //直接访问i点并出边界的代价  
  102.        }  
  103.        for(j=0;j<(1<<k);j++)  
  104.        {  
  105.            for(i=0;i<k;i++)  
  106.            {  
  107.                if(j&(1<<i)&&j!=(1<<i))  
  108.                {  
  109.                    for(p=0;p<k;p++)  
  110.                    {  
  111.                        if(j&(1<<p)&&j!=(1<<p)&&i!=p)  
  112.                        {  
  113.                            dp[i][j]=min(dp[i][j],dp[p][j-(1<<i)]+g[p][i]);  
  114.                        }  
  115.                    }  
  116.                }  
  117.            }  
  118.        }  
  119.        int ans=INF;  
  120.        for(i=0;i<k;i++)  
  121.        {  
  122.            ans=min(ans,dp[i][(1<<k)-1]+cos[i]);  
  123.        }  
  124.        printf("%d\n",ans);  
  125.    }  
  126.    return 0;  
  127. }  
  128.   
  129.   
  130. //AC   
  131. //140MS  
  132. /*  
  133.    tre[][]记录宝藏的编号  
  134.    g[][]记录两个宝藏间的距离  
  135.    cos[i]记录从i宝藏出边界的代价  
  136.    node[i]记录宝藏对于整个矩阵的相对位置  
  137. */  
0 0
原创粉丝点击