题目描述

城市中有R条有向马路,n个马路连接点,通过每条马路都要花去一定费用。你现在在编号为1的连接点 ,手里有k元钱,要去n号连接点的最短路径的长度是多少?途中经过道路的花费不能超过k。注意:两个 马路连接点间可能有多条马路

输入格式

第一行,k(0 <= K <= 10000)

第二行,n(2 <= N <= 100)

第三行,R(1 <= R <= 10000)

以下R行

x y L t 从x到y的马路,长度L(1<=每条马路的长度<=100),花费t(0<=每条马路的费用<=100)

输出格式

满足条件最短路长度


很裸的二维最短路题。

不同于普通最短路的地方是,这里多了一维限制——花费不能超过k。在这个前提下,需要路径最短。

首先,题目并不要求花费最少。所以我们只需要不超过k即可。那么我们在松弛时,只需要判断花费不超过k即可。时间复杂度还是O((N+M)log(N+M))

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define maxn 101
#define maxm 10001
using namespace std; struct edge{
int to,dis,c,next;
edge(){}
edge(const int &_to,const int &_dis,const int &_c,const int &_next){ to=_to,dis=_dis,c=_c,next=_next; }
}e[maxm];
int head[maxn],k;
struct node{
int now,dis,cost;
node(){}
node(const int &_now,const int &_dis,const int &_cost){ now=_now,dis=_dis,cost=_cost; }
bool operator<(const node &x)const{ return dis>x.dis; }
};
priority_queue<node> q;
int n,m,w,ans; inline int read(){
register int x(0),f(1); register char c(getchar());
while(c<'0'||'9'<c){ if(c=='-') f=-1; c=getchar(); }
while('0'<=c&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
} inline void dijkstra(){
q.push(node(1,0,0));
while(q.size()){
int u=q.top().now,d=q.top().dis,c=q.top().cost; q.pop();
if(u==n){ ans=d; return; }
for(register int i=head[u];~i;i=e[i].next){
int v=e[i].to;
if(c+e[i].c<=w) q.push(node(v,d+e[i].dis,c+e[i].c));
}
}
} int main(){
int t=read();
while(t--){
memset(head,-1,sizeof head),k=0,ans=-1;
while(q.size()) q.pop();
w=read(),n=read(),m=read();
for(register int i=1;i<=m;i++){
int u=read(),v=read(),w=read(),c=read();
e[k]=edge(v,w,c,head[u]),head[u]=k++;
}
dijkstra();
printf("%d\n",ans);
}
return 0;
}

SP338 ROADS的更多相关文章

  1. 洛谷 SP338 ROADS - Roads 题解

    思路 dfs(只不过要用邻接表存)邻接表是由表头结点和表结点两部分组成,其中表头结点存储图的各顶点,表结点用单向链表存储表头结点所对应顶点的相邻顶点(也就是表示了图的边).在有向图里表示表头结点指向其 ...

  2. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  3. Jungle Roads[HDU1301]

    Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  4. POJ1947 Rebuilding Roads[树形背包]

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11495   Accepted: 5276 ...

  5. Constructing Roads——F

    F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...

  6. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

  7. 【CodeForces 567E】President and Roads(最短路)

    Description Berland has n cities, the capital is located in city s, and the historic home town of th ...

  8. POJ 1947 Rebuilding Roads

    树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...

  9. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

随机推荐

  1. Java线程池二:线程池原理

    最近精读Netty源码,读到NioEventLoop部分的时候,发现对Java线程&线程池有些概念还有困惑, 所以深入总结一下 Java线程池一:线程基础 为什么需要使用线程池 Java线程映 ...

  2. Maven之继承

    这里我还是将通过一个例子来了解一下Maven继承的初步使用配置.还是使用三个工程项目Project-Parent.Project-C和Project-D来进行说明,三个项目关系如下: <?xml ...

  3. 持久层之 MyBatis: 第三篇 :缓存 And 高级查询

    MyBatis入门到精通3 缓存机制 Mybatis一级缓存测试 Mybatis二级缓存测试 高级查询 表关系说明 一对一查询 一对多查询 多对多查询 缓存机制 正如大多数持久层框架一样,MyBati ...

  4. PHP比较数组、对象是否为空

    PHP简单对比对象.数组是不是为空: 1 <?php 2 /*简单的比较对象和数组是不是为空*/ 3 4 #定义空类EmptyClass 5 class EmptyClass{} 6 7 $em ...

  5. jquery 局部刷新load 某个div或者某个表格

    在使用 ajax 进行删除用户操作的时候,可以在 success 里写一个 window.location.reload(); 让页面刷新. 但是,我不想那样,我只想局部刷新 比如,我删除几个用户后, ...

  6. C#获取时间戳的几种方式

    Console.WriteLine(Convert.ToDouble(DateTime.UtcNow.Ticks - 621355968000000000) / (10 * 1000 * 1000)) ...

  7. 死磕以太坊源码分析之txpool

    死磕以太坊源码分析之txpool 请结合以下代码阅读:https://github.com/blockchainGuide/ 写文章不易,也希望大家多多指出问题,交个朋友,混个圈子哦 交易池概念原理 ...

  8. 将.Net Core发布至Docker,并连接 Redis、上传文件到本机、连接sqlserver数据库

    此片文章目标是将 .Net Core 发布到 Docker 上,并且连接到在 Docker上的 Redis .上传文件到本机文件夹和连接 sqlserver 数据库. 创建项目 创建项目就不用说了,我 ...

  9. 记一次jedis并发使用问题JedisException: Could not return the resource to the pool

    今天线上突然发现个奇怪的问题项目第一次启动的时候redis报错JedisException: Could not return the resource to the pool 直接访问接口的时候不报 ...

  10. hive向es推送数据

    第一步:首先要保证网络是通的,很多公司里子网遍布,要和运维和工程侧同事确认好网络是通的,es的地址可以通过curl es地址的方式测试一下. 第二步:下载需要的jar包,必须的是es-hadoop的包 ...