poj 1724 ROADS

#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

int vist[105];
int head[10005];
struct node
{
    int from;
    int to;
    int next;
    int len;
    int c;
} s[10005];

struct Node
{

    int l, v, in;
    friend bool operator<(const Node &x, const Node &y)
    {

        if(x.l != y.l)
        {

            return x.l > y.l;
        }
        else
        {

            return x.v > y.v;
        }
    }
};
int ans;

int n, m, s1;
int cnt;

void add(int a, int b, int d, int e)
{
    s[cnt].from = a;
    s[cnt].to = b;
    s[cnt].len = d;
    s[cnt].next = head[a];
    s[cnt].c = e;
    head[a] = cnt;
    cnt++;

}

void bfs()
{

    priority_queue<Node>que;
    Node cur, next;
    cur.l = 0;
    cur.v = 0;
    cur.in = 1;
    que.push(cur);
    while(!que.empty())
    {

        cur = que.top();
        que.pop();
        if(cur.in == n)
        {

            ans = cur.l;
            break;
        }
        int now = cur.in;
        for(int i = head[now]; i != -1; i = s[i].next)
        {

            int u = s[i].to;
            int temp = s[i].len + cur.l;
            int mi = s[i].c + cur.v;
            if(mi <= s1)   //只要有硬币就可以继续查找,
            {

                next.in = u;
                next.l = temp;
                next.v = mi;
                que.push(next);
            }

        }

    }

}

int main()
{
    int a, b, d, e;
    while(scanf("%d", &s1) != EOF)
    {

        scanf("%d", &n);
        scanf("%d", &m);
        cnt = 0;
        memset(head, -1, sizeof(head));

        for(int i = 1; i <= m; i++)
        {

            scanf("%d%d%d%d", &a, &b, &d, &e);
            add(a, b, d, e);
        }
        memset(vist, 0, sizeof(vist));
        ans = -1;
        bfs();

        printf("%d\n", ans);

    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值