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, lijwhere 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 whereM 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 ≤ riCtiC 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
    
  •  
     
    //写了两节课调了一节课,将(除法+减法)改成mod就AC,好囧
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #define M 100000
    int n,m;
    int st,en;
    struct NODE{
           int c,b,p,r;
    }node[310];
    struct EDGE{
           int x,next,l;
    }edge[28100];
    int k[310];
    int f[M];
    int d[310];
    bool v[310];
    int tot,sum;
    int pre[310];
    int len[310];
    int ans;
    int min(int a,int b){return a<b?a:b;}
    void add(int a,int b,int c){
         edge[++tot].x=b;
         edge[tot].l=c;
         edge[tot].next=k[a];
         k[a]=tot;
    }
    int wait(int x,int y,int t){
        int i,j;
        //初始状态比较 
        int tt=t;
        if (tt<node[x].r) i=node[x].c;
        else{
             tt=(t-node[x].r) % (node[x].p+node[x].b);
             int dd;
             if (node[x].c==0) dd=node[x].b;else dd=node[x].p;
             if (tt>=0 && tt<dd) i=!node[x].c;else i=node[x].c;
        }
        //------------------------------
        tt=t;
        if (tt<node[y].r) j=node[y].c;
        else{
             tt=(t-node[y].r) % (node[y].b+node[y].p);
             int dd;
             if (node[y].c==0) dd=node[y].b;else dd=node[y].p;
             if (tt>=0 && tt<dd) j=!node[y].c;else j=node[y].c;
        }
        if (i==j) return 0;
        //不同进入第一次变灯 
        int tx,ty;
        if (t<node[x].r) tx=node[x].r;
        else{
             int mid=(t-node[x].r) % (node[x].b+node[x].p);
             int dd;
             if (node[x].c==0) dd=node[x].b;else dd=node[x].p;
             if (mid>=0 && mid<dd) tx=t-mid+dd;
             else tx=t-mid+(node[x].b+node[x].p);
        }
        //------------------------------
        if (t<node[y].r) ty=node[y].r;
        else{
             int mid=(t-node[y].r) % (node[y].b+node[y].p);
             int dd;
             if (node[y].c==0) dd=node[y].b;else dd=node[y].p;
             if (mid>=0 && mid<dd) ty=t-mid+dd;
             else ty=t-mid+(node[y].b+node[y].p);
        }
        if (tx!=ty) return min(tx,ty)-t;
        //第一次编订后仍不同进入第二次变灯 
        int mid=(tx-node[x].r) % (node[x].b+node[x].p); 
        int dd;
        if (node[x].c==0) dd=node[x].b;else dd=node[x].p;
        if (mid>=0 && mid<dd) tx=tx-mid+dd;
        else tx=tx-mid+(node[x].b+node[x].p);
        //------------------------------
        mid=(ty-node[y].r) % (node[y].b+node[y].p);
        if (node[y].c==0) dd=node[y].b;else dd=node[y].p;
        if (mid>=0 && mid<dd) ty=ty-mid+dd;
        else ty=ty-mid+(node[y].b+node[y].p);
        if (tx!=ty) return min(tx,ty)-t;
        //第二次变灯后仍不同进入第三次变灯
        mid=(tx-node[x].r) % (node[x].b+node[x].p); 
        if (node[x].c==0) dd=node[x].b;else dd=node[x].p;
        if (mid>=0 && mid<dd) tx=tx-mid+dd;
        else tx=tx-mid+(node[x].b+node[x].p);
        //------------------------------
        mid=(ty-node[y].r) % (node[y].b+node[y].p);
        if (node[y].c==0) dd=node[y].b;else dd=node[y].p;
        if (mid>=0 && mid<dd) ty=ty-mid+dd;
        else ty=ty-mid+(node[y].b+node[y].p);
        if (tx!=ty) return min(tx,ty)-t;
        //第三次编订后仍不同进入第四次变灯
        mid=(tx-node[x].r) % (node[x].b+node[x].p); 
        if (node[x].c==0) dd=node[x].b;else dd=node[x].p;
        if (mid>=0 && mid<dd) tx=tx-mid+dd;
        else tx=tx-mid+(node[x].b+node[x].p);
        //------------------------------
        mid=(ty-node[y].r) % (node[y].b+node[y].p);
        if (node[y].c==0) dd=node[y].b;else dd=node[y].p;
        if (mid>=0 && mid<dd) ty=ty-mid+dd;
        else ty=ty-mid+(node[y].b+node[y].p);
        if (tx!=ty) return min(tx,ty)-t;
        //第四次编订后仍不同进入第五次变灯
        mid=(tx-node[x].r) % (node[x].b+node[x].p);
        if (node[x].c==0) dd=node[x].b;else dd=node[x].p;
        if (mid>=0 && mid<dd) tx=tx-mid+dd;
        else tx=tx-mid+(node[x].b+node[x].p);
        //------------------------------
        mid=(ty-node[y].r) % (node[y].b+node[y].p);
        if (node[y].c==0) dd=node[y].b;else dd=node[y].p;
        if (mid>=0 && mid<dd) ty=ty-mid+dd;
        else ty=ty-mid+(node[y].b+node[y].p);
        if (tx!=ty) return min(tx,ty)-t;
        //无法通行 
        return -1;
    }
    //最短路求解 
    void SPFA(){
         int head=0,tail=1;
         memset(d,63,sizeof(d));
         memset(v,0,sizeof(v));
         d[st]=0;
         v[st]=true;
         f[tail]=st;
         while (head!=tail){
               head++;
               if (head==M) head=1;
               int x=f[head];
               v[x]=false;
               for (int t=k[x];t;t=edge[t].next){
                   int tmp=wait(x,edge[t].x,d[x]);
                   if (tmp!=-1 && d[edge[t].x]>d[x]+tmp+edge[t].l){
                                                  d[edge[t].x]=d[x]+tmp+edge[t].l;
                                                  pre[edge[t].x]=x;
                                                  if (!v[edge[t].x]){
                                                                  v[edge[t].x]=true;
                                                                  tail++;
                                                                  if (tail==M) tail=1;
                                                                  f[tail]=edge[t].x;
                                                  }
                   }
               }
         }
         ans=d[en];
    }    
    int main(){
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
        scanf("%d%d\n",&st,&en);
        if (st==en){
                    printf("0\n%d\n",st);
                    return 0;
        }
        scanf("%d%d\n",&n,&m);
        for (int i=1;i<=n;i++){
            char c;
            scanf("%c%d%d%d\n",&c,&node[i].r,&node[i].b,&node[i].p);
            node[i].c=('B'==c);
        }
        for (int i=1;i<=m;i++){
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);
            add(y,x,z);
        }
        SPFA();
        if (ans>100000){printf("0\n");return 0;}
        printf("%d\n",ans);
        len[++sum]=en;
        int t=pre[en];
        while (t){
              len[++sum]=t;
              t=pre[t];
        }
        for (int i=sum;i>1;i--) printf("%d ",len[i]);
        printf("%d\n",len[1]);
        return 0;
    }