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 ...
随机推荐
- Linux驱动编程--基于I2C子系统的I2C驱动的Makefile
ifeq ($(KERNELRELEASE),) KERNELDIR ?= /lib/modules/$(shell uname -r)/buildPWD := $(shell pwd) TEST = ...
- linux 安装sysstat使用iostat、mpstat、sar、sa(转载)
使用yum安装 #yum install sysstat sysstat的安装包是:sysstat-5.0.5-1.i386.rpm,装完了sysstat-5.0.5-1.i386.rpm后 就会有i ...
- Informix 物联网应用示例(转)
相关概念 MQTT 是一个物联网传输协议,它被设计用于轻量级的发布/订阅式消息传输,旨在为低带宽和不稳定的网络环境中的物联网设备提供可靠的网络服务.MQTT 是专门针对物联网开发的轻量级传输协议.MQ ...
- IDEA笔记
快捷键: 查找类:ctrl + shif + R (eclipse)查找文件:double shift查找文件中的变量名和方法:ctrl + H (eclipse)system.out:输入 sout ...
- IDEA操作GIT说明
公司的代码库从TFS升级到了GIT,我们的自动化测试代码就需要迁移到git上.操作如下: 1.安装GIT 安装完成后,在IDEA中配置git安装路径 2.在本地磁盘新建一个空目录,例如:D:\Wo ...
- 通过数据绑定模板得到对应的Item控件
这类控件都继承于Selector,其中主要有ComboBox.listview.listbox.datagrid. 由于个人对WPF的了解所有可能有遗漏,希望各位能够指出一起进步. 在遍历上面控件时主 ...
- 1103. Integer Factorization (30)
The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...
- Ubuntu下Apache+php+mysql网站架设详解
目录 1 基础 2 安装 2.1 安装LAMP 2.2 图形化管理软件(可选) 2.2.1 安装webmin 2.2.2 安装phpmyadmin 3 配置文件路径 3.1 常用命令 3.2 配置ap ...
- Setting composer minimum stability for your application
Do you have a confusion of how do you determine the stability when using composer dependency manager ...
- Entity Framework (二) 查询
待完善-------------------------------------- ----------- base 关键字用于从派生类中访问基类的成员: 调用基类上已被其他方法重写的方法. 指定创建 ...