CodeForces - 721C Journey 动态规划

一开始做这道题的时候,我只推出了状态转移方程,即:dp[v][j]=min(dp[v][j],dp[u][j-1]+time[u][v])

但循环写错了,路径也不会保存QAQ...看了大佬的代码,豁然开朗Orz

dp[i][j]:到达i点,经过j个城市的最小时间

pre[i][j]:到达i点,经过j个城市的前一个城市

然后模仿人家写的交了一发,结果显示"Memory limit exceeded on test 1"如下:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<utility>
using namespace std;

const int MAX=5005;
int n,m,t;
vector<int>a[MAX];
int time[MAX][MAX];
int dp[MAX][MAX];//到达i点,经过j个城市的最小时间
int pre[MAX][MAX];//到达i点,经过j个城市的前驱结点
int ans[MAX];//答案数组
int main()
{
    while(~scanf("%d%d%d",&n,&m,&t))
    {
        int u,v,w;
        for(int i=0;i<=n;i++)
            a[i].clear();
        memset(time,0,sizeof(time));
        memset(pre,0,sizeof(pre));
        memset(ans,0,sizeof(ans));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            a[u].push_back(v);
            time[u][v]=w;
        }
        memset(dp,INF,sizeof(dp));
        dp[1][1]=0;
        for(int j=2;j<=n;j++)//已经经过的顶点数
        {
            //cout<<"j="<<j<<endl;
            for(int u=1;u<=n;u++)//起点
            {
                //cout<<" u="<<u<<" dp[u][j-1]="<<dp[u][j-1]<<endl;
                for(int k=0;k<a[u].size();k++)
                {
                    int v=a[u][k];//终点
                    //cout<<"  v="<<v<<" dp="<<dp[v][j]<<endl;
                    if(dp[u][j-1]+time[u][v]<dp[v][j])
                    {//dp[v][j]=min(dp[v][j],dp[u][j-1]+time[u][v]);
                        dp[v][j]=dp[u][j-1]+time[u][v];
                        pre[v][j]=u;
                    }
                    //cout<<"   dp="<<dp[v][j]<<endl;
                }
            }
            if(dp[n][j]<=t)
            {
                //cout<<"j="<<j<<" t="<<dp[n][j]<<endl;
                ans[0]=j;//记录个数
            }
        }
        printf("%d\n",ans[0]);
        for(int i=ans[0];i>=1;i--)
        {
            ans[i]=n;
            n=pre[n][i];
        }
        for(int i=1;i<ans[0];i++)
            printf("%d ",ans[i]);
        printf("%d\n",ans[ans[0]]);
    }
	return 0;
}

后来再看了大佬的代码,发现是用pair来存储的,改了下然后就过了emmmmm...看来以后能用STL就尽量用吧。。。

附上AC代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<utility>
using namespace std;
typedef pair<int,int>pp;

const int MAX=5001;
int n,m,t;
vector<pp>a[MAX];
//int time[MAX][MAX];
int dp[MAX][MAX];//到达i点,经过j个城市的最小时间
int pre[MAX][MAX];//到达i点,经过j个城市的前驱结点
int main()
{
    int i,j,k,u,v,w;
    while(~scanf("%d%d%d",&n,&m,&t))
    {
        int *ans=new int[n+5];//答案数组
        for(i=0;i<=n;i++)
        {
            a[i].clear();
            for(int j=0;j<=n;j++)
                dp[i][j]=t+1;
        }
        //memset(time,0,sizeof(time));
        memset(pre,0,sizeof(pre));
        memset(ans,0,sizeof(ans));
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            a[u].push_back(make_pair(v,w));
            //time[u][v]=w;
        }
        dp[1][1]=0;
        for(j=2;j<=n;j++)//已经经过的顶点数
        {
            for(u=1;u<=n;u++)//起点
            {
                for(k=0;k<a[u].size();k++)
                {
                    //int v=a[u][k].first;//终点
                    if(dp[u][j-1]+a[u][k].second<dp[a[u][k].first][j])
                    {//dp[v][j]=min(dp[v][j],dp[u][j-1]+time[u][v]);
                        dp[a[u][k].first][j]=dp[u][j-1]+a[u][k].second;
                        pre[a[u][k].first][j]=u;
                    }
                }
            }
            if(dp[n][j]<=t)
            {
                ans[0]=j;//记录个数
            }
        }
        printf("%d\n",ans[0]);
        for(i=ans[0];i>=1;i--)
        {
            ans[i]=n;
            n=pre[n][i];
        }
        for(i=1;i<ans[0];i++)
            printf("%d ",ans[i]);
        printf("%d\n",ans[ans[0]]);
        delete []ans;
    }
	return 0;
}

动规这块我还是太菜了,还是得多刷题啊55555.....

### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值