ROADS
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10751 | Accepted: 3952 |
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题意:给出N个城市,然后给出M条单向路,以及每条路的距离和花费,问一个人有K coins,在不超出其money的情况下从城市1到城市n最短的路径是多少,首先可能会想到这是一道二级最短路问题,就是尽量让花费最小,然后让花费最小的基础上让距离最短,但是对于这道题目来说有bug,假如算出的最小花费是cost<k
其对应的最短路是dis[n],可能会存在这样一种情况,还存一种花费cost1,满足cost<cost1<=k,最短路dis1[n]<dis[n];所以不能用这种方法;
所以这道题目要用上优先队列,就是让当到达某个点的时候此时的花费<=k然后就把该点入队,某个点可能会反复入队,出队,然后优先队列保证的是当花费不超过k的情况下优先让距离最近的点出队,然后反复进行,就避免了上述的问题
程序;
#include"stdio.h" #include"string.h" #include"iostream" #include"map" #include"string" #include"queue" #include"stdlib.h" #include"math.h" #define M 109 #define eps 1e-10 #define inf 1000000000 #define mod 1000000000 using namespace std; struct st { int u,v,w,time,next; }edge[M*M*2]; int t,head[M],use[M],dis[M],time[M],k; void init() { t=0; memset(head,-1,sizeof(head)); } void add(int u,int v,int w,int time) { edge[t].u=u; edge[t].v=v; edge[t].w=w; edge[t].time=time; edge[t].next=head[u]; head[u]=t++; } struct node { int id,dis,time; friend bool operator<(node a,node b) { if(a.dis==b.dis) return a.time>b.time; return a.dis>b.dis; } }; int spfa(int S,int n) { int i; priority_queue<node>q; node u; u.id=S; u.dis=u.time=0; q.push(u); while(!q.empty()) { node u=q.top(); q.pop(); if(u.id==n) return u.dis; for(i=head[u.id];i!=-1;i=edge[i].next) { node v; v.id=edge[i].v; if(u.time+edge[i].time<=k) { v.dis=u.dis+edge[i].w; v.time=u.time+edge[i].time; q.push(v); } } } return -1; } int main() { int n,m; while(scanf("%d",&k)!=-1) { scanf("%d%d",&n,&m); init(); while(m--) { int a,b,c,d; scanf("%d%d%d%d",&a,&b,&c,&d); add(a,b,c,d); } int ans=spfa(1,n); printf("%d\n",ans); } return 0; }