UVa1025 A Spy in the Metro(dp)
来源:互联网 发布:!号c语言中是什么意思 编辑:程序博客网 时间:2024/06/05 10:30
分析:
紫书 P267
AC代码:
#include <iostream>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <math.h>#include <vector>#include <stack>#include <queue>#include <map>#include <set>#include<list>#include <bitset>#include <climits>#include <algorithm>#define mset(a,x) memset(a,x,sizeof(a))#define gcd(a,b) __gcd(a,b)#define FIN freopen("input","r",stdin)#define FOUT freopen("output","w",stdout)typedef long long LL;const LL mod=1e9+7;const int INF=0x3f3f3f3f;const double PI=acos(-1.0);using namespace std;int a[500];// 起点发车时刻int b[500];// 终点发车时刻int Time[500];// 车站到车站之间的距离int dp[500][500];int judge[500][500][10];int main (){ int n,T,M1,Me; int cc=1; //FIN; while (scanf ("%d",&n)&&n){ mset(Time,0);mset(judge,0); scanf ("%d",&T); for (int i=1;i<n;i++) scanf ("%d",&Time[i]); scanf ("%d",&M1);// 起点 for (int i=1;i<=M1;i++) scanf ("%d",&a[i]); scanf ("%d",&Me);// 终点 for (int i=1;i<=Me;i++) scanf ("%d",&b[i]); for (int i=1;i<=M1;i++){// 起点到终点有车的车站时刻 int tt=a[i]; judge[tt][1][0]=1; for (int j=1;j<n;j++){ judge[tt+=Time[j]][j+1][0]=1; } } for (int i=1;i<=Me;i++){// 终点到起点有车的车站时刻 int tt=b[i]; judge[tt][n][1]=1; for (int j=n-1;j>=1;j--){ judge[tt+=Time[j]][j][1]=1; } } for (int i=0;i<=T;i++) for (int j=0;j<=n;j++) dp[i][j]=INF; for (int i=0;i<=n;i++) dp[i][n+1]=dp[i][0]=0; dp[0][1]=0; for (int i=1;i<=T;i++){ for (int j=1;j<=n;j++){ dp[i][j]=dp[i-1][j]+1; if(j>1&&judge[i][j][0]&&i>=Time[j-1])// 从左向右 dp[i][j]=min(dp[i][j],dp[i-Time[j-1]][j-1]); if(j<n&&judge[i][j][1]&&i>=Time[j])// 从右向左 dp[i][j]=min(dp[i][j],dp[i-Time[j]][j+1]); } } printf ("Case Number %d: ",cc++); if (dp[T][n]>=INF)printf ("impossible\n"); else printf ("%d\n",dp[T][n]); } return 0;}
阅读全文
0 0
- UVa1025 - A Spy in the Metro(DP)
- UVa1025--A Spy in the Metro DP
- UVA1025 A Spy in the Metro DP
- UVa1025 A Spy in the Metro(dp)
- UVA1025---A Spy in the Metro(简单dp)
- UVA1025 A Spy in the Metro(dp)
- Uva1025 A Spy in the Metro (dp)
- UVA1025 A Spy in the Metro —— DP
- #UVA1025#A Spy in the Metro(Dp经典)
- uva1025 A Spy in the Metro (dp)
- uva1025 A Spy in the Metro
- UVa1025 - A Spy in the Metro
- Uva1025 - A Spy in the Metro
- UVA1025 - A Spy in the Metro
- uva1025 a spy in the metro
- UVA1025 A Spy in the Metro
- uva1025 A Spy in the Metro
- A Spy in the Metro uva1025
- Logistic Regression的思考与整理
- maven--pom.xml错误--CoreException: Could not get the value for parameter compilerId
- [bzoj1650][Usaco2006 Dec][二分]跳石子
- 使用Nginx+Lua(OpenResty)开发高性能Web应用
- js 添加事件 attachEvent 和 addEventListener 的用法
- UVa1025 A Spy in the Metro(dp)
- 连续自然数和(尺取法)
- jquery按键监听(移动div)
- 《上瘾:让用户养成使用习惯的四大产品逻辑》读书笔记
- 最近搞Hadoop集群迁移踩的坑杂记
- requireJS(1)---使用requireJS的shim参数,完成jquery插件的加载
- ffmpeg开发之旅(6):详解ffmpeg命令在Android平台上的使用
- include/opencv2/core/cvstd.hpp:647: undefined reference to `cv::String::deallocate()'
- 无服务器探索之路(初级):AWS Lambda服务应用场景实践之一(四)