POJ 1724 ROADS(BFS+优先队列)
题意 : 求从1城市到n城市的最短路。但是每条路有两个属性,一个是路长,一个是花费。要求在花费为K内,找到最短路。
思路 :这个题好像有很多种做法,我用了BFS+优先队列。崔老师真是千年不变的SPFA啊,链接。还有一个神用了好几种方法分析,链接 。
用优先队列控制长度,保证每次加的都是最短的,每次从队列中取元素,沿着取出来的点往下找,如果费用比K少再加入队列,否则不加,这样可以省时间。
//
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue> using namespace std ; int K,N,R ;
int cnt,ans ;
int head[] ;
const int INF = ; struct node
{
int u,v ;
int len ;
int cost ;
int next ;
} st[];
struct node1
{
int len,cost,u ;
friend bool operator < (node1 a,node1 b)
{
return a.len > b.len ;
}
}; void addedge(int u,int v,int len,int cost)
{
st[cnt].u = u ;
st[cnt].v = v ;
st[cnt].len = len ;
st[cnt].cost = cost ;
st[cnt].next = head[u] ;
head[u] = cnt ++ ;
} void bfs()
{
priority_queue<node1>Q ;
struct node1 p ;
p.len = ;
p.cost = ;
p.u = ;
Q.push(p) ;
while(!Q.empty())
{
p = Q.top() ;
Q.pop() ;
if(p.u == N)
{
ans = p.len ;
break ;
//return ans ;
}
for(int i = head[p.u] ; i != - ; i = st[i].next)
{
struct node1 st1 ;
st1.u = st[i].v ;
if(p.cost + st[i].cost <= K)
{
st1.cost = p.cost+st[i].cost ;
st1.len = p.len+st[i].len ;
Q.push(st1) ;
}
}
}
}
int main()
{
int S,D,L,T ;
while(scanf("%d %d %d",&K,&N,&R) != EOF)
{
cnt = ;
memset(head,-,sizeof(head)) ;
for(int i = ; i <= R ; i++)
{
scanf("%d %d %d %d",&S,&D,&L,&T) ;
addedge(S,D,L,T) ;
}
ans = INF ;
bfs() ;
if(ans < INF) printf("%d\n",ans) ;
else printf("-1\n") ;
}
return ;
}
POJ 1724 ROADS(BFS+优先队列)的更多相关文章
- 深搜+剪枝 POJ 1724 ROADS
POJ 1724 ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12766 Accepted: 4722 D ...
- POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)
题目链接: https://cn.vjudge.net/problem/POJ-1724 N cities named with numbers 1 ... N are connected with ...
- POJ 1724 ROADS(bfs最短路)
n个点m条边的有向图,每条边有距离跟花费两个参数,求1->n花费在K以内的最短路. 直接优先队列bfs暴力搞就行了,100*10000个状态而已.节点扩充的时候,dp[i][j]表示到达第i点花 ...
- poj 1724(最短路+优先队列)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13436 Accepted: 4921 Descriptio ...
- POJ:2049Finding Nemo(bfs+优先队列)
http://poj.org/problem?id=2049 Description Nemo is a naughty boy. One day he went into the deep sea ...
- poj 1724 ROADS 很水的dfs
题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少 链接:http://poj.org/problem?id=1724 直接dfs搜一遍就是 代码: # ...
- Meteor Shower POJ - 3669 (bfs+优先队列)
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26455 Accepted: 6856 De ...
- poj 1724 ROADS 解题报告
题目链接:http://poj.org/problem?id=1724 题目意思:给出一个含有N个点(编号从1~N).R条边的有向图.Bob 有 K 那么多的金钱,需要找一条从顶点1到顶点N的路径(每 ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
随机推荐
- C语言 电梯函数
#include <stdio.h> #include <time.h> #include <stdlib.h> void test(){//汉字输出 printf ...
- C# 枚举操作扩展类
using System; using System.Linq; using System.ComponentModel; namespace Demo.Common { /// <summar ...
- 【风马一族_Android】让app上传到Android市场的网站介绍
豌豆荚 开发者中心 http://open.wandoujia.com/account/info China app http://www.chinaapp.org
- Ubuntu 在未知root密码的情况下修改root密码
一, 开机按 F12 (或长按Shift), 进入GRUB界面. 二, 在 recovery mode 按e Ubuntu, Linux 3.5.0-17-generic (恢复模式) (或recov ...
- 转:const“变量”、define的常量和static 变量
首先讲C编译器的内存分配: 代码区 数据区 用户区=线程栈+堆 其中的数据区存储:常量(define)+静态变量(static)+符号集(const)+全局变量 然后讲一下编译的大致顺序: 注释- ...
- TCP连接,传输数据时的粘包问题讨论
第一个需要讨论的大概就是粘包问题了.因为这个是TCP的个性问题,UDP通信时不存在这个问题的.首先看一下什么叫粘包: 客户端采取与服务器的长连接方式建立通信(Open-Write/Read-Write ...
- Vbox安装oracle-linux报错:VT-x features locked or unavailable in M
1.安装完Vbox后,通过vbox来安装oracle-linux时报“VT-x features locked or unavailable in MSR”: 2.报错原因:CPU没有开启虚拟化支持 ...
- VirtualBox中虚拟Ubuntu添加新的虚拟硬盘
VirtualBox中装好Ubuntu后,发现硬盘空间不够使用 了.以下是搜集整理的解决办法: 1. 添加新硬盘 设置 -> Storage -> SATA控制器->右击,选择&qu ...
- Context详解
前言 Context在android中的作用不言而喻,当我们访问当前应用的资源,启动一个新的activity的时候都需要提供Context,而这个Context到底是什么呢,这个问题好像很好回答又好像 ...
- STM32普通定时器实现延时函数
/* SystemFrequency / 1000 1ms中断一次 * SystemFrequency / 100000 10us中断一次 * SystemFrequency / 1000000 1u ...