poj1724 ROADS

大致題意 從一個城市到N城市,有多條路徑可以選擇,每條路徑有對應的錢數,問是否能用不超過K的硬幣數到達目的地。

思路:dijkstra+heap的應用,加上A*的約束條件。需要對dijkstra有很好的理解,蠻像dfs的。改普通的dij爲:只要一邊起點的當前花費錢數 + 這條邊的花費錢數 <= coin,就讓這邊終點的信息加入heap.(普通的dij是終邊的距離有變小才讓其加入heap)。這樣的話,就可能有很多的同一個城市的不同點(距離,花費不同)出現在heap中,這時,根據heap的性質,首先出列的必然是dis最小的點,這就保證了首先得到的必是滿足c <= coin的到這城市的最短路徑。

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
#include<string.h>
#include<queue>
#include<stdio.h>
using namespace std;
struct
{
    int v,w,c,next;
} edge[10005];
struct node
{
    int u,c,dis;
    bool operator < (node a)const
    {
        return a.dis<dis;
    }
};
int n,m,coin,k=1,head[105];

int bfs()
{
    node a;
    priority_queue<node>q;
    a.u=1;
    a.dis=0;
    a.c=0;
    q.push(a);
    while(!q.empty())
    {
        node now=q.top();
        q.pop();
        if(now.u==n)return now.dis;//第一個出列的比爲符合約束條件的最短路徑

        for(int i=head[now.u]; i!=0; i=edge[i].next)  //約束條件下的dijkstra
        {
            if(now.c+edge[i].c<=coin)
            {
                a.u=edge[i].v;          
                a.dis=edge[i].w+now.dis;
                a.c=edge[i].c+now.c;
                q.push(a);
            }
        }
    }
    return -1;
}


int main()
{
    int u,v,w,c,m;
    while(~scanf("%d%d%d",&coin,&n,&m))
    {
        memset(head,0,sizeof(head));
        while(m--)
        {
            scanf("%d%d%d%d",&u,&v,&w,&c);
            edge[k].v=v;
            edge[k].w=w;
            edge[k].c=c;
            edge[k].next=head[u];
            head[u]=k++;
        }
        printf("%d\n",bfs());
    }
    return 0;
}


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