SGU 103. Traffic Lights (SPFA)

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 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 Bfor blue or P for purple, 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: ijlijwhere 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

    题意:一个无向图,每个点上有一个交通灯,对于一条边,只有在两端点的灯是一样颜色的时候才可以走。求s到t的最短时间,并输出路径。


    因为求最短时间,可以用spfa的思想不断更新从s到每一个点的最短时间,最后就能求出s到t的最短时间。

    更新的时候就枚举两端的交通灯第一次一样颜色的时间,除非两个端点在同一时刻变色且周期是一样(应该只有这一种,欢迎补充),其他情况都应该在不超过两个周期的时间内能变成同一种颜色,所以枚举就可以了。

    对于路径输出,保存一下这一个点是从哪个点更新过来的就行了。


    #include<queue>
    #include<vector>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define LL long long
    
    using namespace std;
    
    const int inf = 1e8;
    
    int n,m,s,t;
    
    int dis[330],vis[330],pre[330];
    struct node
    {
        int c,r,tb,tp;
    }p[330];
    struct edge
    {
        int to,cost;
        edge(int _to,int _cost)
        {
            to = _to;
            cost = _cost;
        }
    };
    vector<edge> e[330];
    
    int judge(int x,int time) //判断颜色
    {
        if(time < p[x].r)
            return p[x].c;
        int t = (time - p[x].r) % (p[x].tb + p[x].tp);
        if(p[x].c == 0)
        {
            if(t >= p[x].tp)
                return 0;
            else
                return 1;
        }
        else
        {
            if(t >= p[x].tb)
                return 1;
            return 0;
        }
    }
    int spfa()
    {
        queue<int> q;
        for(int i=1;i<=n;i++)
            dis[i] = inf;
        memset(pre,0,sizeof(pre));
        dis[s] = 0;
        vis[s] = 1;
        q.push(s);
        while(!q.empty())
        {
            int x = q.front();
            q.pop();
            vis[x] = 0;
            for(int i=0;i<e[x].size();i++)
            {
                int xx = e[x][i].to;
                int cost = e[x][i].cost;
                for(int j=dis[x];j<=dis[x]+300;j++) //不超过两个周期
                {
                    if(judge(x,j) == judge(xx,j))
                    {
                        if(j + cost < dis[xx])
                        {
                            dis[xx] = j + cost;
                            pre[xx] = x;
                            if(vis[xx] == 0)
                            {
                                vis[xx] = 1;
                                q.push(xx);
                            }
                        }
                        break;
                    }
                }
            }
        }
        if(dis[t] != inf)
            return dis[t];
        else
            return 0;
    }
    
    void print(int x)
    {
        if(x == 0)
            return;
        print(pre[x]);
        printf("%d",x);
        if(x == t)
            printf("\n");
        else
            printf(" ");
    }
    
    int main(void)
    {
        int i,j;
        char ch[5];
        while(scanf("%d%d",&s,&t)==2)
        {
            scanf("%d%d",&n,&m);
            for(i=1;i<=n;i++)
            {
                scanf("%s %d %d %d",ch,&p[i].r,&p[i].tb,&p[i].tp);
                if(ch[0] == 'B')
                    p[i].c = 0;
                else
                    p[i].c = 1;
                e[i].clear();
            }
            for(i=1;i<=m;i++)
            {
                int x,y,z;
                scanf("%d%d%d",&x,&y,&z);
                e[x].push_back(edge(y,z));
                e[y].push_back(edge(x,z));
            }
            int ans = spfa();
            printf("%d\n",ans);
            print(t);
        }
    
        return 0;
    }
    


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值