poj 2490 Pimp My Ride

来源:互联网 发布:男人床上粗鲁 知乎 编辑:程序博客网 时间:2024/05/22 08:49

很明显的状态dp。

/* * Author: stormdpzh * Created Time: 2012/8/19 15:21:37 * File Name: poj_2490.cpp */#include <iostream>#include <cstdio>#include <sstream>#include <cstring>#include <string>#include <cmath>#include <vector>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>#include <functional>#define sz(v) ((int)(v).size())#define rep(i, n) for(int i = 0; i < n; i++)#define repf(i, a, b) for(int i = a; i <= b; i++)#define repd(i, a, b) for(int i = a; i >= b; i--)#define out(n) printf("%d\n", n)#define mset(a, b) memset(a, b, sizeof(a))#define wh(n) while(1 == scanf("%d", &n))#define whz(n) while(1 == scanf("%d", &n) && n != 0)#define lint long longusing namespace std;const int INF = 1 << 30;const int MaxN = 16;const int MaxM = (1 << 14) ^ 1;int cost[MaxN][MaxN];int f[MaxN][MaxM];int n;int t;void gao(){    for(int i = 0; i <= n; i++) for(int j = 0; j <= (1 << n); j++)        f[i][j] = INF;    for(int i = 0; i < n; i++) f[1][1 << i] = cost[i][i];    for(int i = 2; i <= n; i++) for(int j = 0; j < (1 << n); j++) {        if(f[i - 1][j] == INF) continue;        for(int k = 0; k < n; k++) {            if(j & (1 << k)) continue;            int id = j | (1 << k);            int c = f[i - 1][j] + cost[k][k];            for(int l = 0; l < n; l++) {                if(j & (1 << l)) c += cost[k][l];            }            f[i][id] = min(f[i][id], c);        }    }}int main(){    scanf("%d", &t);    int ans = 1;    while(t--) {        scanf("%d", &n);        int total = 0;        rep(i, n) rep(j, n) scanf("%d", &cost[i][j]);        gao();        printf("Scenario #%d:\n", ans++);        printf("You have officially been pimped for only $%d\n", f[n][(1 << n) - 1]);        puts("");    }    return 0;}

原创粉丝点击