标签:des style blog http color java os io
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 841 Accepted Submission(s): 247
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <utility> #include <queue> #define inside(x) ((x)>0 && (x)<=n) using namespace std; typedef long long ll; typedef double DB; typedef pair<int,int> pii; const int INF = 1e9; const DB eps = 1e-6; const int N = 20; const int dx[]={1,-1,0,0}; const int dy[]={0,0,1,-1}; // http://106.185.28.251/vjudge/contest/view.action?cid=53977#overview char g[N][N]; struct _edge{ int sx,sy,ex,ey; }e[N]; int n,m; int w[N][N],dis[N][N]; bool vis[N][N]; int dp[1<<17][N]; bool dpvis[1<<17][N]; void build_dp() { int maxn=1<<m; for(int i=0;i<maxn;++i) for(int j=0;j<m;++j) dp[i][j]=INF; queue<pii> q; memset(dpvis,0,sizeof(dpvis)); for(int i=0;i<m;++i) { dp[1<<i][i]=0; q.push(make_pair(1<<i,i)); } int s,u,ns; while(!q.empty()) { s = q.front().first; u = q.front().second; q.pop(); if(dpvis[s][u]) continue; dpvis[s][u]=1; for(int v=0;v<m;++v) { if(w[u][v]==-1 || (s&(1<<v))) continue; ns = s|(1<<v); if(dp[s][u]+w[u][v] < dp[ns][v]) { dp[ns][v] = dp[s][u]+w[u][v]; q.push(make_pair(ns,v)); } } } // for(int i=0;i<maxn;++i) // { // printf("%x: ",i); // for(int j=0;j<m;++j) printf("%d ",dp[i][j]); // puts(""); // } } void bfs(int x,int y) { memset(vis,0,sizeof(vis)); dis[x][y]=0; vis[x][y]=1; queue<pii> q; q.push(make_pair(x,y)); while(!q.empty()) { x=q.front().first; y=q.front().second; q.pop(); for(int i=0;i<4;++i) { int nx=x+dx[i], ny=y+dy[i]; if(inside(nx) && inside(ny) && !vis[nx][ny] && g[nx][ny]!=‘#‘) { vis[nx][ny]=1; dis[nx][ny]=dis[x][y]+1; q.push(make_pair(nx,ny)); } } } } void run() { for(int i=1;i<=n;++i) { scanf("%s",g[i]); for(int j=n;j>0;--j) g[i][j]=g[i][j-1]; } for(int i=0;i<m;++i) scanf("%d%d%d%d",&e[i].sx,&e[i].sy,&e[i].ex,&e[i].ey); if(m==1) { puts("0"); return; } for(int i=0;i<m;++i) { bfs(e[i].ex,e[i].ey); for(int j=0;j<m;++j) { if(!vis[e[j].sx][e[j].sy]) w[i][j]=-1; else w[i][j]=dis[e[j].sx][e[j].sy]; } w[i][i]=-1; } // for(int i=0;i<m;++i) // { // for(int j=0;j<m;++j) // cout<<w[i][j]<<‘ ‘; // cout<<endl; // } build_dp(); int ans=INF; int ts=0; for(int i=0;i<m;++i) ts|=(1<<i); for(int i=0;i<m;++i) ans=min(ans,dp[ts][i]); if(ans==INF) puts("-1"); else printf("%d\n",ans); } int main() { #ifdef LOCAL freopen("in","r",stdin); #endif while(scanf("%d%d",&n,&m)!=EOF) run(); return 0; }
HDU 4856 Tunnels,布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/YRETSIM/p/3918443.html