其他分享
首页 > 其他分享> > 【洛谷习题】售货员的难题

【洛谷习题】售货员的难题

作者:互联网

题目链接:https://www.luogu.org/problemnew/show/P1171


 

比较经典的状压DP吧,就是最后一个点不开O2卡不过去。。。

主要是对位运算熟,注意f[1][0]=0,其他状态为正无穷,别的看代码吧。

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 using namespace std;
 5 
 6 inline int get_num() {
 7     int num = 0;
 8     char c = getchar();
 9     while (c < '0' || c > '9') c = getchar();
10     while (c >= '0' && c <= '9')
11         num = num * 10 + c - '0', c = getchar();
12     return num;
13 }
14 
15 const int maxn = 20;
16 
17 int dist[maxn][maxn], f[1 << maxn][maxn];
18 
19 int main() {
20     int n, ans = 0x3f3f3f3f;
21     n = get_num();
22     for (register int i = 0; i < n; ++i)
23         for (register int j = 0; j < n; ++j)
24             dist[i][j] = get_num();
25     memset(f, 0x3f, sizeof(f));
26     f[1][0] = 0;
27     for (register int i = 1; i < 1 << n; ++i)
28         for (register int j = 0; j < n; ++j) if (i >> j & 1)
29             for (register int k = 0; k < n; ++k) if ((i ^ 1 << j) >> k & 1)
30                 if (f[i ^ 1 << j][k] + dist[k][j] < f[i][j])
31                     f[i][j] = f[i ^ 1 << j][k] + dist[k][j];
32     for (register int i = 0; i < n; ++i)
33         if (f[(1 << n) - 1][i] + dist[i][0] < ans)
34             ans = f[(1 << n) - 1][i] + dist[i][0];
35     printf("%d", ans);
36     return 0;
37 }
90分代码

 

标签:洛谷,int,代码,while,num,习题,include,售货员,getchar
来源: https://www.cnblogs.com/Mr94Kevin/p/10380586.html