Example-09-01

#define _CRT_SECURE_NO_WARNINGS 

#include <cstdio>
#include <cstring>

int min(int a, int b)
{
    if (a < b) return a;
    else return b;
}

int main()
{
    int N; // 2 <= N <= 50
    int t[55];
    int T; // 0 <= T <= 200
    int M1, M2; // 1 <= M1, M2 <= 50
    int d[55], e[55];

    int have_train[205][55][2]; // [time][station][direction]
                                // time <= 200
                                // sation <= 50
                                // direction == 0 left, == 1 right

    int kase = 1;

    while (scanf("%d", &N) && N) {
        scanf("%d", &T);
        for (int i = 1; i < N; i++)
            scanf("%d", &t[i]); // t[i] is the time between station i and station i+1

        memset(have_train, 0, sizeof(have_train));

        scanf("%d", &M1);

        for (int i = 0; i < M1; i++){ // for each train
            int start;
            scanf("%d", &start);
            have_train[start][1][0] = 1; // station 1
            for (int j = 1; j < N; j++) { // station 2, 3, 4..., N; j+1 is the station number
                // start + t[j] is the time the train arrives at station j+1, also is start time + time from station 1 to station j+1
                // t[j] is the time between station j and station j+1
                if (start + t[j] <= T) {
                    have_train[start + t[j]][j + 1][0] = 1;
                }
                start += t[j];
            }
        }

        scanf("%d", &M2);
        for (int i = 0; i < M2; i++){ // for each train
            int start;
            scanf("%d", &start);
            have_train[start][N][1] = 1; // station N
            for (int j = N-1; j >= 1; j--) { // station N-1, N-2, ..., 1; j is the station number
                // start + t[j] is the time the train arrives at station j, also is start time + time from station N to station j
                // t[j] is the time between station j and station j+1
                if (start + t[j] <= T) {
                    have_train[start + t[j]][j][1] = 1;
                }
                start += t[j];
            }
        }

        #define INF 10000000
        int counts = 0;
        int dp[205][205];

        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 && i + t[j] <= T && have_train[i][j][0]){
                    dp[i][j] = min(dp[i][j], dp[i+t[j]][j+1]); // t[j] is the time between station j and station j+1
                }
                if (j > 1 && i + t[j-1] <= T && have_train[i][j][1]){
                    dp[i][j] = min(dp[i][j], dp[i+t[j-1]][j-1]); // t[j-1] is the time between station j-1 and station j
                }
            }
        }

        if (dp[0][1] >= INF)
            printf("Case Number %d: impossible\n", ++counts);
        else
            printf("Case Number %d: %d\n", ++counts, dp[0][1]);

    }

    return 0;
}
时间: 2024-08-01 11:21:56

Example-09-01的相关文章

【16.09.01】【三零技术】最新更新文章推荐

1.[PHP编程:PHP简单无限分类类代码] 简介:这里就不多解释原理了,直接发代码.PS:这里代码是不能直接使用的,必须结合我的一些其他库类.应该说思想才是最重要的,这里主要提供一种分类的思路.... 地址:http://www.q3060.com/list3/list117/34130.html 2.[PHP编程:PHP简单的采集程序] 简介:    (修改了下,增加了数据缓存功能..汗,没有使用lite_cache了,自己写了个最简单的那种..)                      

archlinux2015.09.01基本系统安装

下载镜像 http://mirrors.ustc.edu.cn/archlinux/iso/2015.09.01/archlinux-2015.09.01-dual.iso 把ISO镜像写入U盘,我用的是rawrite32 笔记本的secureboot嫌麻烦可以先关掉,但是arch iso 可以在secureboot下启动,wiki上有: https://wiki.archlinux.org/index.php/Unified_Extensible_Firmware_Interface#Secu

调试大叔V1.0.1(2017.09.01)|http/s接口调试、数据分析程序员辅助开发神器

2017.09.01 - 调试大叔 V1.0.1*支持http/https协议的get/post调试与反馈:*可保存请求协议的记录:*内置一批动态参数,可应用于URL.页头.参数:*可自由管理cookie:*支持请求的代理与模拟环境参数设置:*时间戳管理(取网络当前时间戳.转成日期.转成倒计时):*支持拖放文件到本程序窗计算文件的MD5.SHA1.CRC32值:*集成常见编码解码(Ansi与Utf8互转.URL加解密.UniCode转中文);*集成常用符号与其UniCode值:*集成常用数学计算

Bentely RAM Connection V8i 09.01.00.94 1CD钢结构连接设计软件

Bentely RAM Connection V8i 09.01.00.94 1CD钢结构连接设计软件RAM Connection 几乎可为所有的连接类型提供全面的分析和设计,包括繁琐的抗震规范设计.通过与三维设计和详图绘制模 型相集成且利用程序定制功能可以实现工作流程的优化. PLS CADD v9.20 1CD Schlumberger Omega 2700 Linux 2DVD Ansoft Maxwell v14.02 Update Only 1CD Ansoft Simplorer v

TimePickerDialog时间选择器,选择0-9分钟时,显示为10:0 9:1的样式,要改成 10:00 09:01的样式 的解决方式

1 /** 2 * 设置时间选择器 3 */ 4 private void setTimePickerDialog() { 5 Calendar mCalendar = Calendar.getInstance(); 6 mCalendar.setTimeInMillis(System.currentTimeMillis()); 7 final int hour = mCalendar.get(Calendar.HOUR_OF_DAY); 8 int minute = mCalendar.get

easyUI笔记09.01

layout的布局可以自适应 <body class="easyui-layout"> <div data-options="region:'north',title:'North Title',split:true" style="height:100px;"></div> <div data-options="region:'south',title:'South Title',split:

分页存储过程,转载;原文http://www.cnblogs.com/chenqiang001/archive/2009/09/01/1558077.html

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->CREATE PROCEDURE proc_pagination(@tblName nvarchar(1000), ----要显示的表或多个表的连接,必须参数@fldName nvarchar(4000) = '*', ----要显示的字段列表@fldSort nvarchar(4000) = null

2014/09/01

周一早上是不是起床最困难的时候呢?反正这次是,眼皮相当的酸疼,挣扎着煮了点牛奶,啃了几片饼干,说好的新计划呢?一定要早睡早起!昨晚上安装了无线路由器,配置了好久才能上网,拖了不少时间导致没有早睡,当然这不是借口,主要原因是看了一会电影,上班后很少看电影和电视剧了,好不容易下载到<老男孩之猛龙过江>,里面有红到烂的小苹果,总体感觉居然是不错,现在接受度真是越来越高了. 接下来的这一个月将会很有盼头,工作五天放中秋三天,工作四天双休然后领工资,然后两周后国庆放七天O(∩_∩)O 说完废话该上班了,

C++了解free和delete(转自:http://www.cnblogs.com/mrye/archive/2012/09/01/2667079.html)

void MyMethod1() {     using namespace std;     int a=6;     int b=6;     int* pa=new int;     int* pb=new int;     *pa=a;     pb=pa;     cout<<"pa的内容赋值为:"<<a<<endl;     delete(pa);     //free(pa);//加上这句造成pa不可用,     //cout<&

Cheatsheet: 2016 09.01 ~ 09.30

Web Is JavaScript Single-Threaded? Quill 1.0 – Better Rich Text Editor for Web Apps Next Generation Server Compression With Brotli Debugging With Node.js .NET YOU'RE USING HTTPCLIENT WRONG AND IT IS DESTABILIZING YOUR SOFTWARE Troubleshooting High CP