标签:journey path include namespace can round code const ++
题解:
dfs+dp
dp[i][j]表示经过i个点后到达j点的最小花费
在dfs每个点的过程中加个for循环i
注意用 long long MLE
代码:
#include<bits/stdc++.h> #define maxn 5010 #define mod 1000000007 #define ll long long #define pb push_back using namespace std; const int INF=1e9+7; struct Edge { int v,nxt,t; }edge[maxn]; int head[maxn],pre[maxn][maxn],vis[maxn]; int dp[maxn][maxn]; int cnt,sum,n,m,T; void init() { memset(head,-1,sizeof(head)); cnt=0; for(int i=0;i<maxn;i++) for(int j=0;j<maxn;j++) dp[i][j]=INF; memset(pre,-1,sizeof(pre)); } void addedge(int u,int v,int t) { edge[cnt].v=v; edge[cnt].t=t; edge[cnt].nxt=head[u]; head[u]=cnt++; } void dfs(int u) { if(vis[u]) return; vis[u]=1; for(int i=head[u];i!=-1;i=edge[i].nxt) { int v=edge[i].v,t=edge[i].t; dfs(v); for(int i=2;i<=n;i++) { ll dis=dp[v][i-1]+t; if(dis<dp[u][i]) { dp[u][i]=dis; pre[u][i]=v; } } } } void print_path() { for(int i=n;i>=0;i--) { if(dp[1][i]<=T) { printf("%d\n",i); int x=1; printf("%d ",x); while(i>1) { x=pre[x][i--]; printf("%d ",x); } break; } } } int main() { init(); scanf("%d%d%d",&n,&m,&T); for(int i=1;i<=m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); addedge(x,y,z); } dp[n][1]=0; vis[n]=1; dfs(1); print_path(); }
Codeforces Round #374 (Div. 2) C. Journey
标签:journey path include namespace can round code const ++
原文地址:http://www.cnblogs.com/byene/p/5988165.html