uva 1025 A Spy in the Metro DAG上的动态规划(固定终点)
来源:互联网 发布:北京seo外包公司 编辑:程序博客网 时间:2024/06/14 20:24
迭代实现
#include<cstdio>
#include<iostream>#include<cstring>
#include<algorithm>
#include<vector>
#define min2(a,b) a<b?a:b
using namespace std;
const int INF=99999999;
int N,T;
int g[100][100];
int v1[300][60],v2[300][60];
int dp[300][60];
void dp1(int t,int st){
if(t>T||st>N||st<1)return;
for(int tt=t;tt<=T;tt++){
if(dp[tt][st]==-1||dp[tt][st]>dp[t][st]+tt-t){
dp[tt][st]=dp[t][st]+tt-t;
if(tt!=t)dp1(tt,st);
}
if(v1[tt][st]){
if(dp[tt+g[st][st+1]][st+1]==-1||dp[tt+g[st][st+1]][st+1]>dp[t][st]+tt-t){
dp[tt+g[st][st+1]][st+1]=dp[t][st]+tt-t;
dp1(tt+g[st][st+1],st+1);
}
}
if(v2[tt][st]){
if(dp[tt+g[st][st-1]][st-1]==-1||dp[tt+g[st][st-1]][st-1]>dp[t][st]+tt-t){
dp[tt+g[st][st-1]][st-1]=dp[t][st]+tt-t;
dp1(tt+g[st][st-1],st-1);
}
}
}
}int main(){
int cnt=1;
while(cin>>N&&N){
cin>>T;
memset(g,0,sizeof(g));
for(int i=1;i<=N-1;i++){int t;cin>>t;g[i][i+1]=t;g[i+1][i]=t;}
memset(v1,0,sizeof(v1));
int M1;
cin>>M1;
for(int i=1;i<=M1;i++){
int d;
cin>>d;
for(int t=d,st=1;t<=T&&st<=N;t+=g[st][st+1],st++)v1[t][st]=1;
}
memset(v2,0,sizeof(v2));
int M2;
cin>>M2;
for(int i=1;i<=M2;i++){
int e;
cin>>e;
for(int t=e,st=N;t<=T&&st>=1;t+=g[st][st-1],st--)v2[t][st]=1;
}
memset(dp,-1,sizeof(dp));
dp[0][1]=0;
dp1(0,1);
cout<<"Case Number "<<cnt++<<": ";
if(dp[T][N]!=-1)cout<<dp[T][N]<<endl;
else cout<<"impossible"<<endl;
}
return 0;}
0 0
- uva 1025 A Spy in the Metro DAG上的动态规划(固定终点)
- 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 [动态规划] [DAGdp]
- UVA 1025 A Spy in the Metro (动态规划)
- UVa 1025 (DAG 上的动态规划,有固定终点的最短时间,逆推法)
- UVA 1025A Spy in the Metro 动态规划 _ SPFA
- 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
- vue-resource和axios的简单使用方法总结
- 高级IO操作
- 多线程编程(一)--进程和线程之由来
- TSNE动态可视化
- JVM 调优实战之优化 eclipse 启动时间
- uva 1025 A Spy in the Metro DAG上的动态规划(固定终点)
- toj-3488-stones2(优先队列)
- 如何去硅谷就业?
- 回溯法解决N皇后问题(java实现)
- 时间与延时
- toj-3515-middle number(优先队列)
- NYOJ 1274 信道安全 裸的最短路 spfa 邻接表 第九届省赛C
- Android--各种Drawable介绍
- hdu 4725 The Shortest Path in Nya Graph(最短路)