# Codeforces - 721c journey dynamic planning

At the beginning of this problem, I only introduced the equation of state transition, that is: dp[v][j]=min(dp[v][j],dp[u][j-1]+time[u][v])

But the loop is wrong, and the path will not save QAQ... After reading the big guy's code, it suddenly becomes bright Orz

dp[i][j]: the minimum time to reach point I and pass through j cities

pre[i][j]: arrive at point I and pass through the previous city of j cities

After that, I gave a round on imitating what others had written. The result shows that "Memory limit exceeded on test 1" is as follows:

```#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];//The minimum time to reach point i and pass through j cities
int pre[MAX][MAX];//Arrive at point i and pass through the precursor nodes of j cities
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++)//Number of vertices that have passed
{
//cout<<"j="<<j<<endl;
for(int u=1;u<=n;u++)//Starting point
{
//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];//End
//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;//Number of records
}
}
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;
}```

Later, I looked at the big guy's code and found that it was stored in pair. I changed it, and then emmmmm passed. It seems that I will try my best to use STL later...

Attach AC Code:

```#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];//The minimum time to reach point i and pass through j cities
int pre[MAX][MAX];//Arrive at point i and pass through the precursor nodes of j cities
int main()
{
int i,j,k,u,v,w;
while(~scanf("%d%d%d",&n,&m,&t))
{
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++)//Number of vertices that have passed
{
for(u=1;u<=n;u++)//Starting point
{
for(k=0;k<a[u].size();k++)
{
//int v=a[u][k].first; / / end point
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;//Number of records
}
}
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;
}```

I'm still too busy with moving rules. I have to brush more questions. 55555

Posted by kevinak on Thu, 09 Jan 2020 09:47:43 -0800