POJ 1724 最短路费用限制
迪杰斯塔拉裸题
最大花费
n个点
m条有向边
起点终点 路径长度 路径花费
问:在花费限制下,最短路径的长度
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cctype>
#include <queue>
#include <stdlib.h>
#include <cstdlib>
#include <math.h>
#include <set>
#include <vector>
#define inf 107374182
#define N 101
#define M 10001
#define ll int
using namespace std;
inline ll Max(ll a,ll b){return a>b?a:b;}
inline ll Min(ll a,ll b){return a<b?a:b;}
struct Edge{
int f,t,d,w;
int nex;
}edge[M];
int head[N],edgenum;
void addedge(int u,int v,int d,int w){
Edge E={u,v,d,w,head[u]};
edge[edgenum]=E;
head[u]=edgenum++;
} struct node{
int to,dd,use;
node(int a=0,int c=0,int b=0):to(a),dd(c),use(b){}
bool operator<(const node&a)const{
if(a.dd==dd)return a.use<use;
return a.dd<dd;
}
};
int n,maxcost,dis[N];
void spfa(int s,int e){
int i;
for(i=1;i<=n;i++)dis[i]=inf;
dis[s]=0; priority_queue<node>q; while(!q.empty())q.pop();
q.push(node(s,0,0));
while(!q.empty())
{
node temp=q.top(); q.pop();
int u=temp.to,nowcost=temp.use,d=temp.dd;
if(u==e)return;
for(i=head[u];i!=-1;i=edge[i].nex)
{
int v=edge[i].t;
if(nowcost+edge[i].w<=maxcost)
{
if(dis[v]>d+edge[i].d)
dis[v]=d+edge[i].d;
q.push(node(v,d+edge[i].d,nowcost+edge[i].w));
}
}
}
}
int main()
{
int i,m,u,v,d,w;
while(~scanf("%d",&maxcost)){
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head)); edgenum=0; while(m--){
scanf("%d %d %d %d",&u,&v,&d,&w);
addedge(u,v,d,w);
} spfa(1,n);
if(dis[n]==inf)dis[n]=-1;
printf("%d\n",dis[n]);
}
return 0;
}
POJ 1724 最短路费用限制的更多相关文章
- POJ 1724 二维费用最短路
题目大意:有N个城市,编号1-N有R条路,每条路(单向)的起点为Si,终点为Di,长度为Li,如果要走这条路需要花Ti的钱现在你只有K元钱,求在不超支的前提下,从1走到N需要的最短距离 这里总是希望路 ...
- 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 ...
- 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 ...
- Heavy Transportation POJ 1797 最短路变形
Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...
- ROADS POJ - 1724 约束最短路 暴搜 加剪枝
http://poj.org/problem?id=1724 题意:最短路的模板,不过每条边加上一个费用,要求总费用不超过k 题解:不能用dijkstra ,直接暴力,dfs维护len和cost. 普 ...
- poj 1724(有限制的最短路)
题目链接:http://poj.org/problem?id=1724 思路: 有限制的最短路,或者说是二维状态吧,在求最短路的时候记录一下花费即可.一开始用SPFA写的,900MS险过啊,然后改成D ...
- 【poj 1724】 ROADS 最短路(dijkstra+优先队列)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12436 Accepted: 4591 Description N ...
随机推荐
- WIN7_64位系统安装oracle以及PLSQL方法(不用装32位oracle客户端)
一.oracle10g安装,比较简单 1.去Oracle网站下载Vista版的Oracle:Oracle Database 10g Release 2 (10.2.0.4) for Microsoft ...
- Jquery之家5个顶级Material Design框架
谷歌Material Design在如今的前端页面设计中非常流行.Material Design的设计风格向我们展示了一个简单而有内涵的现代UI设计方案. Material Design是如此的简洁美 ...
- Search Insert Position 解答
Question Given a sorted array and a target value, return the index if the target is found. If not, r ...
- JDBC官方用法
JDBC官方用法https://bitbucket.org/xerial/sqlite-jdbc/#markdown-header-usage 代码下载https://github.com/xeria ...
- UGUI 滚动视图
滚动视图是常用的UI控件之一,它是由多个基本控件组合而成.如图 ==================================================================== ...
- .Net语言中关于AOP 的实现详解
来源: IT人家 发布时间: 2011-03-22 20:28 阅读: 3546 次 推荐: 2 原文链接 [收藏] 摘要:该文章主要和大家讲解开发应用系统时在.Net语言中关于AOP ...
- ASP无惧上传类不能上传中文双引号文件及ASP函数InStr存在bug
ASP无惧上传类不能上传中文双引号文件及ASP函数InStr存在bug 近日发现eWebEditor V2.8 asp 版本上传文件文件名不能包含中文双引号,发现eWebEditor使用ASP“无惧上 ...
- nginx错误日志级别
在配置nginx.conf 的时候,有一项是指定错误日志的,默认情况下你不指定也没有关系,因为nginx很少有错误日志记录的.但有时出现问题时,是有必要记录一下错误日志的,方便我们排查问题.error ...
- Android Geocoder(位置解析)
Android中提供GPS定位服务,同时开发者可以对获得的位置信息进行解析,可以获得位置的详细信息. 1.gps定位 在Eclipse中建立android应用程序.android sdk中提供了loc ...
- 第二章实例:ActionBar结合Fragment显示
package tab.test; import com.example.actionbar_tab.R; import android.app.ActionBar; import android.a ...