Funny Car Racing(最短路变形)

描述

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.

输入

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.

输出

For each test case, print the shortest time, in seconds. It's always possible to arrive at t from s.

样例输入

样例输出

题目来源

湖南省第九届大学生程序设计竞赛

题解:将时间看成最短路的模型,上一层的时间已经是最优的解了,所以下一层在更新的结点也会是最优的,如果到达的当前边时间为 T,那么就要判断是否要在当前城市等待了。。

 

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int INF = 999999999;
int n,m;
struct Edge{
    int v,a,b,t,next;
}edge[50005];
int tot;
int head[305];
void addEdge(int u,int v,int a,int b,int t,int &k){
    edge[k].v = v,edge[k].a = a,edge[k].b = b,edge[k].t = t,edge[k].next = head[u],head[u]=k++;
}
void init(){
    memset(head,-1,sizeof(head));
    tot = 0;
}
int low[305];
bool vis[305];
int dijsktra(int s,int t){
    memset(vis,false,sizeof(vis));
    for(int i=1;i<=n;i++){
        low[i] = INF;
    }
    low[s] = 0;
    vis[s] = true;
    for(int i=1;i<n;i++){
        int MIN = INF;
        for(int j=1;j<=n;j++){
            if(low[j]<MIN&&!vis[j]){
                MIN = low[j];
                s = j;
            }
        }
        vis[s] = true;
        for(int k=head[s];k!=-1;k=edge[k].next){
            int v = edge[k].v,a=edge[k].a,b = edge[k].b,tim = edge[k].t ;
            if(a>=tim){
                int t0 = low[s]%(a+b);
                if(t0+tim<=a) low[v] = min(low[s]+tim,low[v]);
                else low[v] = min(low[s]+a+b-t0+tim,low[v]);
            }
        }
    }
    return low[t];
}
int main()
{
    int s,t,cas=1;
    while(scanf("%d%d%d%d",&n,&m,&s,&t)!=EOF){
        init();
        for(int i=1;i<=m;i++){
            int u,v,a,b,t;
            scanf("%d%d%d%d%d",&u,&v,&a,&b,&t);
            addEdge(u,v,a,b,t,tot);
        }
        printf("Case %d: %d\n",cas++,dijsktra(s,t));
    }
    return 0;
}

 

 

 

posted @ 2016-08-20 09:58  樱花庄的龙之介大人  阅读(311)  评论(0编辑  收藏  举报