uva 12661 Funny Car Racing - 单源最短路-道路有时间限制

题意:
从 u -> v 道路开放a秒,关闭b秒,从 u->v 需要花费 t 秒,在点处可以进行等待,等路开放
问从起点到终点最短要多少秒

解题思路
但源最短路,dijkstra就可以解决
首先 t>a 的路可以直接抛弃了
其次到达当前点更新的时候有两种情况,
1、道路是开放的并且可以在规定时间内走完这条路 d[u]%(a+b) + cost<= a
2、道路是不开的 并且等待几秒再走 a+b - d[u]%(a+b) + cost < d[v]

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <stack>
#include <cctype>
using namespace std;
typedef long long LL;

const int maxn = 50000+10;
const int INF = 0x3f3f3f3f;

struct edge{
    int to,cost;
    int a,b;
};

typedef pair <int,int> P;
vector <edge> G[maxn];
int d[maxn];

int n,m;
void dijkstra(int s){
    priority_queue<P,vector<P>,greater<P> > que;
    memset(d,INF,sizeof(d));
    d[s] = 0;
    que.push(P(0,s));
    while(!que.empty()){
        P p = que.top();que.pop();
        int v = p.second;
        if(d[v] < p.first)continue;
        for(int i = 0;i<G[v].size();++i){
            edge e = G[v][i];
            if(d[e.to] <= d[v]+e.cost) continue;///不用更新
            ///规定时间内能通过
            if(d[v]%(e.a+e.b) + e.cost <= e.a){
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to],e.to));
            }
            ///规定时间不能通过,等待一段时间
            else if(d[e.to] > d[v]+e.cost + (e.a+e.b) - (d[v]%(e.a+e.b))){
                d[e.to] =  d[v]+e.cost + (e.a+e.b) - (d[v]%(e.a+e.b));
                que.push(P(d[e.to],e.to));
            }
        }
    }

}

int main() {
    int n,m,s,t;
    int u,v,a,b,c;
    int cas = 1;
    while(scanf("%d%d%d%d",&n,&m,&s,&t)!=EOF){

        for(int i = 0;i<maxn;++i){
            G[i].clear();
        }
        while(m--){
            edge e;
            scanf("%d%d%d%d%d",&u,&e.to,&e.a,&e.b,&e.cost);
            if(e.a < e.cost) continue;
            G[u].push_back(e);
        }
        dijkstra(s);
        printf("Case %d: %d\n",cas++,d[t]);
    }

    return 0;
}
/*
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
*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值