poj 1724~ROADS(dijkstra+优先队列)

来源:互联网 发布:想给自己淘宝店铺刷单 编辑:程序博客网 时间:2024/05/16 23:42
ROADS
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14776 Accepted: 5352

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

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

Source

CEOI 1998
这题搜索可以写,没想到,入了最短路的坑,不过代码跑得快
开一个二维数组dis[v][cost]存距离,cost表示从1到达这个点,花这么多钱的时候距1多远,那么,到达每一个点都有一个cost与之对应,然后判断,不断松弛即可;用优先队列优化了一下
#include <iostream>#include<stdio.h>#include<string.h>#include<string>#include<algorithm>#include<stdlib.h>#include<queue>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3fusing namespace std;int n,m,k;struct node{    int num,dis,cost;    bool friend operator < (node a,node b)    {        return a.dis>b.dis;    }};int dis[105][10005];int u[10005],v[10005],w[10005],c[10005];int first[105],nex[10005];void spfa(){    for(int i=1;i<=n;i++)    {        for(int j=0;j<=k;j++)        {            dis[i][j]=inf;        }    }    dis[1][0]=0;    priority_queue<node>q;    node a,b;    a.num=1;    a.cost=0;    a.dis=0;    q.push(a);    while(!q.empty())    {        a=q.top();        q.pop();        if(a.num==n)        {            printf("%d\n",a.dis);            return;        }        for(int i=first[a.num];i!=-1;i=nex[i])        {            int cost=a.cost+c[i];            if(cost>k)continue;            if(dis[v[i]][cost]>a.dis+w[i])            {            dis[v[i]][cost]=a.dis+w[i];            b.num=v[i];            b.cost=cost;            b.dis=dis[v[i]][cost];            q.push(b);            }        }    }    printf("-1\n");}int main(){    while(~scanf("%d",&k))    {        scanf("%d",&n);        scanf("%d",&m);        mem(first,-1);        for(int i=0;i<m;i++)        {            scanf("%d%d%d%d",&u[i],&v[i],&w[i],&c[i]);            nex[i]=first[u[i]];            first[u[i]]=i;        }        spfa();    }    return 0;}





0 0
原创粉丝点击