NOIP2017D2T2 宝藏

本文介绍了一种使用状态压缩动态规划解决复杂组合问题的方法。通过枚举状态补集的子集来转移状态,利用lowbit技巧优化计算过程。此外,还讨论了另一种枚举每一层状态并仅枚举一个未找到的点进行转移的策略。

考试的时候yy了一发 可以枚举层数 然后枚举当前层选那些 但是我不会枚举一个状态补集的子集 于是就GG了 昨天看了CQzhangyu的博客 发现原来 lowbit 可以实现 我才想到 lowbit 就是找了的从右至左的第一个1的位置 这样就可以很快转移到每个状态了 茅塞顿开

f[i][s]isp,f[i+1][s|p]=min(f[i+1][s|p],f[i][s]+cost[p])

计算补集的子集状态和费用可以通过前面说的 lowbit 实现 复杂 O(n3n 具体看代码(感觉我跟抄了一遍一样)吧

哦对了 还有一个思路 是zjqaq大爷想的 直接枚举每一层的状态 然后只枚举一个未找的点 转移到下一层 我觉得这个好像是 O(n32n) 的复杂度 不过确实可过(为啥我没想出来呢 QAQ)

#include<bits/stdc++.h>
#define bug(x) cout<<(#x)<<" "<<(x)<<endl
#define inf 99999999
#define ll long long
#define lowbit(x) (x&(-x))
using namespace std;
const int N=13;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
ll n,m,cnt,ans=inf; 
ll a[N][N],po[N],v[N],zjqaq[(1<<N)],val[(1<<N)],f[N][(1<<N)],zjq[(1<<N)];
int main(){
#ifdef Devil_Gary
    freopen("in.txt","r",stdin);
#endif
    cout<<lowbit(1)<<endl;
    n=read(),m=read();
    for(int i=0;i<n;i++) for(int j=0;j<n;j++) a[i][j]=inf;
    memset(f,0x3f,sizeof f);
    for(int i=1;i<=m;i++){
        ll x=read()-1,y=read()-1,z=read();
        a[x][y]=a[y][x]=min(a[x][y],z);
    }
    for(int i=0;i<n;i++) f[0][1<<i]=0,zjq[1<<i]=i;
    for(int i=0;i<n;i++){
        for(int o=0;o<(1<<n);o++){
            cnt=0;
            for(int j=0;j<n;j++){
                if(!(o&(1<<j))){
                    po[cnt]=(1<<j),v[cnt]=inf;
                    for(int k=0;k<n;k++)
                        if(o&(1<<k))
                            v[cnt]=min(v[cnt],a[j][k]*(i+1));
                    ++cnt;
                }
            }
            for(int m=1;m<(1<<cnt);m++){
                val[m]=val[m-lowbit(m)]+v[zjq[lowbit(m)]];
                zjqaq[m]=zjqaq[m-lowbit(m)]+po[zjq[lowbit(m)]];
                f[i+1][o+zjqaq[m]]=min(f[i+1][o+zjqaq[m]],f[i][o]+val[m]);
            }
        }
    }
    for(int i=0;i<=n;i++) ans=min(ans,f[i][(1<<n)-1]);
    cout<<ans<<endl;
    return 0;
}
NOIP2017(全国青少年信息学奥林匹克联赛)的题目涵盖了多个算法与数据结构方面的挑战,包括模拟、图论、动态规划等。以下是一些NOIP2017提高组的题目参考代码,供学习和训练使用。 ### 1. **时间复杂度分析(T3)** 该题要求根据伪代码判断程序的时间复杂度。核心在于解析循环结构,判断嵌套与并列关系,并计算复杂度。 ```cpp #include <bits/stdc++.h> using namespace std; int T, n; string s; stack<int> st; // 用于记录循环深度 int max_depth; void process() { max_depth = 0; while (!st.empty()) st.pop(); int depth = 0; for (int i = 0; i < n; ++i) { cin >> s; if (s == "F") { string var, start, end; cin >> var >> start >> end; int s_val = stoi(start), e_val = stoi(end); if (s_val <= e_val) { depth++; st.push(e_val - s_val + 1); max_depth = max(max_depth, depth); } else { // 循环体不执行 st.push(0); depth++; } } else if (s == "E") { if (!st.empty()) { st.pop(); depth--; } } } } int main() { cin >> T; for (int t = 1; t <= T; ++t) { cin >> n; process(); cout << "Case #" << t << ": " << max_depth << endl; } return 0; } ``` ### 2. **奶酪问题(T2)** 给定一个三维空间中的奶酪块,内部有若干球形孔洞,判断是否可以从底部走到顶部。使用并查集处理连通性问题。 ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1005; int fa[maxn]; struct Sphere { long long x, y, z, r; } spheres[maxn]; int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } void unite(int x, int y) { int fx = find(x), fy = find(y); if (fx != fy) fa[fx] = fy; } long long dist2(Sphere a, Sphere b) { return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z); } bool is_connect(Sphere a, Sphere b) { return dist2(a, b) <= (a.r + b.r) * (a.r + b.r); } int main() { int T; cin >> T; while (T--) { int n; long long h; cin >> n >> h; for (int i = 0; i < n; ++i) { cin >> spheres[i].x >> spheres[i].y >> spheres[i].z >> spheres[i].r; fa[i] = i; } for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (is_connect(spheres[i], spheres[j])) { unite(i, j); } } } // 判断底部和顶部是否连通 bool ok = false; for (int i = 0; i < n; ++i) { if (spheres[i].z - spheres[i].r <= 0) { for (int j = 0; j < n; ++j) { if (spheres[j].z + spheres[j].r >= h) { if (find(i) == find(j)) { ok = true; break; } } } if (ok) break; } } cout << (ok ? "Yes" : "No") << endl; } return 0; } ``` ### 3. **小明搬家(T1)** 给出一个图,判断是否存在欧拉回路。若存在,则输出“YES”,否则输出“NO”。 ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1005; int degree[maxn]; bool visited[maxn]; vector<int> adj[maxn]; void dfs(int u) { visited[u] = true; for (int v : adj[u]) { if (!visited[v]) dfs(v); } } int main() { int T; cin >> T; while (T--) { int n, m; cin >> n >> m; memset(degree, 0, sizeof(degree)); memset(visited, false, sizeof(visited)); for (int i = 1; i <= n; ++i) adj[i].clear(); for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); degree[u]++; degree[v]++; } // 判断是否连通 dfs(1); bool connected = true; for (int i = 1; i <= n; ++i) { if (degree[i] > 0 && !visited[i]) { connected = false; break; } } // 判断是否所有点度数为偶数 bool all_even = true; for (int i = 1; i <= n; ++i) { if (degree[i] % 2 != 0) { all_even = false; break; } } if (connected && all_even) cout << "YES" << endl; else cout << "NO" << endl; } return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值