有限制的最短路spfa+优先队列
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10751 | Accepted: 3952 |
Description
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 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
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;
}
有限制的最短路spfa+优先队列的更多相关文章
- 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)
关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...
- L - Subway(最短路spfa)
L - Subway(最短路spfa) You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. In ...
- hdu 1874(最短路 Dilkstra +优先队列优化+spfa)
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 四点之间最短路(spfa+优先队列+枚举优化)UESTC1955喜马拉雅山上的猴子
喜马拉雅山上的猴子 Time Limit: 1000 MS Memory Limit: 256 MB Submit Status 余周周告诉我喜马拉雅山上有猴子,他们知道点石成金的方法.我不信 ...
- ACM学习历程—HDU 2112 HDU Today(map && spfa && 优先队列)
Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线 ...
- POJ1722二维spfa+优先队列优化
题意: 给你一个有向图,然后求从起点到终点的最短,但是还有一个限制,就是总花费不能超过k,也就是说每条边上有两个权值,一个是长度,一个是花费,求满足花费的最短长度. 思路: 一开 ...
- ACM/ICPC 之 最短路-SPFA+正逆邻接表(POJ1511(ZOJ2008))
求单源最短路到其余各点,然后返回源点的总最短路长,以构造邻接表的方法不同分为两种解法. POJ1511(ZOJ2008)-Invitation Cards 改变构造邻接表的方法后,分为两种解法 解法一 ...
- hdu 4784 Dinner Coming Soon(spfa + 优先队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4784 思路:建图,对于同一个universe来说,就按题目给的条件相连,对于相邻的universe,连 ...
- POJ 1847 Tram --set实现最短路SPFA
题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...
随机推荐
- C++:在堆上创建对象,还是在栈上?
这篇文章来自于一次讨论:http://www.devbean.net/2013/01/qt-study-road-2-model-view/#comment-17532.关于究竟是在堆上还是在栈上创建 ...
- 初识EseNt
转自:http://www.cnblogs.com/goosao/archive/2011/09/23/2186412.html 一.什么是EseNtEseNt(Extensible Storage ...
- 多媒体开发之rtcp详解---rtcp数据包
http://www.360doc.com/content/13/0606/10/1317564_290865866.shtml http://blog.csdn.net/hrbeuwhw/artic ...
- endl的读法
endl是“end line”的缩写,所以它应该念作“endELL”而不是“endONE”.
- linux 访问远程务器代码
比如用SSH 访问远程 登陆名为hadoop 的IP为192.168.1.35的主机,则用ssh hadoop@192.168.1.35,然后依据提示输入密码即可.
- SharePoint 2013 网站迁移流程
在新的Farm(场)里,创建一个新的Web Application(网站应用程序),不需要创建Site Collection(网站集) Copy(复制)自定义开发的WSP包到新的Farm Server ...
- 对ChemDraw Prime 16.0你了解多少
ChemDraw Prime 16.0应用是化学智能绘图程序的行业领导者.除了创建符合出版标准的绘图,化学家们可以使用ChemDraw Prime软件预测性能,搜索数据库等来节省时间,提高数据的准确性 ...
- [转] CSocket 和CAsyncSocket类介绍
微软的MFC把复杂的WinSock API函数封装到类里,这使得编写网络应用程序更容易. CAsyncSocket类逐个封装了WinSock API,为高级网络程序员提供了更加有力而灵活的方法.这个类 ...
- oracle之trunc(sysdate)
--截取后得到的仍为date数据类型 select trunc(sysdate) from dual;--2017-03-13 00:00:00select trunc(sysdate+1) from ...
- Spring------SpringBoot参考书籍
转载: http://download.csdn.net/download/plus_dy/8972653