HDU 4568 hunter short circuit + pressure DP

Source: Internet
Author: User

Give an N * m lattice with some numbers. If it is a positive integer, It is the cost of the lattice. If it is-1, it indicates that the lattice cannot be reached, now, I want to give the location of K treasures (k <= 13), ask a person to enter the entire chessboard from a point outside the boundary, and then take away the minimum cost of all the treasures that can be taken away, if you cannot take all the items you can get at one time or you cannot get any treasures at all, output 0.

Solution: we can see that the range of K should think of State compression. Each grid is regarded as a vertex, and two new vertices are created. One is the start point outside the boundary, and the other is represented by 0, an end point outside the boundary is represented by N * m + 1, and then an edge is created and a directed edge is built. The edge weight is the cost value of the destination grid. (In fact, no edge is required, directly run the Shortest Path) and then find the shortest path between the K + 2 points, and then convert it into a TSP problem, which can be solved by pressing the DP.

The shortest distance between K + 2 points can be obtained by running K + 2 spfa, which is not complex.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#define Mod 1000000007using namespace std;#define N 10007int mp[304][304];int C[304][304];int dis[20][20];int n,m,k;int d[50005];struct node{    int v,w,next;}G[4*50005];int head[4*50005],tot;int dx[4] = {0,0,1,-1};int dy[4] = {1,-1,0,0};int vis[50006];struct Point{    int x,y;}P[15];int OK(int nx,int ny){    if(nx >= 1 && nx <= n && ny >= 1 && ny <= m)        return 1;    return 0;}void addedge(int u,int v,int w){    G[tot].v = v;    G[tot].w = w;    G[tot].next = head[u];    head[u] = tot++;}void SPFA(int s){    queue<int> que;    while(!que.empty())        que.pop();    memset(vis,0,sizeof(vis));    vis[s] = 1;    que.push(s);    for(int i=0;i<=n*m+1;i++)        d[i] = Mod;    d[s] = 0;    while(!que.empty())    {        int u = que.front();        que.pop();        vis[u] = 0;        for(int i=head[u];i!=-1;i=G[i].next)        {            int v = G[i].v;            int w = G[i].w;            if(d[v] > d[u] + w)            {                d[v] = d[u] + w;                if(!vis[v])                {                    vis[v] = 1;                    que.push(v);                }            }        }    }}int dp[1<<17][20];int main(){    int i,j;    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        for(i=1;i<=n;i++)            for(j=1;j<=m;j++)            {                scanf("%d",&C[i][j]);                if(C[i][j] == -1)                    C[i][j] = Mod;            }        memset(head,-1,sizeof(head));        tot = 0;        for(i=1;i<=n;i++)        {            for(j=1;j<=m;j++)            {                int now = (i-1)*m + j;                for(int h=0;h<4;h++)                {                    int kx = i + dx[h];                    int ky = j + dy[h];                    if(!OK(kx,ky))                        continue;                    int tmp = (kx-1)*m + ky;                    addedge(now,tmp,C[kx][ky]);                }                if(i == 1 || i == n || j == 1 || j == m)                {                    addedge(0,now,C[i][j]);                    addedge(now,n*m+1,0);                }            }        }        scanf("%d",&k);        P[0].x = 1, P[0].y = 0;        P[k+1].x = n,P[k+1].y = m+1;        for(i=1;i<=k;i++)            scanf("%d%d",&P[i].x,&P[i].y),P[i].x++,P[i].y++;        for(i=0;i<=k+1;i++)        {            int s = (P[i].x-1)*m + P[i].y;            SPFA(s);            for(j=0;j<=k+1;j++)            {                if(i == j) continue;                int v = (P[j].x-1)*m + P[j].y;                dis[i][j] = d[v];            }        }        for(i=0;i<(1<<16);i++)        {            for(j=0;j<16;j++)                dp[i][j]=Mod;        }        for(i=0;i<k;i++)        {            dp[1<<i][i+1]=dis[0][i+1];        }        for(i=0;i<(1<<k);i++)        {            for(int kk=0;kk<k;kk++)            {                if(!(i&(1<<kk)))continue;                for(int j=0;j<k;j++)                {                    if(i&(1<<j)) continue;                    dp[i+(1<<j)][j+1]=min(dp[i+(1<<j)][j+1],dp[i][kk+1]+dis[kk+1][j+1]);                }            }        }        int minn=Mod;        for(i=1;i<=k;i++)            minn=min(dp[(1<<k)-1][i]+dis[i][k+1],minn);        if(minn == Mod)            cout<<0<<endl;        else            cout<<minn<<endl;    }    return 0;}
View code

 

HDU 4568 hunter short circuit + pressure DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.