首页 > 代码库 > HDU 4114 Disney's FastPass (状压DP)

HDU 4114 Disney's FastPass (状压DP)

题意:给定 n 个区域,然后给定两个区域经过的时间,然后你有 k 个景点,然后给定个每个景点的区域和有票没票的等待时间,从哪些区域能够得到票,问你从景点1开始,最后到景点1,而且要经过看完这k个景点。

析:一个状压DP,dp[s1][s2][i] 表示已经访问了 s1 中的景点,拥有 s2 的票,当前在 i 区域,然后两种转移一种是去下一个景点,另一种是去下一个区域拿票。当时输入,写错了,卡了好长时间。。。。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int mod = 1000;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}

int d[55][55];
int dp[1<<8][1<<8][55];

int ft[10], t[10], p[10];
int st[55];

int main(){
  int T;  cin >> T;
  for(int kase = 1; kase <= T; ++kase){
    int K;
    scanf("%d %d %d", &n, &m, &K);
    memset(d, INF, sizeof d);
    for(int i = 0; i < m; ++i){
      int u, v, val;
      scanf("%d %d %d", &u, &v, &val);
      --u, --v;
      d[u][v] = d[v][u] = val;
    }
    for(int i = 0; i < n; ++i)  d[i][i] = 0;
    memset(st, 0, sizeof st);
    for(int i = 0; i < K; ++i){
      int tt;
      scanf("%d %d %d %d", p+i, t+i, ft+i, &tt);
      --p[i];
      for(int j = 0; j < tt; ++j){
        int x;
        scanf("%d", &x);
        st[x-1] |= 1<<i;
      }
    }
    for(int k = 0; k < n; ++k)  for(int i = 0; i < n; ++i)
      for(int j = 0; j < n; ++j)
        if(d[i][k] != INF && d[k][j] != INF)
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

    memset(dp, INF, sizeof dp);
    dp[0][st[0]][0] = 0;
    int all = 1<<K;

    for(int i = 0; i < all; ++i)
      for(int j = 0; j < all; ++j){
        for(int k = 0; k < n; ++k){
          if(dp[i][j][k] == INF)  continue;
          for(int l = 0; l < K; ++l){
            if(i&(1<<l))  continue;
            int &tmp = dp[i|(1<<l)][j|st[p[l]]][p[l]];
            if(j&(1<<l))  tmp = min(tmp, dp[i][j][k] + d[k][p[l]] + ft[l]);
            else  tmp = min(tmp, dp[i][j][k] + d[k][p[l]] + t[l]);
          }
          for(int l = 0; l < n; ++l)
            dp[i][j|st[l]][l] = min(dp[i][j|st[l]][l], dp[i][j][k] + d[k][l]);
        }
      }

    int ans = dp[all-1][0][0];
    for(int i = 0; i < n; ++i)
      for(int j = 0; j < all; ++j)
        ans = min(dp[all-1][j][i] + d[i][0], ans);
    printf("Case #%d: %d\n", kase, ans);
  }
  return 0;
}

  

HDU 4114 Disney's FastPass (状压DP)