POJ-1724-ROADS(优先队列,邻接表,BFS)

E - ROADS
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

POJ 1724
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

题意:前三行输出K(最大花费),N(城市数量),R(道路数量),接下来R行,每行四个数字SDLT,代表S到T的距离为L,花费为T。在最大花费的前提下,输出最短路径长度

思路:迪杰斯特拉的思想,不过是采用优先队列的BFS,使路径短的先出队,路径相同时,花费少的先出队,新节点入队时,判断花费是否超标。由于是稀疏图,而且时间卡的比较紧,还是试着用邻接表存图吧。

代码

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;//正无穷
const int maxn=10005;
int K;//最大花费
int N;//城市数量
int R;//道路数量
int S;//起点编号
int D;//终点编号
int L;//路径长度
int T;//路径花费
struct state
{
    int u;//起点编号
    int v;//终点编号
    int w;//路径长度
    int c;//路径花费
    int next;//表头指针
} first[maxn]; //邻接表first数组
int next[maxn];//邻接表next数组
struct node
{
    int n;//当前城市序号
    int w;//路径长度
    int c;//路径花费
    friend bool operator < (node x,node y)//这里重载可能有问题
    {
        if(x.w==y.w)//路径相同时,花费少的先出队
            return x.c>y.c;
        return x.w>y.w;//路径小的先出队
    }
};//重载优先队列运算符
void BFS()//优先队列和邻接表优化的迪杰斯特拉
{
    priority_queue<node>q;//建立优先队列
    while(!q.empty())//初始化优先队列
        q.pop();
    node star,end;//中间变量
    int min_dis=INF;//初始化最短路径为正无穷
    star.n=1;//初始所在城市编号为1
   star.w=0;//初始已走路径为0
   star.c=0;//初始已花费为0
   q.push(star);//入队
   while(!q.empty())//优先队列配合邻接表的广搜
   {
       star=q.top();//读取一个结点
       q.pop();//删除已读取的结点
       if(star.n==N)//如果已经到达第N个城市
       {
           min_dis=star.w;//记录已走路径
           break;//跳出循环
       }
       for(int k=next[star.n];k!=-1;k=first[k].next)//遍历邻接表
       {
           if(star.c+first[k].c<=K)//选择下一个结点的花费没有超标
           {
               end.n=first[k].v;//更新当前城市编号
               end.w=first[k].w+star.w;//更新已走路径
               end.c=first[k].c+star.c;//更新已用花费
               q.push(end);//结点入队
           }
       }
   }
   min_dis==INF?printf("-1\n"):printf("%d\n",min_dis);
}
int main()
{
    scanf("%d%d%d",&K,&N,&R);//最大花费,城市数量,道路数量
    memset(next,-1,sizeof(next));
    for(int i=0; i<R;) //建立邻接表
    {
        scanf("%d%d%d%d",&S,&D,&L,&T);//s到t长度为l花费为t
        first[i].u=S;
        first[i].v=D;
        first[i].w=L;
        first[i].c=T;
        first[i].next=next[S];
        next[S]=i++;
    }
    BFS();
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值