洛谷P1171_状压dp

dp[i][j]:状态为i,当前到达节点是j的最短路径
状态转移方程:dp[(1<< j)|i][j] = Min(dp[(1<< j)|i][j], dp[i][k]+a[k][j]);
宏定义的min比stl快

#include 
#include 
#include 
#define Min(a, b) a>b?b:a
using namespace std;

int n, a[20][20], dp[1<<20][20];

int main()
{
    scanf("%d", &n);
    int i, j, k;
    for(i = 0; i < n; ++i)
        for(j = 0; j < n; ++j)
            scanf("%d", &a[i][j]);
    memset(dp, 0x3f, sizeof(dp));
    dp[1][0] = 0;
    for(i = 0; i < (1<//枚举状态
        for(j = 0; j < n; ++j)//枚举将要到达的地点
        {
            if((1<continue;
            for(k = 0; k < n; ++k)//枚举中间节点
            {
                if(!((1<continue;
                dp[(1<1<int ans = 0x3f3f3f3f;
    for(i = 1; i < n; ++i)
        ans = Min(ans, dp[(1<1][i]+a[i][0]);
    printf("%d\n", ans);
    return 0;
}

你可能感兴趣的:(状压dp)