CodeForces 721C Journey (DFS来模拟DP顺序)

题目链接:http://codeforces.com/problemset/problem/721/C

DP+DFS

#include
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=5e3+10;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){
    if(y==0) return x;
    return gcd(y,x%y);
}
int n,m,k,x,y,z;
vector g[maxn];
int dp[maxn][maxn],nxt[maxn][maxn];
void dfs(int u,int pre,int t,int v){
    if(v<0) return ;
    if(dp[u][t]>=v) return ;
    dp[u][t]=v,nxt[u][t]=pre;
    rep(i,0,g[u].size()){
        int tv=v-g[u][i].se;
        dfs(g[u][i].fi,u,t+1,tv);
    }
}
void output(int x,int y){
    if(y==0) return ;
    output(nxt[x][y],y-1);
    cout<>n>>m>>k;
    rep(i,0,m){
        cin>>x>>y>>z;
        g[x].push_back(mk(y,z));
    }
    mst(dp,-1);///mst(nxt,-1);
    dfs(1,-1,1,k);
    int i=n;while(i>=1&&dp[n][i]==-1) i--;
    cout<

 

 

你可能感兴趣的:(Codeforces习题集,搜索(DFS+BFS))