hdu 4856 Tunnels 狀態壓縮dp
阿新 • • 發佈:2017-10-11
con 通道 panel follow span scan urn left cit
Input
The input contains mutiple testcases. Please process till EOF.
For each testcase, the first line contains two integers N (1 ≤ N ≤ 15), the side length of the square map and M (1 ≤ M ≤ 15), the number of tunnels.
The map of the city is given in the next N lines. Each line contains exactly N characters. Barrier is represented by “#” and empty grid is represented by “.”.
Then M lines follow. Each line consists of four integers x1, y1, x2, y2, indicating there is a tunnel with entrence in (x1, y1) and exit in (x2, y2). It’s guaranteed that (x1, y1) and (x2, y2) in the map are both empty grid.
If it is impossible for Bob to visit all the tunnels, output -1.
Tunnels
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
For each testcase, the first line contains two integers N (1 ≤ N ≤ 15), the side length of the square map and M (1 ≤ M ≤ 15), the number of tunnels.
The map of the city is given in the next N lines. Each line contains exactly N characters. Barrier is represented by “#” and empty grid is represented by “.”.
Output For each case, output a integer indicating the minimal time Bob will use in total to walk between tunnels.
Sample Input 5 4 ....# ...#. ..... ..... ..... 2 3 1 4 1 2 3 5 2 3 3 1 5 4 2 1
Sample Output 7
Source 2014西安全國邀請賽
題意:一個圖,#不可達,m條單向通道,點間花費1時間,通道起點到終點不花費時間,求最少花費時間;
思路:狀態壓縮dp,dp[i][j] i表示已經走過哪些通道,j表示最後的走的那條通道是哪條。
dp[i][j]=dp[i-(1<<(j-1)][k] +dis(k,j) dis(k,j)表示第k條通道的終點到第j條通道的起點距離;
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> #include<bitset> #include<time.h> using namespace std; #define LL long long #define pi (4*atan(1.0)) #define eps 1e-8 #define bug(x) cout<<"bug"<<x<<endl; const int N=10+10,M=1e6+10,inf=1e9+7,MOD=1e9+7; const LL INF=1e18+10,mod=1e9+7; int n,m,vis[N][N]; char a[N][N]; struct is { int s,t,e,d; } q[N]; int check(int x,int y) { if(x<=0||x>n||y<=0||y>n)return 0; return 1; } int dis[N][N][N][N]; int xx[5]= {1,0,-1,0}; int yy[5]= {0,1,0,-1}; void bfs(int s,int t) { queue<pair<int,int> >q; q.push(make_pair(s,t)); memset(vis,0,sizeof(vis)); vis[s][t]=1;dis[s][t][s][t]=0; while(!q.empty()) { pair<int,int> p=q.front(); q.pop(); for(int i=0; i<4; i++) { int x=p.first+xx[i]; int y=p.second+yy[i]; if(check(x,y)&&!vis[x][y]&&a[x][y]==‘.‘) { vis[x][y]=1; dis[s][t][x][y]=dis[s][t][p.first][p.second]+1; q.push(make_pair(x,y)); } } } } int dp[50000][N]; int main() { while(~scanf("%d%d",&n,&m)) { memset(dis,-1,sizeof(dis)); for(int i=1; i<=n; i++) scanf("%s",a[i]+1); for(int i=1; i<=m; i++) scanf("%d%d%d%d",&q[i].s,&q[i].t,&q[i].e,&q[i].d); for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)bfs(i,j); memset(dp,-1,sizeof(dp)); for(int i=1;i<=(1<<m)-1;i++) { for(int j=1;j<=m;j++) { if((1<<(j-1))&i) { int now=i-(1<<(j-1)); if(!now) { dp[i][j]=0; continue; } for(int k=1;k<=m;k++) { if(now&(1<<(k-1))) { if(dp[now][k]!=-1&&dis[q[k].e][q[k].d][q[j].s][q[j].t]!=-1) { int temp=dp[i][j]; dp[i][j]=dp[now][k]+dis[q[k].e][q[k].d][q[j].s][q[j].t]; if(temp!=-1)dp[i][j]=min(dp[i][j],temp); } } } } } } int ans=inf; for(int i=1;i<=m;i++) if(dp[(1<<m)-1][i]!=-1)ans=min(ans,dp[(1<<m)-1][i]); if(ans!=inf)printf("%d\n",ans); else printf("-1\n"); } return 0; }
hdu 4856 Tunnels 狀態壓縮dp