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 条 ...
随机推荐
- IIS设置允许下载.exe文件解决方法
最近很多客户使用IIS服务器,然后提示返现宝下载无法找到等无法下载的问题. 返现宝是.exe安装文件,部分服务器或主机可能无法下载. 第一.如果是自己服务器或VPS请按如下设置: 1.设置MIME,让 ...
- Spring Data Redis—Pub/Sub(附Web项目源码) (转)
一.发布和订阅机制 当一个客户端通过 PUBLISH 命令向订阅者发送信息的时候,我们称这个客户端为发布者(publisher). 而当一个客户端使用 SUBSCRIBE 或者 PSUBSCRIBE ...
- 基于 Groovy 的自动化构建工具 Gradle 入门(转)
本人工作之初没有使用自动化构建,后来敏捷了,开始使用 Ant - 完全面向过程的定义步骤,不进行依赖管理.再发展到 Maven,面向对象的方式管理工程,有了依赖的管理,JAR 包统一从中央仓库获得,保 ...
- [ACM] POJ 3259 Wormholes (bellman-ford最短路径,推断是否存在负权回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29971 Accepted: 10844 Descr ...
- VC中Tab control的用法
1. 新建一个MFC工程, 取名MyTab, 选择Dialog based, 然后Finish. 2. 删除对话框上默认添加的三个控件. 添加Tab Control控件并在Property属性中设置I ...
- Android 纯代码加入点击效果
项目中非常多的Button, 同一时候配置非常多button切图,Selector是不是非常烦, 使用以下这个类,就能够直接为Button添加点击效果. 不用多个图片,不用Selector. 使用方法 ...
- Emacs经常使用快捷键的注意事项
一直用VIM,尝试了好几次Emacs都被它"多得像天上的星星"一样的快捷键给吓倒了.这几天最终下定决心再次尝试. 将它的Tutor练习了一下,顺便对经常使用快捷键做了一下笔记,方便 ...
- GUI & Event例子
Student No.: _______________ Name: ________________________________________1TK2934 Object-Oriented P ...
- android 项目中设置背景图片
xml文件设置背景图片中:任意一个控件,button imageView 或layout,在其的xml属性设置中,添加 [XML] view plaincopy android:background= ...
- 利用ffmpeg将H264解码为RGB
因为公司买到了一个不提供解码器的设备,我不得已还要做解码的工作.在网上找了一圈,H264解码比較方便的也就是ffmpeg一系列的函数库了,原本设备中也是用这套函数库解码,但厂家不给提供,没办法,仅仅得 ...