hdu 三部曲 ROADS

Problem 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
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算法求最短路
*********************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<queue>
 5 using namespace std;
 6 struct  edge
 7 {
 8     int rp,len,wast,next;
 9 }e[10111];
10 struct  point
11 {
12     int sp,len,wast;
13     bool  operator<(const struct point c)const//重载运算符
14       {
15           if(len==c.len)
16            return c.wast<wast;
17           return c.len<len;
18       }
19 }p1,p2;
20 int totalmoney,head[1001],num,dis[1001],numcity,numpath,i,j;
21 void add(int x,int y,int ll,int v)//加入边 链式前向星
22       {
23         e[num].rp=y;
24         e[num].len=ll;
25         e[num].wast=v;
26         e[num].next=head[x];
27         head[x]=num++;
28       }
29 priority_queue<point>qq;
30 void spfa()//求源点到各边的距离
31 {
32     while(!qq.empty())
33       qq.pop();
34     p1.sp=1;p1.len=0;p1.wast=0;
35     qq.push(p1);
36     while(!qq.empty())
37      {
38          p2=qq.top();
39          if((p2.sp)==numcity)
40          {
41              cout<<p2.len<<endl;
42              return;
43          }
44          qq.pop();
45          for(int i=head[(p2.sp)];i!=-1;i=e[i].next)
46           {
47              int y=e[i].rp;
48              if(p2.wast+e[i].wast<=totalmoney)
49                {
50                    p1.len=p2.len+e[i].len;
51                    p1.wast=p2.wast+e[i].wast;
52                    p1.sp=y;
53                    qq.push(p1);
54                }
55           }
56 
57      }
58     cout<<"-1"<<endl;
59     return;
60 }
61 int main()
62 {
63     cin>>totalmoney;
64     memset(head,-1,sizeof(head));
65     cin>>numcity>>numpath;
66     num=0;
67     int x,y,ll,v;
68     for(int i=1;i<=numpath;i++)
69      {
70          cin>>x>>y>>ll>>v;
71          add(x,y,ll,v);
72      }
73      spfa();
74      return 0;
75 }
View Code
 

转载于:https://www.cnblogs.com/sdau--codeants/p/3317606.html

猜你喜欢

转载自blog.csdn.net/weixin_34310127/article/details/93727628