poj1724 ROADS

来源:互联网 发布:淘宝店招在线设计 编辑:程序博客网 时间:2024/06/06 17:21

大致题意 从一个城市到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
5671 2 2 32 4 3 33 4 2 41 3 4 14 6 2 13 5 2 05 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;}


原创粉丝点击