1025 - A Spy in the Metro
来源:互联网 发布:网络粪坑 编辑:程序博客网 时间:2024/06/05 08:25
经典dp
时间单向流动,是一个天然的序列,影响到决策的只有时间和所处的车站,所以用dp[i][j]表示在i时刻你在j车站,最少还需要等待多长时间。
状态就是时间和车站序号,状态转移方程就是 1、如果在该时刻该车站没有列车,等待时间+1;2、如果有向右开的列车dp[i][j] = min(dp[i][j],dp[i+t[j]][j+1]); 有两个选择,不乘坐或者乘坐;3、与2相似
#include<bits/stdc++.h>using namespace std;const long long INF = 1000000;int dp[205][55];int N,T,t[100],M1,M2,kase = 0;bool has_train[300][100][5];int main() { while(scanf("%d",&N)!=EOF&&N) { memset(dp,0,sizeof(dp)); memset(t,0,sizeof(t)); scanf("%d",&T); for(int i=1;i<=N-1;i++) { scanf("%d",&t[i]); dp[T][i] = INF; } scanf("%d",&M1); int tt; memset(has_train,false,sizeof(has_train)); for(int i=0;i<M1;i++) { scanf("%d",&tt); for(int j=1;j<=N-1;j++) { has_train[tt][j][0] = true; tt+=t[j]; } } scanf("%d",&M2); for(int i=0;i<M2;i++) { scanf("%d",&tt); for(int j=N-1;j>=1;j--) { has_train[tt][j+1][1] = true; tt+=t[j]; } } dp[T][N] = 0; for(int i=T-1;i>=0;i--) for(int j = 1;j <= N;j++) { dp[i][j] = dp[i+1][j] + 1; if(j<N&&has_train[i][j][0]&&i+t[j] <= T) dp[i][j] = min(dp[i][j],dp[i+t[j]][j+1]); if(j>1&&has_train[i][j][1]&&i+t[j-1]<=T) dp[i][j] = min(dp[i][j],dp[i+t[j-1]][j-1]); } printf("Case Number %d: ",++kase); if(dp[0][1] >= INF) printf("impossible\n"); else printf("%d\n",dp[0][1]); } return 0;}
1 0
- 1025-A Spy in the Metro
- UVA 1025 A Spy in the Metro
- 1025 - A Spy in the Metro
- 1025 - A Spy in the Metro
- UVa 1025 A Spy in the Metro
- Uva 1025 A Spy in the Metro
- UVa - 1025 - A Spy in the Metro
- 1025 - A Spy in the Metro
- UVA 1025 A Spy in the Metro
- uva 1025 - A Spy in the Metro
- uva 1025 A Spy in the Metro
- UVa 1025 - A Spy in the Metro
- UVA 1025 A Spy in the Metro
- uva 1025 A Spy in the Metro
- UVa 1025 A Spy in the Metro
- 1025 - A Spy in the Metro
- UVA - 1025 A Spy in the Metro
- 1025 A Spy in the metro 【DP】
- 《C语言及程序设计》实践参考——算工资
- 《C语言及程序设计》实践参考——成绩统计
- 通过VMWare将CentOS安装在U盘
- 使用哈希加盐法来为密码加密
- Mysql左连接去除重复数据
- 1025 - A Spy in the Metro
- 内核panic后使用GDB调试
- 【C语言探索之旅】 第二部分第七课:文件读写
- centos6.5 系统内核版本获取
- 网络字节序和主机字节序相互转换函数
- Activity的四种launchMode
- js常用正则
- C++/C字符串格式化
- 第三章第三题