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;
} 

 

 

 

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