[POJ 1724] ROADS (有约束条件的最短路)

ROADS

题目链接:http://poj.org/problem?id=1724

题目大意:

有一副有向图,每条边有两个权值,一个是路径长度,一个是走该条路的花销。现在问在不超过K元钱的基础上,从起点到终点的最短路径。

解题思路:

有条件约束的最短路,可以用Dijkstra + heap 做。(采用优先队列)


#include<iostream>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<ctype.h>
#include<algorithm>
#include<string>
#define PI acos(-1.0)
#define maxn 1000
#define INF 1<<25
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
using namespace std;
struct node
{
    int x, l, t;
    bool friend operator < (node s, node v) // 先根据路径长度非递减排序,如果长度相同根据费用非递减排序
    {
        if (s.l != v.l) return s.l > v.l;
        return s.t > v.t;
    }
};
struct E
{
    int d, l, t;
};
int k, n, r;
vector<E> v[110];
int bfs()
{
    node now, tmp;
    now.x = 1, now.l = 0, now.t = 0;
    priority_queue<node> q;
    q.push(now);
    while(!q.empty())
    {
        now = q.top();
        q.pop();
        if (now.x == n) return now.l;
        for (int u = 0; u < v[now.x].size(); u++) if (now.t + v[now.x][u].t <= k)
        {
            tmp.x = v[now.x][u].d, tmp.l = now.l + v[now.x][u].l, tmp.t = now.t + v[now.x][u].t;
            q.push(tmp);
        }
    }
    return -1;
}
int main ()
{
    scanf("%d%d%d", &k, &n, &r);
    while(r--)
    {
        int s;
        E e;
        scanf("%d%d%d%d", &s, &e.d, &e.l, &e.t);
        v[s].push_back(e);
    }
    printf("%d\n", bfs());
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值