uva 1025 A Spy in the Metro
来源:互联网 发布:红色警戒扫矿软件 编辑:程序博客网 时间:2024/05/18 01:17
题目大意:
一个人从车站1出发,在 T 时刻到达车站 n 。求最少的等待时间。
其他的都好读懂了,刚开始做的时候不知道必须要在 T 时间到达,英语不好害死人。
解题思路:
先要把题目给的条件初始化成可用的模型:
vis[i][j][0]; i时刻是否有车在 从左 到 j 站
vis[i][j][1]; i时刻是否有车在 从右 到 j 站
纯暴力刷表,没有“技巧”,两个自变量 i:时刻 j:车站;
dp[i][j] : i 时刻 在 j 车站 等待的最少时间。
想着一个递推式必须有起点,要么是从n,要么从1,这道题从n 开始走好些, 因为状态已知 dp[T][n]=0;
到达 j 站有三种方法:
1.从 j 站到 j+1 站:dp[i+t[j]][j+1] 逆推的,有点绕,因为我们是从 n 站往前走的;
2.从j 站到 j-1 站: dp[i+t[j-1][j-1] 理由同上;
3.从j站到j站:dp[i+1][j]+1; 原地不动,毕竟计算机吗,可以一分钟一分钟的加。
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;const int maxn=1000;const int inf=0x3f3f3f3f;int cas;int t[maxn];int vis[maxn][maxn][2];int dp[maxn][maxn];int T;int n;void init(){ int le,ri; int h; scanf("%d",&T); for(int i=1;i<n;i++) { scanf("%d",&t[i]); } memset(vis,0,sizeof(vis)); scanf("%d",&le); for(int i=0;i<le;i++) { scanf("%d",&h); vis[h][1][0]=1; for(int j=1;j<n;j++) { h+=t[j]; vis[h][j+1][0]=1; } } scanf("%d",&ri); for(int i=0;i<ri;i++) { scanf("%d",&h); vis[h][n][1]=1; for(int j=n-1;j>=1;j--) { h+=t[j]; vis[h][j][1]=1; // cout<<h<<" "<<j<<endl; } } //cout<<"ddd"<<endl;}void solve(){ memset(dp,0x3f,sizeof(dp)); dp[T][n]=0; int i,j; for(i=T-1;i>=0;i--) { for(j=1;j<=n;j++) { dp[i][j]=dp[i+1][j]+1; if(vis[i][j][0] && j<n) { if(i+t[j]<=T) dp[i][j]=min(dp[i][j],dp[i+t[j]][j+1]); } if(vis[i][j][1] && j>1) { if(i+t[j-1]<=T) dp[i][j]=min(dp[i][j],dp[i+t[j-1]][j-1]); } } } printf("Case Number %d: ",cas++); if(dp[0][1]<inf) { printf("%d\n",dp[0][1]); } else { printf("impossible\n"); }}int main(){ cas=1; while(scanf("%d",&n),n) { init(); solve(); } return 0;}
0 0
- 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
- 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
- A Spy in the Metro UVA
- UVA A Spy in the Metro
- A Spy in the Metro UVA
- A Spy in the Metro UVA
- UVA 1025 - A Spy in the Metro(动态规划)
- DirectX11 指定材质
- Image Map
- getline()~~
- pageDateTime(网页日期时间)
- 本地mapReduce项目报错:java.io.IOException: Unable to establish loopback connection;
- uva 1025 A Spy in the Metro
- DirectX11 平行光
- HDU 2899 Strange fuction(二分)
- JSP之Cookie的实现
- 条款13:关于文中多次提及的auto_ptr和shared_ptr
- Java设置环境变量
- spark 日志分析
- LeetCode 065 Valid Number
- 自适应网页开发