ROADS+dijkstra的灵活运用+POJ
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10742 | Accepted: 3949 |
Description
Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash.
We want to help Bob to find the shortest path from the city 1 to the city N that he can afford with the amount of money he has.
Input
The second line contains the integer N, 2 <= N <= 100, the total number of cities.
The third line contains the integer R, 1 <= R <= 10000, the total number of roads.
Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters :
- S is the source city, 1 <= S <= N
- D is the destination city, 1 <= D <= N
- L is the road length, 1 <= L <= 100
- T is the toll (expressed in the number of coins), 0 <= T <=100
Notice that different roads may have the same source and destination cities.
Output
If such path does not exist, only number -1 should be written to the output.
Sample Input
5
6
7
1 2 2 3
2 4 3 3
3 4 2 4
1 3 4 1
4 6 2 1
3 5 2 0
5 4 3 2
Sample Output
11
解决方式:此题我是这样做的,用上优先队列,在费用可行的情况下,不断松弛路径。事实上也相当于bfs+优先队列。首先路径最短的优先级最高,其次是花费,通过不断的把符合费用要求能到达的点增加优先队列,每次出队即更新能到达的点。最后假设出队的点是N,算法结束,得到的路径既是在花费符合的情况下最短的,这题考察的是能不能深刻理解dijkstra的原理,并运用。
code:#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define MMAX 10003
#define Max 103
using namespace std;
int K,N,R,k;
int head[Max];
struct edge
{ int from,to,len,cost;
int next;
} E[MMAX];
struct stay
{ int dis,cost,x;
bool operator<(const stay &s)const
{
if(dis!=s.dis)
{
return dis>s.dis;
}
else return cost>s.cost; } };
void add(int from,int to,int len,int cost)
{
E[k].from=from;
E[k].to=to;
E[k].len=len;
E[k].cost=cost;
E[k].next=head[from];
head[from]=k++; }
int dijkstra()
{
priority_queue<stay> Q;
stay in;
in.dis=0,in.cost=0,in.x=1;
Q.push(in);
while(!Q.empty())
{
stay out=Q.top(); if(out.x==N) {return out.dis;}
Q.pop();
for(int v=head[out.x]; v!=-1; v=E[v].next)
{
if(out.cost+E[v].cost<=K)
{
stay temp;
temp.x=E[v].to;
temp.dis=out.dis+E[v].len;
temp.cost=out.cost+E[v].cost;
Q.push(temp);
}
}
} }
int main()
{
while(~scanf("%d%d%d",&K,&N,&R))
{
memset(head,-1,sizeof(head));
k=0;
int from,to,len,cost;
for(int i=0; i<R; i++)
{
scanf("%d%d%d%d",&from,&to,&len,&cost);
add(from,to,len,cost);
}
printf("%d\n",dijkstra()); }
return 0;
}
ROADS+dijkstra的灵活运用+POJ的更多相关文章
- Roads in the North POJ - 2631
Roads in the North POJ - 2631 Building and maintaining roads among communities in the far North is a ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- Dijkstra算法:POJ No 3268 Silver Cow Party
题目:http://poj.org/problem?id=3268 题解:使用 priority_queue队列对dijkstra算法进行优化 #include <iostream> #i ...
- 【lightoj-1002】Country Roads(dijkstra变形)
light1002:传送门 [题目大意] n个点m条边,给一个源点,找出源点到其他点的‘最短路’ 定义:找出每条通路中最大的cost,这些最大的cost中找出一个最小的即为‘最短路’,dijkstra ...
- Light oj 1002 Country Roads (Dijkstra)
题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1002 题目描述: 有n个城市,从0到n-1开始编号,n个城市之间有m条边,中 ...
- Codeforces 806 D. Perishable Roads Dijkstra
原文链接https://www.cnblogs.com/zhouzhendong/p/CF806D.html 题目传送门 - CF806D 题意 给定一个 n 个点的无向完全图,每一条边有一定的边权. ...
- Jungle Roads(kruskar)
Jungle Roads 题目链接;http://poj.org/problem?id=1251 Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- Dijkstra with priority queue 分类: ACM TYPE 2015-07-23 20:12 4人阅读 评论(0) 收藏
POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra) //================================================= ...
- 又是图论.jpg
BZOJ 2200 道路和航线重讲ww: FJ 正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到 T 个城镇 (1 ≤ T ≤ 25000),编号为 1 到 T.这些城镇之间通过 R 条 ...
随机推荐
- H264相关随笔
DR(Instantaneous Decoding Refresh)--即时解码刷新. I和IDR帧都是使用帧内预测的.它们都是同一个东西而已,在编码和解码中为了方便,要首个I帧和其他I帧区别开,所以 ...
- Android 一些错误
android fragment里面放viewpager 嵌套fragment 报错: 解决:在adapter的构造方法里加上 super(fragment.getChildFragmentManag ...
- c++ 按行读取txt文本
CStdioFile 类的声明保存在 afx.h 头文件中. CStdioFile 类继承自 CFile 类, CStdioFile 对象表示一个用运行时的函数 fopen 打开的 c 运行时的流式文 ...
- 学习 easyui 之二:jQuery 的 ready 函数和 easyloader 的加载回调函数
Ready 事件不一定 ready 使用 easyloader 的时候,必须要注意到脚本的加载时机问题,easyloader 会异步加载模块,所以,你使用的模块不一定已经加载了.比如下面的代码. &l ...
- ACE定时器
每一秒钟打印一行 http://www.tuicool.com/articles/Zb263e 计时器的打开和关闭封装 http://andylin02.iteye.com/blog/440572 自 ...
- HTTP相关概念
最近观看HTTP权威指南.这本书是一个小更,欲了解更多详细信息,我们不能照顾.但一些基本概念仍然应该清楚.在这里,我整理: HTTP--因特网的多媒体信使 HTTP 使用的是可靠的传输数据协议,因此即 ...
- uva-11234 Expressions
Arithmetic expressions are usually written with the operators in between the two operands (which is ...
- xp硬盘安装Fedora14 过程记录及心得体会(fedora14 live版本680M 和fedora14 DVD版本3.2G的选择)
这次电脑奔溃了,奇怪的是直接ghost覆盖c盘竟然不中.之前电脑上硬盘安装的fedora14操作系统,也是双系统.不知道是不是这个问题,记得同学说过,在硬盘装fedora之后,要手动修改c盘隐藏的那个 ...
- java命令行HPROF Profiler(转)
The HPROF Profiler The Heap and CPU Profiling Agent (HPROF)是JAVA2 SDK自带的一个简单的profiler代理,它通过与Java Vir ...
- SP2010 3D标签云Web部分--很酷的效果,强烈推荐!!
SP2010 3D标签云Web部分--很酷的效果.强烈推荐! ! 项目描述叙事 基于简单Flash的3D标签云Web部件.SP Server 2010使用. 建立在内置标签云Web部件 ...