hdu 4568 bfs + 状压dp

//这题的数据是不是有问题... 不考虑宝藏一个也拿不到也能AC...

  1 #include "bits/stdc++.h"
  2 using namespace std;
  3 const int INF = 0x3f3f3f3f;
  4 int T;
  5 int N, M;
  6 int mat[210][210];
  7 int K;
  8 int tot_tra, tra[210][210];
  9 
 10 //dp parameters
 11 int dis_tra_broder[20], dis_tra_tra[20][20];
 12 int dp[10000][20];
 13 
 14 //bfs parameters
 15 struct Node
 16 {
 17     int row, col;
 18     int cost;
 19     bool operator< (const Node &tmp) const
 20     {
 21         return cost > tmp.cost;
 22     }
 23 }now, Next, begin_pos[20];
 24 
 25 int dis[210][210];
 26 bool vis[210][210];
 27 
 28 inline bool check_border(int row, int col)
 29 {
 30     if(row == 1 || row == N || col == 1 || col == M) {
 31         return 1;
 32     }
 33     return 0;
 34 }
 35 
 36 inline bool check(int row, int col)
 37 {
 38     if(row < 1 || row > N || col < 1 || col > M) {
 39         return 0;
 40     }
 41     return 1;
 42 }
 43 
 44 
 45 int dx[] = {0, 0, -1, 1};
 46 int dy[] = {-1, 1, 0, 0};
 47 
 48 void bfs(int index_tra)
 49 {
 50     priority_queue<Node> q;
 51     q.push(begin_pos[index_tra]);
 52     memset(vis, 0, sizeof(vis));
 53     memset(dis, INF, sizeof(dis));
 54     dis[ begin_pos[index_tra].row ][ begin_pos[index_tra].col ] = 0;
 55     while(!q.empty()) {
 56         now = q.top();
 57         q.pop();
 58         if(vis[now.row][now.col]) {
 59             continue;
 60         }
 61         vis[now.row][now.col] = 1;
 62         if(tra[now.row][now.col]) {
 63             dis_tra_tra[index_tra][ tra[now.row][now.col] ] = now.cost;
 64         }
 65         if(check_border(now.row, now.col)) {
 66             dis_tra_broder[index_tra] = min(dis_tra_broder[index_tra], now.cost);
 67         }
 68         int i;
 69         for(i = 0; i < 4; ++i) {
 70             Next.row = now.row + dx[i];
 71             Next.col = now.col + dy[i];
 72             if(check(Next.row, Next.col) && mat[Next.row][Next.col] != -1) {
 73                 Next.cost = now.cost + mat[Next.row][Next.col];
 74                 if(Next.cost < dis[Next.row][Next.col]) {
 75                     dis[Next.row][Next.col] = Next.cost;
 76                     q.push(Next);
 77                 }
 78             }
 79         }
 80     }
 81 }
 82 
 83 int main()
 84 {
 85     scanf("%d", &T);
 86     while(T--) {
 87         memset(tra, 0, sizeof(tra));
 88         memset(dis_tra_broder, INF, sizeof(dis_tra_broder));
 89         memset(dis_tra_tra, INF, sizeof(dis_tra_tra));
 90         memset(dp, INF, sizeof(dp));
 91         scanf("%d%d", &N, &M);
 92         int i, j;
 93         for(i = 1; i <= N; ++i) {
 94             for(j = 1; j <= M; ++j) {
 95                 scanf("%d", &mat[i][j]);
 96             }
 97         }
 98         scanf("%d", &K);
 99         int row, col;
100         for(i = 1; i <= K; ++i) {
101             scanf("%d%d", &row, &col);
102             ++row;
103             ++col;
104             tra[row][col] = i;
105             begin_pos[i].row = row;
106             begin_pos[i].col = col;
107 //            begin_pos[i].cost = 0;
108         }
109         for(i = 1; i <= K; ++i) {
110             bfs(i);
111         }
112         int tot_s = (1 << K) - 1;
113         int s, step_s, u, pre_s, pre_step_s, v;
114         for(u = 1; u <= K; ++u) {
115             dp[(1 << (u - 1))][u] = dis_tra_broder[u] + mat[begin_pos[u].row][begin_pos[u].col];
116         }
117         for(s = 3; s <= tot_s; ++s) {
118             for(u = 1; u <= K; ++u) {
119                 step_s = (1 << (u - 1));
120                 if((s & step_s) && (step_s != s)) {
121                     pre_s = s ^ step_s;
122                     for(v = 1; v <= K; ++v) {
123                         pre_step_s = (1 << (v - 1));
124                         if(pre_s & pre_step_s) {
125                             dp[s][u] = min(dp[s][u], dp[pre_s][v] + dis_tra_tra[v][u]);
126                         }
127                     }
128                 }
129             }
130         }
131         int res = INF;
132         for(u = 1; u <= K; ++u) {
133             res = min(res, dp[tot_s][u] + dis_tra_broder[u]);
134         }
135         printf("%d\n", res);
136     }
137 }

 

posted @ 2015-07-17 14:50  AC_Phoenix  阅读(129)  评论(0编辑  收藏  举报