Codeforce 721C(DAG上dp)
来源:互联网 发布:阿里云邮箱登录名忘记 编辑:程序博客网 时间:2024/05/17 07:46
链接:点击打开链接
题意:N个点M条的有向无环图,问从1走到n花费时间不超过T最多能经过多少个城市
代码:
#include <vector>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;const int INF=0x3f3f3f3f;struct node{ int to,cost;};vector<node> G[5005];int n,m,T;int vis[5005];int dp[5005][5005],nex[5005][5005];void dfs(int s){ //dp[i][j]表示在第i点经过j个点到n点的最小距离 int i,j,tmp; if(vis[s]) //每个点只能进过一次,只有这样复杂度才是O(n) return; vis[s]=1; for(i=0;i<G[s].size();i++){ tmp=G[s][i].to; dfs(tmp); for(j=2;j<=n;j++){ //从后向前dp,也可以从前向后用拓扑加dp if(dp[tmp][j-1]+G[s][i].cost<dp[s][j]){ dp[s][j]=dp[tmp][j-1]+G[s][i].cost; nex[s][j]=tmp; } } }}int main(){ int i,j,u,v,w,st,ans; while(scanf("%d%d%d",&n,&m,&T)!=EOF){ for(i=1;i<=n;i++) G[i].clear(); for(i=1;i<=m;i++){ scanf("%d%d%d",&u,&v,&w); if(v==1||u==n) //因为是从1点走向n点,所以到1和出n的都没有用 continue; G[u].push_back((node){v,w}); } memset(dp,INF,sizeof(dp)); memset(vis,0,sizeof(vis)); vis[n]=1,dp[n][1]=0; dfs(1); for(i=n;;i--){ if(dp[1][i]<=T){ ans=i; break; } } printf("%d\n",ans); st=1; printf("%d",st); while(st!=n){ st=nex[st][ans--]; printf(" %d",st); } printf("\n"); } return 0;}
0 0
- Codeforce 721C(DAG上dp)
- codeforce 721C Journey (DAG上的dp)
- CF 721C DAG上的dp
- codeforces 721C DAG上dp
- Codeforces 721C. Journey(DAG上的DP)
- bzoj1571【DAG上dp】
- http://codeforces.com/contest/721/problem/C Journey(DAG上的dp)
- Codeforces Round #374 (Div. 2) 721C Journey DAG上DP
- Codeforce 792C(dp)
- UVA1347 - Tour (DAG上的DP)
- NYoj16 矩形嵌套 DAG上的Dp
- Codeforces #374(Div.2)C. Journey【DAG图上的拓扑排序+Dp】
- 嵌套矩形 DAG上的dp(深搜+dp)
- codeforce 67C - Sequence of Balls(dp)
- CodeForce 505C Mr. Kitayuta(DP+DFS)
- Codeforce 467C. George and Job(DP)
- 简单dp(codeforce #336(c)补题
- codeforce 343div2 C dp好题
- usb suspend
- Android greenDAO数据库配置教程
- vundle管理VIM插件
- git The requested URL returned error:443
- Maven Dependency设置
- Codeforce 721C(DAG上dp)
- 32位色和16位色
- 内存管理
- Android项目共享到GitHub_总结篇
- 极品五笔 造词
- C使用字符串
- uTorrent端口设置
- LINUX多路复用select,poll,epoll
- HRGN,剪裁区域