POJ 1724 【存在附加约束的最短路问题】【优先队列】
题意:给K个权值。给含有N个点,R条单向边的图。
每条边都有两个权值,其中一个路长,另外一个是附加权值。
要求路的附加权值之和不超过K的情况下求最短路。
思路:
自己的思路太狭隘,这题还是看了大牛的思路。
利用优先队列,在附加权值不超限的前提下,把每个点通向的点都走,每次找出路长最短的点,如果找到第N个点则算法结束。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int k,n,r,ednum;
struct edge
{
int id,w,mon;
edge *next;
};
edge *adj[];
edge edges[];
struct st
{
int id,mon,w;
st(int a,int b,int c){id=a;mon=b;w=c;}
};
struct cmp
{
bool operator()(const st &a,const st &b)
{
return a.w>b.w;
}
};
inline void addEdge(int a,int b,int c,int d)
{
edge *aa;
aa = &edges[ednum];
ednum++;
aa->id=b;
aa->w=c;
aa->mon=d;
aa->next=adj[a];
adj[a]=aa;
}
int solve()
{
priority_queue<st,vector<st>,cmp>q;
for(edge *p=adj[];p;p=p->next)
{
if(p->mon<k)
{
st tmp(p->id,p->mon,p->w);
q.push(tmp);
}
}
while(!q.empty())
{
st tmp=q.top();
q.pop();
if(tmp.id==n)
return tmp.w;
for(edge *p=adj[tmp.id];p;p=p->next)
{
if(p->mon+tmp.mon<=k)
{
st ttmp(p->id,p->mon+tmp.mon,p->w+tmp.w);
q.push(ttmp);
}
}
}
return -;
}
int main()
{
int a,b,c,d;
// ok=0;
scanf("%d%d%d",&k,&n,&r);
for(int i=;i<=r;i++)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
addEdge(a,b,c,d);
}
printf("%d\n",solve());
}
POJ 1724 【存在附加约束的最短路问题】【优先队列】的更多相关文章
- POJ 1062 【带约束的最短路问题】
中文题题意不写. 建图: 我自己想到的建图方式是把每个物品看作两个点,编号分别是i和i+n,然后每个物品两个点之间边的权值是物品本身的价值.然后从第i个点往外连边,目标是可替代品顶点编号较小的点,权值 ...
- POJ 1724 (分层图最短路)
### POJ 1724 题目链接 ### 题目大意: 给你 N 个点 ,M 条有向路,走每条路需要花费 C 元,这段路的长度为 L . 给你 K 元,问你能否从 1 走到 N 点且花费不超过 K 元 ...
- 深搜+剪枝 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 最短路(dijkstra+优先队列)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12436 Accepted: 4591 Description N ...
- ROADS POJ - 1724 约束最短路 暴搜 加剪枝
http://poj.org/problem?id=1724 题意:最短路的模板,不过每条边加上一个费用,要求总费用不超过k 题解:不能用dijkstra ,直接暴力,dfs维护len和cost. 普 ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- poj 1724(最短路+优先队列)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13436 Accepted: 4921 Descriptio ...
- poj 1724 最短路+优先队列(两个约束条件)
/*两个约束条件求最短路,用优先队列*/ #include<stdio.h> #include<string.h> #include<queue> using na ...
随机推荐
- SQLite -插入查询
SQLite -插入查询 SQLite插入语句是用来添加新行数据到数据库中的一个表. 语法: 有两种基本的插入语句的语法如下: INSERT INTO TABLE_NAME (column1, co ...
- SQLite – GLOB子句
SQLite – GLOB子句 .与LIKE不同,GLOB是大小写敏感的,它遵循语法的UNIX指定以下通配符. The asterisk sign (*) The question mark (?) ...
- C++_pthread read-write lock_读写锁_visual studio 2015下配置
pthread下载地址:https://sourceware.org/pthreads-win32/ 1. 项目->属性->VC++目录 包含目录里添加:pthread所在路径\pthre ...
- typescript知识教程
https://ts.xcatliu.com/basics/type-of-function.html
- 按名字寻找文件和文件夹 find命令
find <指定目录> <指定条件> <指定动作> find /home/bnrc/py-faster-rcnn/caffe-fast-rcnn/ -name 'd ...
- 【转】密码学 :CSP的概念
转:[密码学]CSP的概念 CSP加密服务提供者(Cryptographic Service Provider)具有一下几个特点: CSP是真正执行密码运算的独立模块 物理上一个CSP由两部分组成:一 ...
- eclipse android SDK代理跟新
启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manager - Settings』窗口: 在『Andro ...
- ie8兼容性
ie8下不支持css的nth-child()样式解决方法一:使用jQuery的nth-child()方法例:$(".ability-head-list ul li:nth-child(1) ...
- Manjaro/Arch linux 安装输入法
输入命令: sudo pacman -Syu fcitx fcitx-googlepinyin fcitx-im fcitx-configtool 编辑 ~/.xprofile:# xfcitxexp ...
- luogu P1821 Silver Cow Party
题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...