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 条 ...
随机推荐
- Android应用开发-小巫CSDN博客clientJsoup篇
Android应用开发-小巫CSDN博客clientJsoup篇 距上一篇博客已经过去了两个星期,小巫也认为很抱歉,由于在忙着做另外一个项目,差点儿抽不出空来,这不小巫会把剩下的博文全部在国庆补上.本 ...
- dede 标签
◆织梦内容管理系统模板标签代码参考 [Arclist 标记] 这个标记是DedeCms最常用的一个标记,也叫自由列表标记,其中 hotart.coolart.likeart.artlist.imgl ...
- [欧拉回路] hdu 3018 Ant Trip
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3018 Ant Trip Time Limit: 2000/1000 MS (Java/Others) ...
- HTTPDNS成为移动互联网的标配–原因与原理解析(转)
DNS,作用就是将域名解析成IP.一个DNS查询,先从本地缓存查找,如果没有或者已经过期,就从DNS服务器查询,如果客户端没有主动设置DNS服务器,一般是从服务商DNS服务器上查找.这就出现了不可控. ...
- hdu1507最大匹配
题意:给出n*m的矩阵方块,某些方块是ponds,这些方块不能出售,然后能出售的是1*2的方块 要求求出有多少块1*2的方块能够出售,并输出这些方块的坐标(specia judge) 关键是怎么建出二 ...
- WPF对于xml的简单操作(下)绑定ListView
上个月做好的,电脑给盗了,没及时存在网盘,也及时发到随笔,于是乎悲哉!搞了一个上午终于绑定好了,有时候就是这么眼瞎,Path和XPath全瞎了,摸滚了一个上午,赶紧的随笔跟上先. <ListVi ...
- MATLAB导入数据importdata功能
用load函数导入mat文件大家都会.可是今天我拿到一个数据,文件后缀名竟然是'.data'.该怎么读呢? 我仅仅好用matlab界面Workspace区域的"import data&quo ...
- Lua学习 1) —— Android呼叫变量值和分配
2014-07-08 Lua脚本语言,嵌入在App中扩展开发是非常不错的. 关于Android与Lua的环境搭配,我直接下载别人编好的.so与.jar(放到libs下就好了) 以下简介一下Androi ...
- 键盘控制div上下左右移动 (转)
<html> <head> <title></title> <link rel="stylesheet" type=" ...
- Gradle cookbook(转)
build.gradle apply plugin:"java" [compileJava,compileTestJava,javadoc]*.options*.encoding ...