• UVA——A Spy in the Metro(线性dp)


    A Spy in the Metro

    题意:

    (n)个站台,从第(i)个站台到第(i+1)个站台的所需时间为(t[i])
    站台1出发的车辆有(m1)辆,出发时间为(d1[i])
    站台n出发的车辆有(m2)辆,出发时间为(d2[i])
    给出时间(T)
    求在时间(T)里到达站台(n)所需要的最少等待时间

    思路:

    (dp[i][j])表示在时间为(i)时到达站台(j)所需要的最少等待时间
    有三种选择:
    在站台等待一分钟,向左走,向右走。
    注意处理细节。

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<ll, ll>PLL;
    typedef pair<int, int>PII;
    typedef pair<double, double>PDD;
    #define I_int ll
    inline ll read()
    {
        ll x = 0, f = 1;
        char ch = getchar();
        while(ch < '0' || ch > '9')
        {
            if(ch == '-')f = -1;
            ch = getchar();
        }
        while(ch >= '0' && ch <= '9')
        {
            x = x * 10 + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    #define read read()
    #define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
    #define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i<(b);i++)
    #define per(i,a,b) for(int i=(a);i>=(b);i--)
    #define perr(i,a,b) for(int i=(a);i>(b);i--)
    ll ksm(ll a, ll b, ll p)
    {
        ll res = 1;
        while(b)
        {
            if(b & 1)res = res * a % p;
            a = a * a % p;
            b >>= 1;
        }
        return res;
    }
    const int inf = 0x3f3f3f3f;
    #define PI acos(-1)
    const double eps = 1e-8;
    const int maxn =1e6+7;
    const ll mod=998244353;
    const int N = 5e3 + 7;
    int n,T,t[55],m1,d1[55],m2,d2[55];
    int dp[210][55];
    int vis[210][55][2],Case=1;
    int main() {
    	while(scanf("%d",&n)!=EOF){
    		if(!n) break;
    		memset(dp,0,sizeof dp);
    		T=read;
    		for(int i=1;i<n;i++){
    			t[i]=read;
    			dp[T][i]=inf;
    		}
    		m1=read;
    		for(int i=1;i<=m1;i++) d1[i]=read;
    		m2=read;
    		for(int i=1;i<=m2;i++) d2[i]=read;
    		memset(vis,0,sizeof vis);
    		for(int i=1;i<=m1;i++){
    			int time=d1[i];
    			for(int j=1;j<=n-1;j++){
    				vis[time][j][0]=1;
    				time+=t[j];
    			}
    		}
    		for(int i=1;i<=m2;i++){
    			int time=d2[i];
    			for(int j=n-1;j;j--){
    				vis[time][j+1][1]=1;
    				///cout<<time<<"************"<<j<<endl;
    				time+=t[j];
    			}
    		}
    		for(int i=T-1;i>=0;i--){
    			for(int j=1;j<=n;j++){
    				dp[i][j]=dp[i+1][j]+1;
    				if(i+t[j]<=T&&j+1<=n&&vis[i][j][0])
                        dp[i][j]=min(dp[i][j],dp[i+t[j]][j+1]);///0
    				if(i+t[j-1]<=T&&j-1>=1&&vis[i][j][1])
                        dp[i][j]=min(dp[i][j],dp[i+t[j-1]][j-1]);///1
    			}
    		}
    		cout<<"Case Number "<<Case++<<": ";
    		if(dp[0][1]>inf/2) cout<<"impossible"<<endl;
            else   cout<<dp[0][1]<<endl;
    	}
    	return 0;
    }
    
    
    
    
    
    
    
    
    
    
  • 相关阅读:
    好吧,CSS3 3D transform变换,不过如此!
    Webpack基本使用(详解)
    「万字整理 」这里有一份Node.js入门指南和实践,请注意查收 ❤️
    怎么解决禅道启动服务mysqlzt时的端口失败
    怎么删掉xampp文件夹
    禅道Bug管理工具环境搭建
    svn报错:“Previous operation has not finished; run 'cleanup' if it was interrupted“ 的解决方法
    Fiddler教程
    各种数据库默认端口总结
    百度网盘破解版下载
  • 原文地址:https://www.cnblogs.com/OvOq/p/14787183.html
Copyright © 2020-2023  润新知