CodeForces 152E

本文深入探讨了斯坦纳树问题的求解方法,利用动态规划和最短路径算法实现了一个高效的解决方案。文中详细介绍了如何通过预处理节点间的最短距离,并结合状态压缩技巧来寻找最优的斯坦纳树。

斯坦纳树+输出方案

#include<cstdio>
#include<iostream>
#include<cstring>
const int N=210;
const int INF=1e9;
int tot;
int g[N][N];
int mp[N][N];
int val[N];
int mi[N][N];

void flody(){
    memset(mi,-1,sizeof(mi));
    for(int k=0;k<tot;k++){
        for(int i=0;i<tot;i++){
            for(int j=0;j<tot;j++){
                //g[i][j]=min(g[i][j],g[i][k]+g[k][j]-val[k]);
                if(g[i][k]+g[k][j]-val[k]<g[i][j]){
                    g[i][j]=g[i][k]+g[k][j]-val[k];
                    mi[i][j]=k;
                }
            }
        }
    }
}

int vis[N];
int s[N];
int dp[N][1<<7];
int pre[N][1<<7][2];
int n,m,K;

void stn(){

    int cnt=0;
    for(int i=0;i<tot;i++){
        if(vis[i]){
            //printf("%d %d\n",i/m+1,i%m+1);
            s[i]=1<<cnt;
            cnt++;
        }
        else s[i]=0;
    }
    memset(dp,0x3f,sizeof(dp));
    for(int i=0;i<tot;i++){
        dp[i][s[i]]=val[i];
    }
    memset(pre,-1,sizeof(pre));
    int ed=1<<cnt;
    for(int i=1;i<ed;i++){
        for(int j=0;j<tot;j++){
            for(int k=(i-1)&i;k;k=(k-1)&i){
                //dp[j][i]=min(dp[j][i],dp[j][k|s[j]]+dp[j][i^k|s[j]]-val[j]);
                int x=dp[j][k|s[j]],y=dp[j][i^k|s[j]];
                if(dp[j][i]>x+y-val[j]){
                    dp[j][i]=x+y-val[j];
                    pre[j][i][0]=j;
                    pre[j][i][1]=k|s[j];
                }
            }
        }
        for(int j=0;j<tot;j++){
            for(int j1=0;j1<tot;j1++){
                //dp[j1][i|s[j1]]=min(dp[j1][i|s[j1]],dp[j][i]+val[j1]);
                if(dp[j][i]+g[j][j1]-val[j]<dp[j1][i|s[j1]]){
                    dp[j1][i|s[j1]]=dp[j][i]+g[j][j1]-val[j];
                    pre[j1][i|s[j1]][0]=j;
                    pre[j1][i|s[j1]][1]=i;
                }
            }
        }
    }
}

int aa[N][N];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};

int check(int i,int j){
    if(i<1||i>n||j<1||j>m)return 0;
    return 1;
}

int ans[N][N];
void _find(int x,int y){
    int m1=mi[x][y];
    ans[x/m+1][x%m+1]=1;
    ans[y/m+1][y%m+1]=1;
    if(m1!=-1){
        _find(x,m1);
        _find(m1,y);
    }
}

void dfs(int id,int st){
    int x=pre[id][st][0],y=pre[id][st][1];
    ans[id/m+1][id%m+1]=1;
    if(x==-1)return ;

    if(x==id){
        dfs(x,y|s[id]);
        dfs(x,st^y|s[id]);
    }
    else {
        _find(x,id);
        dfs(x,y);
    }
}
int main(){
    #ifdef DouBi
    freopen("in.cpp","r",stdin);
    #endif // DouBi

    while(scanf("%d%d%d",&n,&m,&K)!=EOF){
        tot=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                mp[i][j]=tot;
                scanf("%d",&val[tot++]);
            }
        }

        memset(g,0x3f,sizeof(g));
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                g[mp[i][j]][mp[i][j]]=val[mp[i][j]];
            }
        }

        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                for(int k=0;k<4;k++){
                    int ii=i+dir[k][0],jj=j+dir[k][1];
                    if(check(ii,jj)){
                        g[mp[i][j]][mp[ii][jj]]=val[mp[i][j]]+val[mp[ii][jj]];
                    }
                }
            }
        }

        flody();
//        for(int i=0;i<tot;i++){
//            for(int j=0;j<tot;j++){
//                printf("%d ",g[i][j]);
//            }printf("\n");
//        }
        memset(vis,0,sizeof(vis));
        for(int i=0;i<K;i++){
            int a,b;scanf("%d%d",&a,&b);
            vis[mp[a][b]]=1;
        }
        stn();

        int ed=1<<K;
        int id=-1,ans1=INF;
        for(int i=0;i<tot;i++){
            if(dp[i][ed-1]<ans1){
                ans1=dp[i][ed-1];
                id=i;
            }
        }
        printf("%d\n",ans1);

        memset(ans,0,sizeof(ans));
        dfs(id,ed-1);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                printf("%c",ans[i][j]?'X':'.');
            }printf("\n");
        }
    }
    return 0;
}
### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值