SGU 103 Traffic Lights 经典最短路

本文介绍了一种在交通信号灯控制的城市中寻找从起点到终点最短路径的方法。考虑到信号灯颜色随时间变化的特点,利用Dijkstra算法进行扩展,通过计算不同路径上的等待时间来确定最优路线。

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

-----------------

103. Traffic Lights
Time limit per test: 0.5 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: NM. The following N lines contain information on N junctions. The (i+2)'th line of the input file holds information about the junction i : CiriCtiBtiP where Ci is either B for blue or P forpurple, indicating the initial color of the light at the junction i. Finally, the next M lines contain information on M roads. Each line is of the form: ijlij where i and j are the id-numbers of the junctions which are connected by this road. 2 ≤ N ≤ 300 where N is the number of junctions. The junctions are identified by integers 1 through N. These numbers are called id-numbers. 1 ≤ M ≤ 14000 where M is the number of roads. 1 ≤ lij ≤ 100 where lij is the time required to move from junction i to j using the road that connects i and j. 1 ≤ tiC ≤ 100 where tiC is the duration of the color c for the light at the junction i. The index c is either 'B' for blue or 'P' for purple. 1 ≤ riC ≤ tiC where riC is the remaining time for the initial color c at junction i

    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
    


    -----------------

    边权随着时间变化。可以证明dijkstra的贪心策略仍然适用。

    所以只要计算出两点间的等待时间即可。

    -----------------

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    using namespace std;
    const int maxn=1111;;
    const int maxm=41111;
    const int INF=0x3f3f3f3f;
    int st,ed;
    int n,m;
    struct Node{
        char c[2];
        int r,tb,tp;
    }a[maxn];
    
    class Dijkstra{
    private:
        struct EdgeNode{
            int to,w,next;
        };
        struct HeapNode{
            int u,d;
            HeapNode(int _u,int _d):u(_u),d(_d){}
            bool operator<(const HeapNode& rhs) const{
                return d>rhs.d;
            }
        };
        EdgeNode edges[maxm];
        int head[maxn],edge;
        int dis[maxn],pre[maxn];
        bool vis[maxn];
        priority_queue<HeapNode>que;
        void color(int time,int x,int &px,char cx[]){
            if (time<a[x].r){
                px=a[x].r-time;
                cx[0]=a[x].c[0];
                return;
            }
            int t=(time-a[x].r)%(a[x].tb+a[x].tp);
            if (a[x].c[0]=='B'&&t<a[x].tp){
                px=a[x].tp-t;
                cx[0]='P';
            }
            else if (a[x].c[0]=='P'&&t<a[x].tb){
                px=a[x].tb-t;
                cx[0]='B';
            }
            else{
                px=a[x].tb+a[x].tp-t;
                if (a[x].c[0]=='B') cx[0]='B';
                else cx[0]='P';
            }
        }
    public:
        int getTime(int time,int x,int y,int num){
            if (num>3) return -1;
            int px,py;
            char cx[2],cy[2];
            color(time,x,px,cx);
            color(time,y,py,cy);
            if (cx[0]==cy[0]) return time;
            if (px==py) return getTime(time+px,x,y,num+1);
            time+=min(px,py);
            return time;
        }
    
        void addedge(int u,int v,int w){
            edges[edge].w=w,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++;
        }
        void init(){
            memset(head,-1,sizeof(head));
            edge=0;
        }
        int getDis(int i){
            return dis[i];
        }
        int getPre(int i){
            return pre[i];
        }
        void dijkstra(int s){
            while (!que.empty()) que.pop();
            memset(dis,0x3f,sizeof(dis));
            memset(vis,0,sizeof(vis));
            memset(pre,-1,sizeof(pre));
            dis[s]=0;
            que.push(HeapNode(s,0));
            while (!que.empty()){
                HeapNode top=que.top();
                que.pop();
                int u=top.u;
                if (vis[u]) continue;
                vis[u]=true;
                for (int i=head[u];i!=-1;i=edges[i].next){
                    int v=edges[i].to;
                    int w=edges[i].w;
                    int time=getTime(dis[u],u,v,1);
                    if (time==-1) continue;
                    if (dis[v]>time+w){
                        dis[v]=time+w;
                        pre[v]=u;
                        que.push(HeapNode(v,dis[v]));
                    }
                }
            }
        }
        void output(int u){
            if (pre[u]!=-1) output(pre[u]);
            else{
                printf("%d",u);
                return;
            }
            printf(" %d",u);
        }
    }dij;
    
    void init(){
        dij.init();
    }
    
    void input(){
        scanf("%d%d",&st,&ed);
        scanf("%d%d",&n,&m);
        for (int i=1;i<=n;i++){
            scanf("%s%d%d%d",a[i].c,&a[i].r,&a[i].tb,&a[i].tp);
        }
        for (int i=1;i<=m;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            dij.addedge(u,v,w);
            dij.addedge(v,u,w);
        }
    }
    
    void solve(){
        dij.dijkstra(st);
        if (dij.getDis(ed)==INF) printf("0\n");
        else{
            printf("%d\n",dij.getDis(ed));
            dij.output(ed);
            printf("\n");
        }
    }
    
    int main()
    {
        init();
        input();
        solve();
        return 0;
    }
    
    


    -----------------

    -----------------


标题基于SpringBoot+Vue的社区便民服务平台研究AI更换标题第1章引言介绍社区便民服务平台的研究背景、意义,以及基于SpringBoot+Vue技术的研究现状和创新点。1.1研究背景与意义分析社区便民服务的重要性,以及SpringBoot+Vue技术在平台建设中的优势。1.2国内外研究现状概述国内外在社区便民服务平台方面的发展现状。1.3研究方法与创新点阐述本文采用的研究方法和在SpringBoot+Vue技术应用上的创新之处。第2章相关理论介绍SpringBoot和Vue的相关理论基础,以及它们在社区便民服务平台中的应用。2.1SpringBoot技术概述解释SpringBoot的基本概念、特点及其在便民服务平台中的应用价值。2.2Vue技术概述阐述Vue的核心思想、技术特性及其在前端界面开发中的优势。2.3SpringBoot与Vue的整合应用探讨SpringBoot与Vue如何有效整合,以提升社区便民服务平台的性能。第3章平台需求分析与设计分析社区便民服务平台的需求,并基于SpringBoot+Vue技术进行平台设计。3.1需求分析明确平台需满足的功能需求和性能需求。3.2架构设计设计平台的整体架构,包括前后端分离、模块化设计等思想。3.3数据库设计根据平台需求设计合理的数据库结构,包括数据表、字段等。第4章平台实现与关键技术详细阐述基于SpringBoot+Vue的社区便民服务平台的实现过程及关键技术。4.1后端服务实现使用SpringBoot实现后端服务,包括用户管理、服务管理等核心功能。4.2前端界面实现采用Vue技术实现前端界面,提供友好的用户交互体验。4.3前后端交互技术探讨前后端数据交互的方式,如RESTful API、WebSocket等。第5章平台测试与优化对实现的社区便民服务平台进行全面测试,并针对问题进行优化。5.1测试环境与工具介绍测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值