【dfs+dp】Codeforces 721C Journey

题目链接:http://codeforces.com/problemset/problem/721/C
题意:有n个点,给定m条路径,每条路径的时间为t,总时间为T,问从点1到达点n用时不得超出T,所经过的点最多,且将经过的点输出!

解题思路:定义一个dp[i][j] ,表示到达i点时所经过j个点所花费的时间为最优 此数组用于dfs中剪枝 ,初始化大值! 存放路径和时间需用vector,因为这样可以获得每个点可以到达它所能到达的点的个数,用于dfs中搜索而返回值类型为bool是为了记录此状态是否可取,可取保存路径.

参考:点击打开链接

#include 
#define Fi first
#define Se second
using namespace std;
const int N=5e3+10;
int dp[N][N],path[N],flag,ans;
int _n,_m,_k;
vector >G[N];
bool dfs(int u,int k,int c)
{
    if(dp[u][k]<=c) return 0;
    dp[u][k]=c;
    if(k>ans&&u==_n&&c<=_k) return ans=k;
    int flag=0;
    for(int i=0;i

你可能感兴趣的:(【dfs+dp】Codeforces 721C Journey)