ROADS POJ 1724(bfs)

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条从u到v的的长度,已经所

需要的花费。要求算出从1出发,走到N点时,在不超出自己已有

的经费内,找出所需要走的最短的路,如果找不到,输出-1。

 

看到这道题时,刚开始想用Dijkstra()写了,再加一些优化条件,测试数据挺顺的,可惜还是WA。后来改成优先队列,用了邻接表存了一下各个点的多种情况

 

           
 f[i].next = head[f[i].u];        //存储上一次出现的位置 
 head[f[i].u] = i;                //存储这次出现的位置 
 f[i].next = head[f[i].u];        //存储上一次出现的位置 
 head[f[i].u] = i;                //存储这次出现的位置 

 

       通过判断点u到v所用的花费和之前的花费之和 是否超出本有的资金,再决定是否进入队列中。

 


for(int i = head[now.u]; i ; i = f[i].next)  
                                        //找到所有和now.s相连的点 
  if(now.cost + f[i].cost <= K)   //注意这里面的i表示的是第几条边 
  {
       tmp.cost = now.cost + f[i].cost;
       tmp.u = f[i].v;
       tmp.s = now.s + f[i].l;
       Q.push(tmp);
   }

 

    本题中,首先用结构体存入R条道路中,所给出的信息。然后再用一个结构体保存在寻找过程中所走的最短路口,以及所需要的花费

 

struct note
{
    int u,v,l,cost;    //地点u -> v, 距离  花费  
    int next;          

 }f[10010];


struct node
{
    int u,s,cost;
    friend bool operator < (node a,node b)
    {
        return a.s > b.s;
    }
};


 

废话就不多说了,直接了解一下代码吧

 

#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
using namespace std;

int N,K,R;

struct note
{
    int u,v,l,cost;
    int next;
}f[10010];

int head[10010];

struct node
{
    int u,s,cost;
    friend bool operator < (node a,node b)
    {
        return a.s > b.s;
    }
};

int bfs()
{
    priority_queue<node>Q;
    node now,tmp;
    now.u = 1;
    now.s = 0;
    now.cost = 0;
    Q.push(now);
    while(!Q.empty())
    {
        now = Q.top();
        Q.pop();
        if(now.u == N)
            return now.s;
        for(int i = head[now.u]; i ; i = f[i].next)        //找到所有和now.s相连的点 
            if(now.cost + f[i].cost <= K)                //注意这里面的i表示的是第几条边 
                {
                    tmp.cost = now.cost + f[i].cost;
                    tmp.u = f[i].v;
                    tmp.s = now.s + f[i].l;
                    Q.push(tmp);
                }
    }
    return -1;
}

int main()
{
    while(~scanf("%d%d%d",&K,&N,&R))
    {
        memset(head,0,sizeof(head));
        for(int i = 1; i <= R; i++)
        {
            scanf("%d%d%d%d",&f[i].u,&f[i].v,&f[i].l,&f[i].cost);
            f[i].next = head[f[i].u];        //存储上一次出现的位置 
            head[f[i].u] = i;            //存储这次出现的位置 
        }
        printf("%d\n",bfs());    
    }    
    return 0;
} 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章