邻接表(稀疏图)

1.题目引入:

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. 

题目大意:从1到总收费小于或等于K硬币的城市N的最短路径的总长度。如果不存在这样的路径,则只应将数字-1。

很显然本题可以使用Dijkstra算法,但显然这个算法的时间复杂度达到了O(N*N),每次找到离1号顶点的时间复杂度为O(N),对于这种边数M远小于N*N的稀疏图,这里我们可以使用邻接表使时间复杂度优化到(M+N)log(N),尽管对于稠密图来说这可能相当于没有优化,但对于稀疏图来说就方便很多。相当于用链表将从i出发的所有边都进行保存,最后达到从一个顶点到另一顶点的(最短,长)路程。

2.样例输出: 

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

3.代码如下: 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
struct dd
{
	int s,d,l,t;
}q[10010];
int head[10010],next[10010],book[10010];
int m,n,k,minn;
void dfs(int x,int s,int num)
{
	int i;
	if(x==n)
	{
		if(s<minn)
		minn=s;
		return;
	}
	for(i=head[x];i!=-1;i=next[i])  //搜索每一个顶点的边 
	{
		if(!book[q[i].d]&&s+q[i].l<minn&&num-q[i].t>=0) //该条道路没有走过且最短路径的长度小于无穷大且资金充足 
		{
			book[q[i].d]=1;                //标记走过的这座城市 
			dfs(q[i].d,s+q[i].l,num-q[i].t);	//更新起始城市,所走道路总数,所剩钱数 
			book[q[i].d]=0;  //如果一个数到达死胡同但还没有到达目的地需要退回上一步 
		}
	 } 
	 return;
}
int main()
{
	//k表示钱数,n表示城市数,m表示道路数 
	while(~scanf("%d%d%d",&k,&n,&m))
	{
		minn=999999; 
		memset(head,-1,sizeof(head));
		memset(book,0,sizeof(book));
		for(int i=0;i<m;i++)
		{
			scanf("%d%d%d%d",&q[i].s,&q[i].d,&q[i].l,&q[i].t);
			//s表示源城市,d表示末城市,l为路长,t为花费的钱数 
			next[i]=head[q[i].s];   //存储编号为i的边的下一条边的编号 
			head[q[i].s]=i;        //保存顶点为q[i].s的第一条边的编号 
		}
		 book[1]=1;
		 dfs(1,0,k);
		 if(minn==999999)
		{
		 	printf("-1\n");
		}
		else
		{
			printf("%d\n",minn);
		 } 
	}
	return 0;
} 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风遥~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值