dp专题1,,hdu4568

Hunter

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


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
有一个n*m的棋盘,每个位置有一个数字,表示经过此点需要的花费,下面给出k个藏宝点坐标,你可以从外界任意一个点进入,拿走所有宝藏并可以从任意一个点出来,问最小花费是多少;;
分析:
 将外界看做一个顶点,从外界进入并经过K个顶点再回到外界,求花费最少, 这就是一个旅行商问题,状态压缩dp,不是很懂旅行商问题,但是书上有个模板 剩下要考虑的事情就是把k+1个顶点之间两两距离算出来,(矩阵表示,旅行商问题 时间复杂度为O(2^n *n^2),本题k<=13,显然不会超时)
<pre name="code" class="cpp">#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
const int maxn=550,inf=0x3f3f3f3f;
using namespace std;
struct point
{
    int x,y,cost;
    point(){;}
    point(int xx,int yy,int Cost)
    {
        x=xx;
        y=yy;
        cost=Cost;
    }
    friend bool operator<(const point a,const point b)
    {
    return a.cost>b.cost;
    }
}kc[30];
int dx[4]={0,1,0,-1};
int dy[4]={-1,0,1,0};
int n,m,k;
int map[maxn][maxn];
int d[30][30];
int isc[maxn][maxn];///宝藏标记图
bool vis[maxn][maxn],visd[30];
void bfs(point tep,int I)///bfs求第I个顶点到其他k个顶点的最短距离
{
    priority_queue<point>que;///优先队列,花费最少的先出,保证是最短路
    que.push(tep);
    int tot=0;///计数,tot==k是退出函数,找全顶点聚停止
    memset(visd,false,sizeof(visd));
    memset(vis,false,sizeof(vis));
    vis[tep.x][tep.y]=true;
    visd[I]=true;
    int nx,ny;
    while(!que.empty())
    {
        point now=que.top();
        que.pop();
        vis[now.x][now.y]=true;
        nx=now.x,ny=now.y;
        if(!visd[0]&&(nx==1||nx==n||ny==1||ny==m))///判断外界,是外界,用0表示外界顶点
        {
            tot++;
            visd[0]=true;
            d[I][0]=now.cost;///宝藏到外界距离
            d[0][I]=now.cost+map[tep.x][tep.y];///外界到宝藏距离
           if(tot==k)
           return;
        }
        int ii=isc[now.x][now.y];
        if(ii>0&&!visd[ii])///若果是宝藏点且没有被访问过
        {
            tot++;
            visd[ii]=true;
            d[I][ii]=now.cost;
            if(tot==k)///找全k个宝藏点就退出
                return;
        }
        for(int j=0;j<4;j++)///四个方向搜索
        {
            nx=now.x+dx[j];
            ny=now.y+dy[j];
            if(nx<1||nx>n||ny<1||ny>m)
                continue;
            if(!vis[nx][ny]&&map[nx][ny]!=-1)
            {
                vis[nx][ny]=true;
                que.push(point(nx,ny,now.cost+map[nx][ny]));
            }
        }
    }
}
const int K=14;
int dp[1<<K][K];
int main()
{
    int T;
    int cx,cy;
    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",&map[i][j]);
       }
       scanf("%d",&k);
       for(int i=0;i<=k;i++)
       {
           for(int j=0;j<=k;j++)
           {
               if(i==j)
                d[i][j]=0;
               else
                d[i][j]=inf;
           }
       }
       memset(isc,0,sizeof(isc));
       for(int i=1;i<=k;i++)
       {
           scanf("%d%d",&cx,&cy);
           cx++,cy++;
           isc[cx][cy]=i;
           kc[i].x=cx;
           kc[i].y=cy;
           kc[i].cost=0;
       }
       for(int i=1;i<=k;i++)
            bfs(kc[i],i);
        ///压缩dp开始了,不很懂QAQ
        memset(dp,inf,sizeof(dp));
        dp[(1<<(k+1))-1][0]=0;
        for(int s=(1<<(k+1))-1;s>=0;s--){
            for(int pp=0;pp<k+1;pp++){
                for(int qq=0;qq<k+1;qq++){
                    if(!(s&(1<<qq)))
                    dp[s][pp]=min(dp[s][pp],dp[s|(1<<qq)][qq]+d[pp][qq]);
                }
            }
        }
        if(dp[0][0]>=inf)
        puts("0");
        else
        printf("%d\n",dp[0][0]);///输出dp00,有0出发回到0,即由外界出发回到外界
    }
    return 0;
}



 
    
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值