[Explanations] NOIP2017 the park (DP)

[Explanations] NOIP2017 the park (DP)

The first post hung up 27 points ... I will not defeat the ...

Consider such an approach, set \ (D_i \) represents from the node to the node n of the shortest path , \ (DP (I, K) \) represents the \ (I \) node to the \ (n \) to walk up \ (K \) number scheme distance. Transfer equivalent to enumerate which ones edges, if the change of state is much to go this side will be more than the shortest.

Transfer equation
\ [dp (i, k)
= \ sum _ {(i, u, w) \ in E} dp (u, k- (w- (d_i-d_u)) \] direct transfer using dfs (determination remember ring) can be.

...

...

...

But we can not be so perfunctory, what exactly is the transfer order?

Can be understood: the shortest reverse run, a new drawing can be built \ (G '= (V, E) \) wherein, \ (E \) a subset of the original edge, and for the edge \ ((U , v) \) if and only if \ (d_u \ ge d_v \) there is (d is the shortest reverse array). The new plan, if not no solution DAG / infinite number of solutions. So now I guarantee a DAG, so after topological sorting can be transferred. (There is one) is the topological sort order DFS backtracking.

Time complexity \ (O (T (n-MK + NK + \ log m)) \) . Legitimate \ (0 \) more to the side of the can top this complexity.

//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>

using namespace std;  typedef long long ll;
inline int qr(){
      register int ret=0,f=0;
      register char c=getchar();
      while(c<48||c>57)f|=c==45,c=getchar();
      while(c>=48&&c<=57) ret=ret*10+c-48,c=getchar();
      return f?-ret:ret;
}
const int maxn=1e5+5;

template<class M>
struct HEAP{
      M data[maxn*2];
      int cnt;
      inline void down(const int&pos){
        for(int t=pos,k;(t<<1)<=cnt;t=k){
          k=t<<1;
          if(k<cnt&&data[k|1]<data[k]) k|=1;
          if(data[t]>data[k]) swap(data[t],data[k]);
          else return;
        }
      }
      inline void up(const int&pos){
        for(int t=pos;t>>1;t>>=1)
          if(data[t]<data[t>>1]) swap(data[t],data[t>>1]);
          else return;
      }
      inline void push(const M&x){data[++cnt]=x,up(cnt);}
      inline void pop(){swap(data[1],data[cnt--]);down(1);}
      inline M top(){return data[1];}
      inline int size(){return cnt;}
};
HEAP< pair<int,int> > q;

struct E{
      int to,nx,w;
      E(){to=nx=w=0;}
      E(const int&x,const int&y,const int&z){to=x; nx=y; w=z;}
}e[maxn<<2];
int head[maxn],cnt,head0[maxn];
inline void add(const int&fr,const int&to,const int&w,int*h=head){e[++cnt]=E(to,h[fr],w),h[fr]=cnt;}
int d[maxn],n,m,k,mod;
typedef pair<int,int> P;

const int inf=1e9;
inline void dij(){
      for(int t=1;t<=n;++t) d[t]=inf;
      q.push((P){d[n]=0,n});
      while(q.size()){
        P now=q.top(); q.pop();
        if(now.first>d[now.second]) continue;
        for(int t=head[now.second];t;t=e[t].nx)
          if(d[e[t].to]>d[now.second]+e[t].w)
            q.push((P){d[e[t].to]=d[now.second]+e[t].w,e[t].to});
      }
}


int dp[55][maxn];
bool usd[55][maxn];
bool in[55][maxn];
int dfs(const int&now,const int&k){
      if(in[k][now])return -1;
      if(usd[k][now]) return dp[k][now];
      dp[k][now]=now==n;
      in[k][now]=usd[k][now]=1;
      for(int t=head0[now];t;t=e[t].nx){
        int g=e[t].w-(d[now]-d[e[t].to]),ret;
        if(g>k)continue;
        if(ret=dfs(e[t].to,k-g),-1==ret) return dp[k][now]=-1;
        dp[k][now]=(dp[k][now]+ret)%mod;
      }
      in[k][now]=0;
      return dp[k][now];
}

int main(){
      int T=qr();
      while(T--){
        cnt=0;
        n=qr(); m=qr(); k=qr(); mod=qr();
        for(register int t=0;t<=n;++t) head[t]=head0[t]=0;
        for(int i=0;i<=k;++i)
          for(register int t=0;t<=n;++t)
            dp[i][t]=usd[i][t]=in[i][t]=0;
        for(int t=1,t1,t2,t3;t<=m;++t)
          t1=qr(),t2=qr(),t3=qr(),add(t2,t1,t3),add(t1,t2,t3,head0);
        dij();
        //for(int t=1;t<=n;++t) printf("%d\n",d[t]);
        printf("%d\n",dfs(1,k));
      }
      return 0;
}

Guess you like

Origin www.cnblogs.com/winlere/p/11614775.html