103. Traffic Lights

9 篇文章 0 订阅

Time limit per test: 0.25 second(s)
Memory limit: 4096 kilobytes
input: standard
output: standard



In the city of Dingilville the traffic is arranged in an unusual way. There are junctions and roads connecting the junctions. There is at most one road between any two different junctions. There is no road connecting a junction to itself. Travel time for a road is the same for both directions. At every junction there is a single traffic light that is either blue or purple at any moment. The color of each light alternates periodically: blue for certain duration and then purple for another duration. Traffic is permitted to travel down the road between any two junctions, if and only if the lights at both junctions are the same color at the moment of departing from one junction for the other. If a vehicle arrives at a junction just at the moment the lights switch it must consider the new colors of lights. Vehicles are allowed to wait at the junctions. You are given the city map which shows:
  • the travel times for all roads (integers)
  • the durations of the two colors at each junction (integers)
  • and the initial color of the light and the remaining time (integer) for this color to change at each junction.

    Your task is to find a path which takes the minimum time from a given source junction to a given destination junction for a vehicle when the traffic starts. In case more than one such path exists you are required to report only one of them.

    Input
    The first line contains two numbers: The id-number of the source junction and the id-number of the destination junction. The second line contains two numbers:N,M. The followingN lines contain information on N junctions. The (i+2)'th line of the input file holds information about the junctioni :Ci,riC,tiB,tiP whereCi is eitherB forblue orP for purple, indicating the initial color of the light at the junctioni. Finally, the nextM lines contain information onM roads. Each line is of the form:i,j, lij wherei andj are the id-numbers of the junctions which are connected by this road. 2 ≤N ≤ 300 whereN is the number of junctions. The junctions are identified by integers 1 throughN. These numbers are called id-numbers. 1 ≤M ≤ 14000 whereM is the number of roads. 1 ≤ lij ≤ 100 wherelij is the time required to move from junctioni toj using the road that connects i and j. 1 ≤tiC ≤ 100 wheretiC is the duration of the colorc for the light at the junctioni. The index c is either 'B' for blue or 'P' for purple. 1 ≤riCtiC whereriC is the remaining time for the initial colorc at junctioni.

    Output
    If a path exists:
  • The first line will contain the time taken by a minimum-time path from the source junction to the destination junction.
  • Second line will contain the list of junctions that construct the minimum-time path you have found. You have to write the junctions to the output file in the order of travelling. Therefore the first integer in this line must be the id-number of the source junction and the last one the id-number of the destination junction.

    If a path does not exist:
  • A single line containing only the integer 0.

    Example(s)
    sample input
    sample output
    1 4
    4 5
    B 2 16 99
    P 6 32 13
    P 2 87 4
    P 38 96 49
    1 2 4
    1 3 40
    2 3 75
    2 4 76
    3 4 77
    
    127
    1 2 4
    

    一个城市有很多路口,每个路口有一个交通灯,灯的颜色是紫色或蓝色,每两个路口之间最多有一条路,路口没有通向自己的路,当一条路两段路口灯颜色一样的时候,车可以通过这条路,如果车在路中行驶的时候不用考虑灯的颜色,求从源点到目的地的最短时间

先给出两个数         起点s                 终点t

接下来两个数         路口数量n         路的数量m

接下来n行              B/P初始此路口灯的颜色c     初始颜色在路口持续的时间tc     每次蓝灯持续的时间tb     每次紫灯持续的时间tp

接下来m行             路口u     路口v     通过路uv需要的时间








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
// 声明的抽象方法,返回下一个交通信号灯 public abstract TrafficSignal nextSignal(); public enum TrafficSignal { RED(30) { @Override public TrafficSignal nextSignal() { return GREEN; } }, YELLOW(5) { @Override public TrafficSignal nextSignal() { return RED; } }, GREEN(45) { @Override public TrafficSignal nextSignal() { return YELLOW; } }; private int duration; // 持续时间 private TrafficSignal(int duration) { this.duration = duration; } public int getDuration() { return duration; } } public class TestEnum { public static void main(String[] args) throws Exception{ System.out.println("打印出所有的交通信号灯:"); TrafficSignal[] lights = TrafficSignal.values(); // 使用TrafficSignal类的静态方法values,以获得所有枚举常量组成的数组 for(TrafficSignal light : lights) { System.out.println(light.ordinal() + ":" + light.name()); } System.out.println("模拟交通信号灯的变化过程:"); TrafficSignal light = TrafficSignal.GREEN; //创建枚举对象:绿色的交通信号灯 for(int i =0; i<7; i++) { System.out.println(light); Thread.sleep(100*light.getDuration()); // 程序会暂停100*duration毫秒,为了加快测试输出,把1秒模拟为100毫秒 light = light.nextSignal(); // 切换到下一个交通信号灯 } } } 在这个示例代码中,我们定义了一个TrafficSignal枚举类,它有三个枚举常量:RED,YELLOW和GREEN。每个枚举常量都有一个持续时间duration,以表示它的持续时间。 在TrafficSignal枚举类中,我们还定义了一个抽象方法nextSignal(),用于返回下一个交通信号灯。每个枚举常量都实现了这个方法,以便能够切换到下一个交通信号灯。 在TestEnum类中,我们首先打印出所有的交通信号灯,然后模拟交通信号灯的变化过程。我们创建了一个TrafficSignal对象,初始值为GREEN,然后使用for循环模拟了交通信号灯的变化过程。在循环中,我们输出当前的交通信号灯,然后使用Thread.sleep()方法模拟灯亮起时间,最后调用nextSignal()方法切换到下一个交通信号灯。 希望这个示例代码能够帮助您理解Java枚举类型的使用方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值