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.

样例输入

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

样例输出

Case 1: 20 Case 2: 9

题目来源

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

题解:将时间看成最短路的模型,上一层的时间已经是最优的解了,所以下一层在更新的结点也会是最优的,如果到达的当前边时间为 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;
}

 

 

 

转载于:https://www.cnblogs.com/liyinggang/p/5789712.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值