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 条 ...
随机推荐
- Java流读写
写: package com.wjy.write; import java.io.BufferedWriter; import java.io.FileOutputStream; import jav ...
- Mysql经常使用命令
1.导出整个数据库 mysqldump -u username -p --default-character-set=latin1 数据库名 > 导出的文件名称(数据库默认编码是latin1) ...
- 抢车位中的排名bug(比較使用了无符号数)
昨天把这个发在了qzone,想来还是怪怪的,还是转过来不吧,纯当发现了一个虫子,玩笑一下.只是csdn如今不能贴图,挺郁闷的,原文在http://user.qzone.qq.com/110907073 ...
- 设计模式初探3——装饰者模式(Decorator Pattern)
装饰者模式:动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更有弹性的替代方案. 适用范围: 1. 须要扩展一个类的功能.或给一个类加入附加职责. 2. 须要动态的给一个对象加入功能,这些功 ...
- ALV DataChange EVENT
在CX项目中,根据需求,自定义一个表,维护供应商的银行账号信息,当输入供应商编号时,自动在供应商名称列里自动填写供应商名称,用到了ALV DataChange 事件 ,下面是源代码: *&- ...
- CodeForces 52C Circular RMQ(间隔周期段树,间隔更新,间隔总和)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://codeforces.com/problemset/problem/52/C You are g ...
- HDU 4337 King Arthur's Knights 它输出一个哈密顿电路
n积分m文章无向边 它输出一个哈密顿电路 #include <cstdio> #include <cstring> #include <iostream> usin ...
- codeforces #256 A. Rewards
A. Rewards time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- tolower (Function)
this is a function that Convert uppercase letter to lowercase Converts c to its lowercase equivalent ...
- Linux curl使用简单介绍 (转)
Curl是Linux下一个很强大的http命令行工具,其功能十分强大. 1) 二话不说,先从这里开始吧! $ curl http://www.linuxidc.com 回车之后,www.linuxid ...