dp特集1,hdu 4568

6839 ワード

)、英雄互娯楽(杭州)
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
",dp[0][0]);/// dp00, 0 0, } return 0; }