/*两个约束条件求最短路,用优先队列*/
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define N 110
struct node {
int u,v,w,f,next;
}bian[N*N*4];
int head[N],yong,money;
void init() {
memset(head,-1,sizeof(head));
yong=0;
}
struct nodee{
int len,w,v;
friend bool operator<(nodee a,nodee b) {
if(a.len==b.len)//在路程相同时,优先选择花费金钱最少的
return a.w>b.w;
return a.len>b.len;
}
};
void addedge(int u,int v,int w,int f) {
bian[yong].u=u;
bian[yong].v=v;
bian[yong].w=w;
bian[yong].f=f;
bian[yong].next=head[u];
head[u]=yong++;
}
int min_len(int s,int t) {
priority_queue<nodee>q;
int visit[N],i;
memset(visit,0,sizeof(visit));
nodee cur,next;
cur.len=0;
cur.w=0;
cur.v=s;
q.push(cur);
while(!q.empty()) {
cur=q.top();
visit[cur.v]=1;
q.pop();
if(cur.v==t)
return cur.len;
for(i=head[cur.v];i!=-1;i=bian[i].next) {
int v=bian[i].v;
next.len=cur.len+bian[i].w;
next.w=cur.w+bian[i].f;
next.v=v;
if(next.w<=money)
q.push(next);
}
}
return -1;
}
int main() {
int n,m,a,b,c,d;
while(scanf("%d%d%d",&money,&n,&m)!=EOF) {
init();
while(m--) {
scanf("%d%d%d%d",&a,&b,&c,&d);
addedge(a,b,c,d);
// addedge(b,a,c,d);没看清题意建成双向边wa了很多次
}
printf("%d\n",min_len(1,n));
}
return 0;
}

poj 1724 最短路+优先队列(两个约束条件)的更多相关文章

  1. poj 1724(最短路+优先队列)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13436   Accepted: 4921 Descriptio ...

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

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

  3. POJ 1724 最短路费用限制

    迪杰斯塔拉裸题 最大花费 n个点 m条有向边 起点终点 路径长度 路径花费 问:在花费限制下,最短路径的长度 #include <iostream> #include <string ...

  4. POJ 1724 (分层图最短路)

    ### POJ 1724 题目链接 ### 题目大意: 给你 N 个点 ,M 条有向路,走每条路需要花费 C 元,这段路的长度为 L . 给你 K 元,问你能否从 1 走到 N 点且花费不超过 K 元 ...

  5. poj 3255 Roadblocks 次短路(两次dijksta)

    Roadblocks Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

  6. poj 3253 Fence Repair 优先队列

    poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...

  7. Heavy Transportation POJ 1797 最短路变形

    Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...

  8. 深搜+剪枝 POJ 1724 ROADS

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

  9. POJ 1475 Pushing Boxes 搜索- 两重BFS

    题目地址: http://poj.org/problem?id=1475 两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方. AC代码: #include <iostrea ...

随机推荐

  1. 确定比赛名次(toposort)

    http://acm.hdu.edu.cn/showproblem.php?pid=1285 #include <stdio.h> #include <string.h> ; ...

  2. 湖南集训day3

    难度:☆☆☆☆☆☆☆ 此时相望不相闻,愿逐月华流照君 /* 23 233 223 啦啦啦德玛西亚 */ #include<iostream> #include<cstdio> ...

  3. P3694 邦邦的大合唱站队/签到题(状压dp)

    P3694 邦邦的大合唱站队/签到题 题目背景 BanG Dream!里的所有偶像乐队要一起大合唱,不过在排队上出了一些问题. 题目描述 N个偶像排成一列,他们来自M个不同的乐队.每个团队至少有一个偶 ...

  4. Akka源码分析-Remote-位置透明

    上一篇博客中,我们研究了remote模式下如何发消息给远程actor,其实无论如何,最终都是通过RemoteActorRef来发送消息的.另外官网也明确说明了,ActorRef是可以忽略网络位置的,这 ...

  5. RHEL6.5设置行号,安装GCC

    vim ~/.vimrc    set nu    set cindent    set tabstop=4    set shiftwidth=4    syntax on 安装gcc,g++编译器 ...

  6. NS2学习笔记(三)

    代理(Agents) 代理是网络层数据包产生和使用的端点,用于实现多层的协议.Agent类部分用OTcl实现,部分用C++实现.C++的代码在~ns/agent.cc和~ns/agent.h中,OTc ...

  7. 题解报告:hdu 1028 Ignatius and the Princess III(母函数or计数DP)

    Problem Description "Well, it seems the first problem is too easy. I will let you know how fool ...

  8. synchronized关键字详解(一)

    synchronized官方定义: 同步方法支持一种简单的策略防止线程干扰和内存一致性错误,如果一个对象对多个线程可见,则对该对象变量的所有读取或写入都是通过同步方法完成的(这一个synchroniz ...

  9. Ch03 React/JSX/Component 簡介

    Facebook 本身有提供 Test Utilities,但由于不够好用,所以目前主流开发社群比较倾向使用 Airbnb 团队开发的 enzyme,其可以与市面上常见的测试工具(Mocha.Karm ...

  10. JS——滚动条

    1.核心思想与之前的拖拽盒子是一样的 2.完全将鼠标在盒子中的坐标给滚动条是错的,因为这样会使滚动条顶部立刻瞬间移动到鼠标位置 3.必须在鼠标按下事件时记住鼠标在滚动条内部的坐标,再将鼠标在盒子中的坐 ...