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最短路)的更多相关文章

  1. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

  2. POJ 1724 ROADS【最短路/搜索/DP】

    一道写法多样的题,很具有启发性. 具体参考:http://www.cnblogs.com/scau20110726/archive/2013/04/28/3050178.html http://blo ...

  3. 深搜+剪枝 POJ 1724 ROADS

    POJ 1724 ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12766   Accepted: 4722 D ...

  4. POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)

    题目链接: https://cn.vjudge.net/problem/POJ-1724 N cities named with numbers 1 ... N are connected with ...

  5. ROADS POJ - 1724(分层最短路)

    就是在最短路的基础上   多加了一个时间的限制 , 多一个限制多一维就好了  记住 分层最短路要用dijistra !!! #include <iostream> #include < ...

  6. poj 1724 ROADS 最短路

    题目链接 n个节点, m条边, 一开始有K这么多的钱, 每条边有len, cost两个属性, 求1到n的最短距离, 花费要小于k. dis数组开成二维的, dis[u][cost]表示到达u花费为co ...

  7. Full Tank? POJ - 3635 (bfs | 最短路)

    After going through the receipts from your car trip through Europe this summer, you realised that th ...

  8. poj 1724 ROADS 很水的dfs

    题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少 链接:http://poj.org/problem?id=1724 直接dfs搜一遍就是 代码: # ...

  9. poj 1724 ROADS 解题报告

    题目链接:http://poj.org/problem?id=1724 题目意思:给出一个含有N个点(编号从1~N).R条边的有向图.Bob 有 K 那么多的金钱,需要找一条从顶点1到顶点N的路径(每 ...

随机推荐

  1. encode_json 会对给定的Perl的数据结构转换为一个UTF-8 encoded, binary string.

    use JSON qw/encode_json decode_json/ ; use Encode; my $data = [ { 'name' => 'Ken' , 'age' => 1 ...

  2. 基于visual Studio2013解决面试题之1007鸡蛋和篮子

     题目

  3. UPC 2959: Caoshen like math 这就是个水题

    http://acm.upc.edu.cn/problem.php?id=2959 这就是个水题,之所以要写这个题是感觉很有纪念意义 用力看就是盲……23333333333333333 这个题就是最小 ...

  4. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: SELECT command denied to user 'xxxx'@''

    这两天项目一直在报这个错误消息: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: SELECT command denied to ...

  5. MyEclipse弹出提示窗体

    MyEclipse弹出提示窗体 1.弹窗例如以下

  6. GDI+ Tutorial for Beginners

    原文 GDI+ Tutorial for Beginners GDI+ is next evolution of GDI. Using GDI objects in earlier versions ...

  7. oracle执行带输入输入参数的存储过程

    declare a1 ); a2 ); begin PKG_INPATIENT.prc_autojf('Y', a1, a2); end;

  8. Spring MVC Cookie example

    In this post we will see how to access and modify http cookies of a webpage in Spring MVC framework. ...

  9. javascript 回调函数应用

    回调函数是什么在学习之前还真不知道js回调函数怎么使用及作用了,下面本文章把我在学习回调函数例子给各位同学介绍一下吧,有需了解的同学不防进入参考. 回调函数原理: 我现在出发,到了通知你”这是一个异步 ...

  10. sqlserver存储过程学习笔记(一)基础知识篇(全)

    说出来有点丢人,做sqlserver应用系统近一年,竟然没有使用过存储过程,现在就好好的梳理一下对应知识,慢慢让其加入到我的项目中去吧. 存储过程的优点:1.运行效率高,提供了在服务器端快速执行sql ...