hdu 4568 dij建图+ tsp模板

Hunter

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


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

题意: 从边界进去拿出尽量多的宝藏出来。

思路: 边界设为点0 跑tsp

代码:

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f

using namespace std;
const int N =205;

int dis[20][20];
int is[N][N];
int mp[N][N];
int isx[15],isy[15];
int vis[N][N];
int n,m,k;
int ne[4][2]={0,1, 1,0 ,-1,0, 0,-1 };
vector< int >ans;

struct node{
	int x,y;
	int cost;
	bool operator <(const node &a)const{
	    return a.cost<cost;
	}
};

priority_queue< node >q;

void bfs(int stx,int sty)
{
    int num=is[stx][sty];
    node tmp,tmp1;
    tmp.x=stx; tmp.y=sty; tmp.cost=0;
    memset(vis,0,sizeof(vis));
    while(!q.empty()) q.pop();
    int x,y,tx,ty,cost;
    q.push(tmp);
    while(!q.empty())
    {
        tmp=q.top(); q.pop();
        x=tmp.x;  y=tmp.y; cost=tmp.cost;
        if(vis[x][y]) continue;
        vis[x][y]=1;
        //cout<<"x "<<x<<" y "<<y<<" cost "<<cost<<endl;
        if(is[x][y]){
            dis[num][is[x][y]]=min(dis[num][is[x][y]],cost);
        }

        for(int i=0;i<4;i++)
        {
            tx=x+ne[i][0]; ty=y+ne[i][1];
            if(tx<1||tx>n||ty<1||ty>m){
                dis[num][0]=min(dis[num][0],cost);
                dis[0][num]=min(dis[0][num],cost+mp[stx][sty]);
                continue;
            }
            if(mp[tx][ty]==-1) continue;
            if(vis[tx][ty]) continue;
            tmp1.x=tx;  tmp1.y=ty;
            tmp1.cost=cost+mp[tx][ty];
            q.push(tmp1);
        }
    }
    return ;
}

int dp[1<<15][15];

void get_ans(int n)
{
    memset(dp, inf, sizeof(dp));
    ans.push_back(0);
    int ens=0;
    int sz=ans.size();
    for(int i=0;i<sz;i++)
    {
        int num=ans[i];
        int x=1<<(num);
        ens=ens|x;
    }
    //cout<<"state "<<ins<<endl;

    dp[ens][0]=0;
    for(int s=ens-1;s>=0;s--)
        for(int v=0;v<=n;v++)
            for(int u=0;u<=n;u++)
                if(!(s>> u & 1))
                    dp[s][v]=min(dp[s][v], dp[s|(1<<u)][u]+dis[v][u]);

}


void init()
{
    memset(dis,inf,sizeof(dis));
    memset(is,0,sizeof(is));
    memset(mp,0,sizeof(mp));
}

void show()
{
    for(int i=0;i<=k;i++)
    {
        for(int j=0;j<=k;j++) cout<<dis[i][j]<<" ";
        cout<<endl;
    }
}

void tsp(int n)
{
    memset(dp, inf, sizeof(dp));
    dp[(1<<n)-1][0]=0;
    for(int s=(1<<n)-2;s>=0;s--)
        for(int v=0;v<n;v++)
            for(int u=0;u<n;u++)
                if(!(s>> u & 1))
                    dp[s][v]=min(dp[s][v], dp[s|(1<<u)][u]+dis[v][u]);
    printf("%d\n",dp[0][0]);
}

/*
void tsp(int tre)
{
        memset(dp,inf ,sizeof(dp));
        for(int i=0;i<=tre;i++)
        {
            dp[1<<i][i]=dis[0][i];
        }
        int end=(1<<(tre+1));
        for(int i=0;i<end;i++)
        {
            for(int j=0;j<=tre;j++)
            {
                if((i>>j)&1)
                {
                    for(int k=0;k<=tre;k++)
                    {
                        if((i>>k)&1)
                        {
                            dp[i][j]=min(dp[i][j],dp[i&(~(1<<j))][k]+dis[k][j]);
                        }
                    }
                }
            }
        }
        printf("%d\n",dp[end-1][0]);
}
*/

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&m);
        init();
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&mp[i][j]);
            }
        }
        for(int i=0;i<=m+1;i++) is[0][i]=is[n+1][i]=1;
        for(int i=0;i<=n+1;i++) is[i][0]=is[i][m+1]=1;

        scanf("%d",&k);

        for(int i=1;i<=k;i++)
        {
            scanf("%d %d",&isx[i],&isy[i]);
            isx[i]++;  isy[i]++;
            is[isx[i]][isy[i]]=i;
        }
        dis[0][0]=0;
        for(int i=1;i<=k;i++)
        {
            bfs(isx[i],isy[i]);
           // show();
        }
        /*for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(is[i][j]&&!vis1[is[i][j]]){
                   // cout<<"index  "<<i<<" "<<j<<endl;
                    bfs1(i,j);
                }
            }
        }
        */
        tsp(k+1);

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yjt9299/article/details/80515348