poj 1724 最短路+优先队列(两个约束条件)
/*两个约束条件求最短路,用优先队列*/
#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 最短路+优先队列(两个约束条件)的更多相关文章
- poj 1724(最短路+优先队列)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13436 Accepted: 4921 Descriptio ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- POJ 1724 最短路费用限制
迪杰斯塔拉裸题 最大花费 n个点 m条有向边 起点终点 路径长度 路径花费 问:在花费限制下,最短路径的长度 #include <iostream> #include <string ...
- POJ 1724 (分层图最短路)
### POJ 1724 题目链接 ### 题目大意: 给你 N 个点 ,M 条有向路,走每条路需要花费 C 元,这段路的长度为 L . 给你 K 元,问你能否从 1 走到 N 点且花费不超过 K 元 ...
- poj 3255 Roadblocks 次短路(两次dijksta)
Roadblocks Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...
- poj 3253 Fence Repair 优先队列
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- Heavy Transportation POJ 1797 最短路变形
Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...
- 深搜+剪枝 POJ 1724 ROADS
POJ 1724 ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12766 Accepted: 4722 D ...
- POJ 1475 Pushing Boxes 搜索- 两重BFS
题目地址: http://poj.org/problem?id=1475 两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方. AC代码: #include <iostrea ...
随机推荐
- perl数组的长度与元素个数
perl数组的长度与元素个数 $#数组名 ---表示数组中最后一个元素的下标,它等于元素个数减1. @数组名 ---表示数组中元素的个数. $标量=@数组名 ---将一个数组赋值给一个标量变量,标量得 ...
- Request returned failure status 401
Request returned failure status 401.Invalid OpenStack Identity credentials.
- String Successor(模拟)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3490 题意:给出一个字符串,一个操作次数,每次操作从当前字符串最右边的字符 ...
- [Swift通天遁地]二、表格表单-(7)电子邮件Mail:实现单元格左右滑动调出功能按钮
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Django day29 视图,路由控制,响应器
一:视图 1.视图的几种方式: (1) 第一种 from rest_framework.mixins import ListModelMixin, CreateModelMixin class Pub ...
- idea ssm项目移包报错问题
写完代码之后发现包结构太乱了 想要规划一下 结果报错 这里面的包路径都可以点进去,还是报找不到com.lf.company.entity.Business 后来发现是 在移动前和移动后都存在这个m ...
- 深拷贝js
JSON 最简单的一个 let obj = {} let newObj = JSON.parse(JSON.stringify(obj)) 弊端:这种方法无法拷贝function函数,undefine ...
- Android Unable to add window -- token android.os.BinderProxy@3a067204 is not valid错误分析记录
打开APP时,出现闪退的情况,查看android studio报错信息,主要为: Unable to add window -- token android.os.BinderProxy@3a0672 ...
- csf 课件转化为wmv正常格式
1. 下载csf文件到本地:如下图 2.从下面百度网盘下载到本地: https://pan.baidu.com/s/1BBbgq n85a 3.安装并出现下面图标,点击打开 4. 运行如下图 5. ...
- jQuery——节点操作
创建节点 1.$():创建一个li标签 $("<li class='aaa'>我是li标签</li>") 2.html():创建一个li标签并同时添加到ul ...