• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

SGU 103 Traffic Lights

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: 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 : Ci, riC, tiB, tiP where Ci is either B for 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: i, j, lij 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
    
  1 #pragma comment(linker, "/STACK:1024000000,1024000000")
  2 #include <map>
  3 #include <queue>
  4 #include <vector>
  5 #include <string>
  6 #include <cmath>
  7 #include <cstdio>
  8 #include <cstring>
  9 #include <cstdlib>
 10 #include <iostream>
 11 #include <algorithm>
 12 using namespace std;
 13 #define maxn 1005
 14 #define ll long long
 15 #define INF 0x7fffffff
 16 #define eps 1e-8
 17 struct Node{
 18     int now, len,clen,col;
 19     void clear(){
 20         now = 0; len = 0; col = 0; clen = 0;
 21     }
 22 };
 23 struct Edge{
 24     int from, to, dist;
 25     Edge(int a, int b, int v){ from = a; to = b; dist = v; }
 26 };
 27 vector<Edge>edges;
 28 vector<int>g[maxn];
 29 Node t[maxn];
 30 int d[maxn];
 31 int pre[maxn];
 32 int in[maxn];
 33 int n, m, s, to;
 34 void init(){
 35     for (int i = 0; i <= n; i++)g[i].clear(),t[i].clear();
 36     edges.clear();
 37 }
 38 void AddEdge(int from, int to, int dist){
 39     edges.push_back(Edge(from, to, dist));
 40     int sz = edges.size();
 41     g[from].push_back(sz - 1);
 42 }
 43 void spfa(){
 44     for (int i = 0; i <= n; i++)d[i] = INF;
 45     int u; 
 46     queue<int>q;
 47     d[s] = 0; in[s] = 1; q.push(s);
 48     while (!q.empty()){
 49         u = q.front(); q.pop();
 50         in[u] = 0;
 51         for (int i = 0; i < g[u].size(); i++){
 52             Edge e = edges[g[u][i]];
 53             int colu, colto, lesu, lesto, wait = 0;
 54             lesu = (d[u] + t[u].now) % t[u].len;
 55             if (lesu >= t[u].clen){ colu = 2; lesu = t[u].len - lesu; }
 56             else { colu = 1; lesu = t[u].clen - lesu; }
 57             lesto = (d[u] + t[e.to].now) % t[e.to].len;
 58             if (lesto >= t[e.to].clen){ colto = 2; lesto = t[e.to].len - lesto; }
 59             else { colto = 1; lesto = t[e.to].clen - lesto; }
 60             if (colu != colto){
 61                 if (lesu == lesto&&t[u].clen == t[e.to].len - t[e.to].clen&&t[u].len == t[e.to].len)continue;
 62                 if (lesu == lesto){
 63                     if (colu == 1){
 64                         if (t[u].len - t[u].clen == t[e.to].clen)wait = lesu + t[e.to].clen + min(t[u].clen, t[e.to].len - t[e.to].clen);
 65                         else wait = lesu + min(t[e.to].clen, t[u].len - t[u].clen);
 66                     }
 67                     if (colu == 2){
 68                         if (t[u].clen == t[e.to].len - t[e.to].clen)wait = lesu + t[u].clen + min(t[e.to].clen, t[u].len - t[u].clen);
 69                         else wait = lesu + min(t[u].clen, t[e.to].len - t[e.to].clen);
 70                     }
 71                 }
 72                 else wait = min(lesu, lesto);
 73             }
 74             if (d[e.to] > d[u] + e.dist + wait){
 75                 d[e.to] = d[u] + e.dist + wait;
 76                 pre[e.to] = g[u][i];
 77                 if (!in[e.to]){
 78                     in[e.to] = 1;
 79                     q.push(e.to);
 80                 }
 81             }
 82         }
 83     }
 84 }
 85 void print(int u){
 86     if (edges[pre[u]].from != s)print(edges[pre[u]].from);
 87     else printf("%d ", s);
 88     if (u == to){printf("%d", to); return;}
 89     printf("%d ", u);
 90 }
 91 int main(){
 92     int cas = 1;
 93     while (~scanf("%d%d", &s, &to)){
 94         scanf("%d%d", &n, &m);
 95         int a, b, v;
 96         char col[5];
 97         init();
 98         for (int i = 1; i <= n; i++){
 99             scanf("%s%d%d%d", col, &v, &a, &b);
100             if (col[0] == 'P'){ t[i].col = 2; t[i].now = a + b - v; }
101             else { t[i].col = 1; t[i].now = a - v; }
102             t[i].clen = a;
103             t[i].len = a + b;
104         }
105         for (int i = 0; i < m; i++){
106             scanf("%d%d%d", &a, &b, &v);
107             AddEdge(a, b, v);
108             AddEdge(b, a, v);
109         }
110         if (s == to){printf("0\n%d\n", s); continue;}
111         spfa();
112         if (d[to]!=INF){ printf("%d\n", d[to]); print(to);}
113         else printf("0");
114         printf("\n");
115     }
116     return 0;
117 }
View Code

 

posted @ 2013-10-31 11:58  HaibaraAi  阅读(102)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3