lightoj1037 状压DP(入门级)

    xiaoxiao2021-03-26  26

    题意

    简单

    思路

    dp[sta]表示状态为sta时需要打的最少次数,dp[0] = 0;

    /***************************************** Author :Crazy_AC(JamesQi) Time :2016 File Name : *****************************************/ // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <algorithm> #include <iomanip> #include <sstream> #include <string> #include <stack> #include <queue> #include <deque> #include <vector> #include <map> #include <set> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <climits> using namespace std; #define MEM(x,y) memset(x, y,sizeof x) #define pk push_back #define lson rt << 1 #define rson rt << 1 | 1 #define bug cout << "BUG HERE\n" #define debug(x) cout << #x << " = " << x << endl #define ALL(v) (v).begin(), (v).end() #define lowbit(x) ((x)&(-x)) #define Unique(x) sort(ALL(x)); (x).resize(unique(ALL(x)) - (x).begin()) #define BitOne(x) __builtin_popcount(x) #define showtime printf("time = %.15f\n",clock() / (double)CLOCKS_PER_SEC) #define Rep(i, l, r) for (int i = l;i <= r;++i) #define Rrep(i, r, l) for (int i = r;i >= l;--i) typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> ii; typedef pair<ii,int> iii; const double eps = 1e-8; const double pi = 4 * atan(1); const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const int MOD = 1e9 + 7; int nCase = 0; //精度正负、0的判断 int dcmp(double x){if (fabs(x) < eps) return 0;return x < 0?-1:1;} template<class T> inline bool read(T &n){ T x = 0, tmp = 1; char c = getchar(); while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar(); if(c == EOF) return false; if(c == '-') c = getchar(), tmp = -1; while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar(); n = x*tmp; return true; } template <class T> inline void write(T n){ if(n < 0){putchar('-');n = -n;} int len = 0,data[20]; while(n){data[len++] = n%10;n /= 10;} if(!len) data[len++] = 0; while(len--) putchar(data[len]+48); } LL QMOD(LL x, LL k) { LL res = 1LL; while(k) {if (k & 1) res = res * x % MOD;k >>= 1;x = x * x % MOD;} return res; } int n; int healthy[16]; char a[16][16]; int dp[1 << 16]; // int dfs(int sta) { // if (!sta) return 0; // if (dp[sta] != -1) return dp[sta]; // dp[sta] = inf; // for (int i = 0;i < n;++i) { // if ((sta >> i) & 1) { // int maxv = 1; // for (int j = 0;j < n;++j) { // maxv = max(maxv, sta >> j & 1 ? 0 : a[j][i] - '0'); // } // dp[sta] = min(dp[sta], dfs(sta^(1<<i)) + (healthy[i] + maxv - 1) / maxv); // } // } // return dp[sta]; // } int solve() { memset(dp, inf, sizeof dp); dp[0] = 0; for (int sta = 0;sta < (1 << n);++sta) { for (int i = 0;i < n;++i) { if ((sta & (1 << i)) == 0) { int maxv = 1; for (int j = 0;j < n;++j) { maxv = max(maxv, sta >> j & 1 ? a[j][i] - '0' : 0); } dp[sta ^ (1 << i)] = min(dp[sta ^ (1 << i)], dp[sta] + (healthy[i] + maxv - 1) / maxv); } } } return dp[(1 << n) - 1]; } int main(int argc, const char * argv[]) { // freopen("/Users/jamesqi/Desktop/in.txt","r",stdin); // freopen("/Users/jamesqi/Desktop/out.txt","w",stdout); // ios::sync_with_stdio(false); // cout.sync_with_stdio(false); // cin.sync_with_stdio(false); int kase;cin >> kase; while(kase--) { cin >> n; Rep(i, 0, n - 1) cin >> healthy[i]; Rep(i, 0, n - 1) cin >> a[i]; memset(dp, -1, sizeof dp); int ans = solve(); // int ans = dfs((1 << n) - 1); printf("Case %d: %d\n", ++nCase, ans); } // showtime; return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-662359.html

    最新回复(0)