POJ 1724 ROADS(领接链表的BFS+优先队列)

Description

N cities named with numbers 1 … N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the number of coins).
Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash.

We want to help Bob to find the shortest path from the city 1 to the city N that he can afford with the amount of money he has.
Input

The first line of the input contains the integer K, 0 <= K <= 10000, maximum number of coins that Bob can spend on his way.
The second line contains the integer N, 2 <= N <= 100, the total number of cities.

The third line contains the integer R, 1 <= R <= 10000, the total number of roads.

Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters :
S is the source city, 1 <= S <= N
D is the destination city, 1 <= D <= N
L is the road length, 1 <= L <= 100
T is the toll (expressed in the number of coins), 0 <= T <=100

Notice that different roads may have the same source and destination cities.
Output

The first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins.
If such path does not exist, only number -1 should be written to the output.
Sample Input

5
6
7
1 2 2 3
2 4 3 3
3 4 2 4
1 3 4 1
4 6 2 1
3 5 2 0
5 4 3 2
Sample Output

11
解题思路:从1 到N要想找花费不大于K的最短路径,首先想的是直接用结构体类型的邻接矩阵,然后再用优先队列,进行广搜,最后发现这样他的起点和终点不好确定,所以就用了邻接矩阵来构图,然后进行广搜就可以了。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#define INF 0x3f3f3f3f

using namespace std;
struct Node//定义一个结构体确定它的优先级是长度小的优先
{
    int v,k,len;
    friend bool operator<(Node a,Node b)
    {
        if(a.len!=b.len) return a.len > b.len;
        return a.k > b.k;
    }
};
//因为是以邻接链表储存且是有向图所以不需要标记数组
int K,N,R,S,D,L,T;
vector<Node> Map[105];//结构体类型的Map用来存图
void bfs()
{
    priority_queue<Node> Q;
    Node cur,next;
    int u,v;
    for(int i=0;i<Map[1].size();i++)//首先将和起始位置相关的节点全部入队
    {
        Q.push(Map[1][i]);
    }
    while(!Q.empty())
    {
        cur=Q.top();Q.pop();
        //printf("cur %d %d %d\n",cur.v,cur.len,cur.k);
        if(cur.v==N)//如果当前节点是目标城市即可以到达目标城市就输出最短路径
         {
             printf("%d\n",cur.len);return;
         }
        u=cur.v;//u为与这个当前城市相连城市的编号
        for(int i=0;i<Map[u].size();i++)
        {
            if(cur.k+Map[u][i].k<=K)//如果当前城市和与他相连的这个城市的花费小于他的总花费就可以入队
            {
                next.len=cur.len+Map[u][i].len;//更新长度即当前城市的长度加上与它相连城市的长度
                next.k=cur.k+Map[u][i].k;//更新花费即当前城市的花费加上与它相连城市的花费
                next.v=Map[u][i].v;//下一个城市的编号就为与当前城市相连城市的编号
               // printf("next %d %d %d\n",next.v,next.len,next.k);
                Q.push(next);//入队
            }
        }
    }
    printf("-1\n");//如果到不了N就输出-1
}
int main()
{
     scanf("%d %d %d",&K,&N,&R);
     Node temp;
     for(int i=1;i<=N;i++)
        Map[i].clear();
     for(int i=0;i<R;i++)
     {
         scanf("%d %d %d %d",&S,&D,&L,&T);
         temp.v=D;temp.len=L;temp.k=T;
         Map[S].push_back(temp);//构图以S为表头节点,将于S相关的节点都接在S后边
     }
//     for(int i=1;i<=N;i++)
//     {
//         for(int j=0;j<Map[i].size();j++)
//            printf(" %d %d %d %d    ", i,Map[i][j].v,Map[i][j].len,Map[i][j].k);
//        cout<<endl;
//     }
     bfs();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值