UVa 1025 A Spy in the Metro

本文介绍了一个列车调度算法的具体实现,该算法通过动态规划方法计算出在给定时间限制内完成从起点到终点行程所需的最少列车数量。算法考虑了不同站点间的行驶时间和多个列车的到达时刻,确保列车调度的有效性和可行性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<deque>
#include<functional>
#include<iterator>
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#define CPY(A, B) memcpy(A, B, sizeof(A))
typedef long long LL;
typedef unsigned long long uLL;
const int MOD = int (1e9) + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos (-1.0);
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
int dp[210][55];
int has_train[210][55][2];
int t[210],d1[55],d2[55];
int main() {
    int n,T,m1,m2; int kase=1;
    while (~scanf ("%d",&n) &&n) {
        cin>>T;
        for (int i=1; i<n; ++i) { cin>>t[i]; }
        cin>>m1;
        for (int i=1; i<=m1; ++i) { cin>>d1[i]; }
        cin>>m2;
        for (int i=1; i<=m2; ++i) { cin>>d2[i]; }
        memset (has_train,0,sizeof (has_train) );
        for (int i=1; i<=m1; ++i) {
            has_train[d1[i]] [1][0]=1;//ith train when go from station 1
            int temp=d1[i];
            for (int j=1; j<n; j++) {
                temp+=t[j];//station one by one
                if (temp<=T) { has_train[temp][j+1][0]=1; }//when arrive station j
                else { break; }
            }
        }
        for (int i=1; i<=m2; i++) {
            has_train[d2[i]] [n][1]=1;
            int temp=d2[i];
            for (int j=n-1; j>1; j--) {
                temp+=t[j];
                if (temp<=T) { has_train[temp][j][1]=1; }
                else { break; }
            }
        }
        for (int i=1; i<=n-1; i++) { dp[T][i]=INF; }
        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]);
                }
            }
        cout<<"Case Number "<<kase++<<": ";
        if (dp[0][1]>=INF) { cout<<"impossible\n"; }
        else { cout<<dp[0][1]<<"\n"; }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值