ROADS
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10804 | Accepted: 3976 |
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.
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 :
Notice that different roads may have the same source and destination cities.
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.
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
思路:邻接表存之 优先队列 spfa松弛过程中用优先队列(边的长度短的优先)来存储边,将符合条件(coins限制)的边都加入优先队列;
直到找到延伸到最后一个顶点即可终止循环;因为最先到达的一定是最短路径,在coins的限制条件下;
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <stack> #include <queue> #include <set> #include <vector> using namespace std; #define N 205 #define M 10010 struct edge{ int rp,value,cost,next; }ee[M]; struct point{ int sp,len,fee; bool operator<(const struct point a)const{ if(a.len!=len) return a.len<len; else return a.fee<fee; } }pp[N],p1,p2; priority_queue<point>qq; int totalmoney,numcity,numpath,num,head[M],dis[M]; void addedge(int x,int y,int ll,int v){ ee[num].rp=y; ee[num].value=ll; ee[num].cost=v; ee[num].next=head[x]; head[x]=num++; } void spfa(){ while(!qq.empty()){ qq.pop(); } p1.fee=0; p1.len=0; p1.sp=1; qq.push(p1); while(!qq.empty()){ p2=qq.top(); qq.pop(); int x=p2.sp; if(x==numcity){ printf("%d\n",p2.len); return ; } for(int i=head[x];i!=-1;i=ee[i].next){ int y=ee[i].rp; if(p2.fee+ee[i].cost<=totalmoney){ p1.fee=p2.fee+ee[i].cost; p1.len=p2.len+ee[i].value; p1.sp=y; qq.push(p1); } } } printf("-1\n"); return ; } int main(){ while(scanf("%d",&totalmoney)!=EOF){ num=0; memset(head,-1,sizeof(head)); scanf("%d%d",&numcity,&numpath); int x,y,ll,v; for(int i=1;i<=numpath;i++){ scanf("%d %d %d %d",&x,&y,&ll,&v); addedge(x,y,ll,v); } spfa(); } return 0; }