uva 12661(最短路)

思路:Dijkstra算法,算边权时,分情况讨论。
#include<cstdio>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int maxn = 100000 + 10;
const double EPS = 1e-5;
const int mod = 1e8 + 7;
const double g = 9.81;
struct Edge{
    int from, to, a, b, t;
    Edge(int from, int to, int a, int b, int t):from(from),to(to),a(a),b(b),t(t) {}
};
vector<Edge> edges;
vector<int> G[maxn];
int n, m, s, t;
int ans[maxn];
int vis[maxn];
struct HeapNode{
    int d, u;
    HeapNode(int u, int d):u(u),d(d) {}
    bool operator < (const HeapNode& rhs) const{
        return d > rhs.d;
    }
};
void solve(int x){
    for(int i = 1; i <= n; ++i) ans[i] = INF;
    ans[x] = 0;
    priority_queue<HeapNode> q;
    q.push(HeapNode(x, 0));
    memset(vis, 0, sizeof vis);
    while(!q.empty()){
        int u = q.top().u; q.pop();
        if(vis[u]) continue;
        vis[u] = 1;
        for(int i = 0; i < G[u].size(); ++i){
            int v = G[u][i];
            Edge &e = edges[v];
            int k = ans[u] % (e.a + e.b);
            if(k + e.t <= e.a &&  ans[e.to] > ans[e.from] + e.t && e.t <= e.a){
                ans[e.to] = ans[e.from] + e.t;
                q.push(HeapNode(e.to, ans[e.to]));
            }
            else if(k  + e.t > e.a && ans[e.to] > ans[e.from] + e.a + e.b + e.t - k && e.t <= e.a){
                 ans[e.to] = ans[e.from] + e.t + e.a + e.b - k;
                q.push(HeapNode(e.to, ans[e.to]));
            }
        }

    }
}
int main(){
    int kase = 0;
    while(~scanf("%d%d%d%d", &n, &m, &s, &t)){
        edges.clear();
        for(int i = 1; i <= n; ++i) G[i].clear();
        for(int i = 0; i < m; ++i){
            int u, v, a, b, t;
            scanf("%d%d%d%d%d", &u, &v, &a, &b, &t);
            edges.push_back(Edge(u, v, a, b, t));
            G[u].push_back(i);
        }
        solve(s);
        printf("Case %d: %d\n", ++kase, ans[t]);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值