POJ 1724 ROADS(bfs最短路)
n个点m条边的有向图,每条边有距离跟花费两个参数,求1->n花费在K以内的最短路。
直接优先队列bfs暴力搞就行了,100*10000个状态而已。节点扩充的时候,dp[i][j]表示到达第i点花费为j时的最短路。没加优化16ms过,不知道discuss里面说bfs超时是怎么回事。。。。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<bitset>
#include<vector>
#include<string>
#include<cstdio>
#include<cmath>
#include<stack>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define REP(i, n) for(int i=0; i<n; i++)
#define CLR(a, b) memset(a, b, sizeof(a))
#define debug puts("**debug**")
#define LL long long
#define PB push_back
#define eps 1e-10
using namespace std; const int maxn = 111;
const int INF = 1e9;
int n, m, K, dp[maxn][10001];
struct Edge
{
int from, to, dist, cost;
};
vector<int> G[maxn];
vector<Edge> edges; void init()
{
FF(i, 1, n+1)
{
G[i].clear();
REP(j, K+1)
{
if(i == 1) dp[i][j] = 0;
else dp[i][j] = INF;
}
}
edges.clear();
}
void add(int u, int v, int d, int c)
{
edges.PB((Edge){u, v, d, c});
int nc = edges.size();
G[u].PB(nc-1);
} struct Node
{
int u, d, c;
bool operator < (const Node& rhs) const
{
return d > rhs.d;
}
}; int bfs()
{
priority_queue<Node> q;
q.push((Node){1, 0, 0});
while(!q.empty())
{
Node x = q.top(); q.pop();
if(x.u == n) return x.d;
int nc = G[x.u].size();
REP(i, nc)
{
Edge e = edges[G[x.u][i]];
if(e.cost + x.c > K) continue;
if(dp[e.to][x.c+e.cost] > x.d+e.dist)
{
dp[e.to][x.c+e.cost] = x.d+e.dist;
q.push((Node){e.to, x.d+e.dist, x.c+e.cost});
}
}
}
return -1;
} int main()
{
while(~scanf("%d%d%d", &K, &n, &m))
{
init();
int a, b, c, d;
REP(i, m)
{
scanf("%d%d%d%d", &a, &b, &c, &d);
if(a != b) add(a, b, c, d);
}
printf("%d\n", bfs());
}
return 0;
}
POJ 1724 ROADS(bfs最短路)的更多相关文章
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- POJ 1724 ROADS【最短路/搜索/DP】
一道写法多样的题,很具有启发性. 具体参考:http://www.cnblogs.com/scau20110726/archive/2013/04/28/3050178.html http://blo ...
- 深搜+剪枝 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 ...
- ROADS POJ - 1724(分层最短路)
就是在最短路的基础上 多加了一个时间的限制 , 多一个限制多一维就好了 记住 分层最短路要用dijistra !!! #include <iostream> #include < ...
- poj 1724 ROADS 最短路
题目链接 n个节点, m条边, 一开始有K这么多的钱, 每条边有len, cost两个属性, 求1到n的最短距离, 花费要小于k. dis数组开成二维的, dis[u][cost]表示到达u花费为co ...
- Full Tank? POJ - 3635 (bfs | 最短路)
After going through the receipts from your car trip through Europe this summer, you realised that th ...
- poj 1724 ROADS 很水的dfs
题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少 链接:http://poj.org/problem?id=1724 直接dfs搜一遍就是 代码: # ...
- poj 1724 ROADS 解题报告
题目链接:http://poj.org/problem?id=1724 题目意思:给出一个含有N个点(编号从1~N).R条边的有向图.Bob 有 K 那么多的金钱,需要找一条从顶点1到顶点N的路径(每 ...
随机推荐
- 不用SWIG,Go使用C++代码的方式
将C++代码用C作一次封装,就可以让Go调用了. 这是一个C++头文件: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #ifndef CGO_CPPGO_C ...
- Troubleshooting:lvm调整分区时“Error parsing metadata for VG fedora”的解决
磁盘满了,由于使用的是lvm,想要扩容时发现无论lvs还是lvdisplay这类命令都会报同样的错: [root@localhost qwang]# lvs Parse error at (line ...
- One simple health check for oracle with sql
There are some sqls which is used for check the oracle database's health condition. ------numbers of ...
- Python文件处理(1)
读取文件 解决方案: 最简单的就是一次性读取所有的内容放在一个大字符串中 all_the_text=open('thefile.txt').read() all_the_data=open('abin ...
- CentOS 配置 ssh
默认安装ssh是有的.只是hosts访问问题. 1.在hosts.deny文件尾添加sshd:ALL意思是拒绝所有访问请求 [root@localhost ~]# vi /etc/hosts.de ...
- SVN 1.8.x 服务器安装(转)
目录[-] 一. SVN 服务器下载.安装 1. 关于 Subversion 1.8.11 2. 兼容性问题 3. 安装包下载 3.1 VisualSVN 3.2 Win32Svn 3.3 SlikS ...
- Eclipse更改默认工作目录的方法
参考: Eclipse更改默认工作目录的方法:http://blog.163.com/take_make/blog/static/208212210201272611406227/ 用记事本打开&qu ...
- PHP开发经验中介(thinkphp3.2使用技巧)
1.在模板中截取字符串 {$vo.create_date|mb_substr=###,0,10,'utf-8'}
- Spring3表达式语言(SpEL)学习笔记
最新地址请访问:http://leeyee.github.io/blog/2011/06/19/spring-expression-language Spring Excpression Langua ...
- css 水平居中的办法
<div style="width: 100%; text-align: center; margin: auto;"> <div style="dis ...