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 ...
随机推荐
- mplayer-for-windows change color scheme in win 7
Q: When I play movie on Windows7, always comes this message: The color scheme has been changed The f ...
- .NET4.5可以给所有线程设置默认的Culture了
How to set CurrentCulture for all threads in a domain in .NET 4.5 Before .NET 4.5 if we wanted to se ...
- eclipse 最全快捷键(网络收集)
Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt+ ...
- php中的in_array分析及其替换方法
php中的in_array函数效率分析 http://www.jb51.net/article/41446.htm http://www.server110.com/php/201309/1150.h ...
- Node.js学习资料整理
了解node,node主要能干啥? Node.js究竟是什么?:http://www.ibm.com/developerworks/cn/opensource/os-nodejs/ nodejs教程: ...
- Entity Framework Code First 常用方法集成
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using S ...
- 用委托在listbox中异步显示信息,解决线程间操作无效,从不是创建控件的线程访问它
//创建一个委托,是为访问listbox控件服务的. public delegate void UpdateTxt(string msg); //定义一个委托变量 public UpdateTxt u ...
- protobuf编译报错
在下载protobuf进行编译的时候会出现如图所示的错误 修改 C:\protobuf-2.4.1\gtest\include\gtest\internal\gtest-tuple.h(C:是我解压p ...
- php编写简单的页面跳转功能
不多说,直接上. //确保magic_quotes_gpc在php.ini中移开启function CheckInput($value){ //去除反斜杠 if(get_magic_quotes_gp ...
- openerp 常见问题 OpenERP在哪储存附件?(转载)
OpenERP在哪储存附件? 原文地址:http://cn.openerp.cn/where_to_store_attachement_in_openerp_7/ 我们知道对OpenERP中的每个内部 ...