POJ 1724 邻接表+优先队列+spfa()

话说这道题昨天就应该ac的,悲剧的是,昨天电脑出了点问题,,耽误了,,,今天重新写了下代码后,ac了。这道题就是用优先队列,图用邻接表存,再用spfa(),时间会减少很多。题目:

ROADS

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7021 Accepted: 2623

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

ac代码:
#include #include #include #include using namespace std; const int N=110; const int 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.fee【POJ 1724 邻接表+优先队列+spfa()】 qq; int totalmoney,numcity,numpath,num,head[M],dis[N]; void addedge(int x,int y,int ll,int v){ ee[num].rp=y; ee[num].value=https://www.it610.com/article/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(){ //freopen("1.txt","r",stdin); while(~scanf("%d",&totalmoney)){ 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; }



    推荐阅读