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的路径(每 ...
随机推荐
- wamp安装后打开默认网页显示dir,图标红点
首先网页显示dir,是因为服务这些没启动,跟图标红点是一个原因,解决了图标红点,就能解决只显示dir的问题. 先测试是不是端口占用问题,如图: 如果是,那就继续往下走. 单击图标左键(记住是左键),然 ...
- const对象默认是static的,而不是extern的
const 和 static 变量,可以放在头文件中 const对象默认是static的,而不是extern的,所以即使放在头文件中声明和定义.多个cpp引用同一个头文件,互相也没有感知,所以不会导致 ...
- Windows窗体透明效果
虚拟机里的win7也想实现透明效果, 使用vitrite这个免费软件就可以了.
- MyEclipse弹出提示窗体
MyEclipse弹出提示窗体 1.弹窗例如以下
- ASP.NET - Web.config文件详解
周金桥:asp.net夜话之十一:web.config详解 链接:http://zhoufoxcn.blog.51cto.com/792419/166441/
- Android KeyCode(官方)
Constants public static final int ACTION_DOWN Added in API level 1 getAction() value: the key has be ...
- Linux注销在线用户
与Windows系统类似,Linux系统上也有注销在线用户的方法,我们可以使用pkill命令,详细的步骤如下: . 首先使用w或who命令查看在线用户,确定用户所在TTY [root@iavp232 ...
- hdu4726贪心
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 【thinking in java】读书笔记(一)
近期開始读tij,好记性不如烂笔头,所以还是记录一下,方便以后查阅. 一.各种初始化问题: 方法重载的问题: 方法的重载,差别是靠传入方法的參数,而不是返回值.比方f(),假设是返回值的话,easy产 ...
- Ubuntu 环境安装整理
Ubuntu11.04下Java开发环境搭建和配置 转自:http://guoyunsky.iteye.com/blog/1175861 类似的搭建,网上一搜一大把,但每次去搜索比较麻烦.我这里就整理 ...