题目描述

城市中有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. Kafka客户端编程入门介绍

    1.maven依赖 <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka ...

  2. node目录

    1 [了解nodejs] 1.1 [node简介] 1.2 [node的特点] 1.3 [如何安装] 1.4 [如何运行] 2 [es6--基本语法] 2.1 [变量的解构赋值] 2.2 [解构赋值] ...

  3. 2、MyCat读写分离

    1.主从复制 搭建mycat的读写分离,首先我们现需要搭建mysql的主从复制 [1].Mysql主从复制原理 [2].MySQL主从复制配置 (1).主机配置 修改配置文件:vim /etc/my. ...

  4. VuePress教程之深入理解插件API

    VuePress教程之深入理解插件API 本文目录 1 VuePress教程之深入理解插件API 2 插件 ??? 2.1 暖暖身 2.2 插件如何运作 3 准备 3.1 Markdown 3.2 P ...

  5. Docker 部署 _实现每日情话 定时推送(apscheduler)

    由于最近工作比较忙,后续博客可能更新不及时,哈哈 前言: 由于python对于微信推送不够友好,需要扫码登录,短信接口需要RMB.我就想到了qq邮箱发送到好友,然而微信有qq邮箱提醒功能,就实现了我需 ...

  6. 干货满满,32个常用 Python 实现,你学废了嘛!

    1. 冒泡排序 lis = [56,12,1,8,354,10,100,34,56,7,23,456,234,-58] def sortport(): for i in range(len(lis)- ...

  7. Winform 去掉 最大化 最小化 关闭按钮(不是关闭按钮变灰)终极解决办法

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. Java 中的PO VO DTO BO

    PO 持久对象,数据: BO 业务对象,封装对象.复杂对象 ,里面可能包含多个类:DTO 传输对象,前端调用时传输 :VO 表现对象,前端界面展示. 当你业务足够简单时,一个POJO 也完全当做PO ...

  9. nginx二级域名配置[CentOS]

    目录 背景 域名配置 服务器配置 Nginx配置 页面访问生效 背景 只有一台云服务器,部署了自己写的后端管理系统,又需要部署下自己的个人博客平台,但是只有一个域名,想要合理的利用下二级域名. 域名配 ...

  10. JavaScript 函数节流和函数去抖

    概念 函数防抖(debounce) 当调用动作过n毫秒后,才会执行该动作,若在这n毫秒内又调用此动作则将重新计算执行时间 函数节流(throttle) 预先设定一个执行周期,当调用动作的时刻大于等于执 ...