CF 793D Presents in Bankopolis

该博客介绍了Codeforces的一道题目,即在一个有序的有向图中寻找长度为k的最短路径,路径选择受到已访问节点的限制。通过动态规划(dp)解决此问题,考虑起点范围[1, n],每经过一个节点,下一个可到达节点的范围会变化。博主发现使用记忆化搜索会导致超时,因此改为递推方式实现,但调整过程遇到了困难。博主强调,以后类似问题应优先考虑递推解决方案。" 114299170,9905981,MySQL:如何不选择特定列获取所有数据,"['mysql', 'sql', '数据库管理']
摘要由CSDN通过智能技术生成

D. Presents in Bankopolis

一个有向图,节点“有序”,找一条长度为k的最短路径,额外要求是当前边不能“跃过”已经经过的节点。
起点的选择范围是[1,n]然后每经过一个点,下一个能到达的点的范围就会缩小,注意到总可以用一个连续的范围表示之,记为[l,r]。那么用dp解决,状态dp[l][r][k]表示可达范围是[l,r],已经走了k步。
貌似dp写成记忆化搜索更符合我的思维,但是这题写记忆化会T。。改递推还改了半天。。
以后能写递推的题,我再写记忆化我就是SB!!!

#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int INF = 1e9;

int n,k;
int m;
const int Max = 88;

int e[Max][Max];

int dp[Max][Max][Max][2];

inline void ckMin(int &x,int y){
    if(x>y){
        x=y;
    }
}

int main(){
    cin>>n>>k;
    cin>>m;

    for(int i=0;i<Max;i++){
        for(int j=i;j<Max;j++){
            for(int kk=0;kk<Max;kk++){
                if(kk==k){
                    dp[i][j][kk][0] = dp[i][j][kk][1] = 0;
                }else{
                    dp[i][j][kk][0] = dp[i][j][kk][1] = INF;
                }
            }
        }
    }

    for(int i=1;i<=m;i++){
        int u,v,c;
        cin>>u>>v>>c;
        if(e[u][v] == 0){
            e[u][v] = c;
        }else{
            e[u][v] = min(e[u][v],c);
        }
    }

    if(k>n){
        cout<<-1<<endl;
        return 0;
    }

    for(int kk=k;kk>0;kk--){
        for(int l=1;l<=n;l++){
            for(int r=l;r<=n;r++){
                for(int pre=1;pre<l;pre++){
                    if(e[pre][l]){
                        ckMin(dp[pre][r][kk-1][1],dp[l][r][kk][1]+e[pre][l]);
                    }
                    if(e[pre][r]){
                        ckMin(dp[pre][r][kk-1][1],dp[l][r][kk][0]+e[pre][r]);
                    }

                }
                for(int pre=r+1;pre<=n;pre++){
                    if(e[pre][r]){
                        ckMin(dp[l][pre][kk-1][0],dp[l][r][kk][0]+e[pre][r]);
                    }
                    if(e[pre][l]){
                        ckMin(dp[l][pre][kk-1][0],dp[l][r][kk][1]+e[pre][l]);
                    }
                }
            }
        }
    }

    int ans = INF;
    for(int l=1;l<=n;l++){
        for(int r=l;r<=n;r++){
            ckMin(ans,dp[l][r][1][0]);
            ckMin(ans,dp[l][r][1][1]);
        }
    }

    if(ans<INF){
        cout<<ans<<endl;
    }else{
        cout<<-1<<endl;
    }
    return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure! For reading and processing the CSV file, we can use the `csv` module in Python. Here's the code to read and collect all the scores in a list: ```python import csv scores = [] with open('midterm.csv', newline='') as csvfile: reader = csv.reader(csvfile) next(reader) # skip header row for row in reader: scores.append(int(row[1])) print(scores) ``` This code will create a list `scores` containing all the numeric scores from the CSV file. For the second part of the question, we need to classify the scores into A, B, C, D, and F categories. According to the criteria given in Problem Set 1, we can use the following classification: - A: score >= 90 - B: 80 <= score < 90 - C: 70 <= score < 80 - D: 60 <= score < 70 - F: score < 60 Here's the code to classify the scores and count the number of scores in each category: ```python counts = {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'F': 0} for score in scores: if score >= 90: counts['A'] += 1 elif score >= 80: counts['B'] += 1 elif score >= 70: counts['C'] += 1 elif score >= 60: counts['D'] += 1 else: counts['F'] += 1 print(counts) ``` This code will create a dictionary `counts` containing the number of scores in each category. Finally, we can use the `plotly` library to create a histogram of the scores. Here's the complete code: ```python import csv import plotly.graph_objs as go scores = [] with open('midterm.csv', newline='') as csvfile: reader = csv.reader(csvfile) next(reader) # skip header row for row in reader: scores.append(int(row[1])) counts = {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'F': 0} for score in scores: if score >= 90: counts['A'] += 1 elif score >= 80: counts['B'] += 1 elif score >= 70: counts['C'] += 1 elif score >= 60: counts['D'] += 1 else: counts['F'] += 1 labels = ['A', 'B', 'C', 'D', 'F'] values = [counts[label] for label in labels] fig = go.Figure([go.Bar(x=labels, y=values)]) fig.show() ``` This code will create a histogram of the scores, showing the number of scores in each category.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值