湖南省第九届省赛 Funny Car Racing

来源:互联网 发布:卖软件的店名 编辑:程序博客网 时间:2024/06/09 23:11

There is a funny car racing in a city with n junctions and m directed roads.
The funny part is: each road is open and closed periodically. Each road is associate with two
integers (a, b), that means the road will be open for a seconds, then closed for b seconds, then open for
a seconds… All these start from the beginning of the race. You must enter a road when it’s open, and
leave it before it’s closed again.
Your goal is to drive from junction s and arrive at junction t as early as possible. Note that you
can wait at a junction even if all its adjacent roads are closed.
Input
There will be at most 30 test cases. The first line of each case contains four integers n, m, s, t
(1 ≤ n ≤ 300, 1 ≤ m ≤ 50, 000, 1 ≤ s, t ≤ n). Each of the next m lines contains five integers u, v, a,
b, t (1 ≤ u, v ≤ n, 1 ≤ a, b, t ≤ 105
), that means there is a road starting from junction u ending with
junction v. It’s open for a seconds, then closed for b seconds (and so on). The time needed to pass this
road, by your car, is t. No road connects the same junction, but a pair of junctions could be connected
by more than one road.
Output
For each test case, print the shortest time, in seconds. It’s always possible to arrive at t from s.
Sample Input
3 2 1 3
1 2 5 6 3
2 3 7 7 6
3 2 1 3
1 2 5 6 3
2 3 9 5 6
Sample Output
Case 1: 20
Case 2: 9

这道题就是一个spfa,就是处理关卡的时候要处理好。
还是很简单的。

#include<cstdio>#include<iostream>using namespace std;#include<cstring>#include<vector>#include<queue>/*    Name:    Copyright:    Author:    Date: 07/05/16 20:12    Description:*/const int maxn=305;const int inf=0x3f3f3f3f;int used[maxn];struct note1{    int op,cl,ti;    note1(){    }    note1(int x,int y,int z){        op=x;        cl=y;        ti=z;    }};vector<note1> vec[maxn][maxn];int n,m,s,t;struct note{    int t,pos;    note(){    }    note(int x,int y){        pos=x;        t=y;    }};void bfs(){    queue<note> que;    while(!que.empty()){        que.pop();    }    memset(used,inf,sizeof(used));    used[s]=0;    que.push(note(s,0));    while(!que.empty()){        struct note q=que.front();        que.pop();//      cout<<"arrive="<<q.pos<<endl;        if(q.pos==t)continue;        int num=q.pos;        int time=q.t;        for(int i=1;i<=n;++i){            int len=vec[num][i].size();            if(len){                int mintime=inf;                for(int j=0;j<len;++j){                    int tis=time;                    struct note1 r=vec[num][i][j];                    int up=r.op+r.cl;//                  cout<<"time="<<time<<endl;                    if((tis+r.ti)%up<=r.op&&(tis%up>=0&&tis%up<=r.op)){                    }else{                        for(int k=1;;++k){                            if(k*up>=tis){                                tis=k*up;break;                            }                        }                    }                    tis+=r.ti;                    mintime=min(mintime,tis);                }                if(mintime!=inf&&used[i]>mintime){                    used[i]=mintime;//                  cout<<"push="<<mintime<<" pos="<<i<<endl;                    que.push(note(i,used[i]));                }            }        }    }    printf("%d\n",used[t]);}int main(){    int coun=0;    #ifndef ONLINE_JUDGE//  freopen("fcr.txt","r",stdin);freopen("f.in","r",stdin);freopen("fcrout.txt","w",stdout);    #endif    while(scanf("%d%d%d%d",&n,&m,&s,&t)!=-1){        printf("Case %d: ",++coun);        for(int i=0;i<maxn;++i){            for(int j=0;j<maxn;++j){                vec[i][j].clear();            }        }        for(int i=0;i<m;++i){            int u,v,as,bs,ts;            scanf("%d%d%d%d%d",&u,&v,&as,&bs,&ts);            if(as>=ts)vec[u][v].push_back(note1(as,bs,ts));        }        bfs();    }    return 0;}
0 0