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 <iostream>
#include <string.h>
#include <cstdio>
#include <queue>
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<fee;
	  return a.len<len;
  }
}pp[N],p1,p2;
priority_queue<point> 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=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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值