题目描述

城市中有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. 谁再问Servlet的问题,我就亲自上门来教学了

    1. 概述 在这篇简短的文章中,我们将从概念上理解什么是servlet 和 servlet 容器以及它们是如何工作的. 同时,还能在请求.响应.会话对象.共享变量和多线程的上下文中看到它们的身影. 2 ...

  2. vue第十九单元(mapState mapMutations等方法的使用)

    第十九单元(mapState mapMutations等方法的使用) #课程目标 1.熟练使用mapState 2.熟练使用mapGetters 3.熟练使用mapActions 4.熟练使用mapM ...

  3. DRF类视图让你的代码DRY起来

    刚开始写views.py模块的代码,一般都是用def定义的函数视图,不过DRF更推荐使用class定义的类视图,这能让我们的代码更符合DRY(Don't Repeat Yourself)设计原则: 使 ...

  4. Flink如何做维表关联?

    使用 RichAsyncFunction 加 CacheBuilder CacheBuilder.newBuilder() //最多存储10000条 .maximumSize(10000) //过期时 ...

  5. C#数据结构-赫夫曼树

    什么是赫夫曼树? 赫夫曼树(Huffman Tree)是指给定N个权值作为N个叶子结点,构造一棵二叉树,若该树的带权路径长度达到最小.哈夫曼树(也称为最优二叉树)是带权路径长度最短的树,权值较大的结点 ...

  6. ElasticSearch 史上最全文章

    老规矩,本篇文章 不做 ElasticSearch 的 编码讲解 ,只介绍 文章学习的一些优秀文章 重点在于不要循规蹈矩,教程 这样走,你不一定要按他这样走,按自己的方式来,学习效率会更高,网上的教程 ...

  7. Logistic 回归-原理及应用

    公号:码农充电站pro 主页:https://codeshellme.github.io 上一篇文章介绍了线性回归模型,它用于处理回归问题. 这次来介绍一下 Logistic 回归,中文音译为逻辑回归 ...

  8. matplotlib学习日记(七)---误差棒图

    (一)误差棒图----误差置信区间的表示 import matplotlib.pyplot as plt import numpy as np x = np.linspace(0.1, 0.6, 10 ...

  9. 痞子衡嵌入式:MCUXpresso IDE下SDK工程导入与workspace管理机制

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是MCUXpresso IDE下SDK工程导入与workspace管理机制. MCUXpresso IDE是恩智浦软件团队倾注很大心血研发 ...

  10. 【代码周边】npm是What?

    社区 程序员自古以来就有社区文化: 社区的意思是:拥有共同职业或兴趣的人们,自发组织在一起,通过分享信息和资源进行合作.虚拟社区的参与者经常会在线讨论相关话题,或访问某些网站.前端程序员也有社区,世界 ...