POJ1724 ROADS(深搜DFS,最短路,dijkstra,用优先队列优化)
来源:互联网 发布:哪种编程语言最厉害 编辑:程序博客网 时间:2024/06/05 16:57
题目:
ROADS
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14825 Accepted: 5370
Description
N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the number of coins).
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.
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 first line of the input contains the integer K, 0 <= K <= 10000, maximum number of coins that Bob can spend on his way.
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 :
Notice that different roads may have the same source and destination cities.
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
The first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins.
If such path does not exist, only number -1 should be written to the output.
If such path does not exist, only number -1 should be written to the output.
Sample Input
5671 2 2 32 4 3 33 4 2 41 3 4 14 6 2 13 5 2 05 4 3 2
Sample Output
11
Source
CEOI 1998
[Submit] [Go Back] [Status] [Discuss]
思路:先说下题意,题意第一行给了一个k,代表你有k的钱数,下一行有一个n,代表n个点,然后一个m,代表m条边,然后接下来m行,每行有四个数,分别代表起点、终点、路径长度和要花费的钱数,题目想问在花的钱不超过k的情况下,从1---n的最短路径是多少。
我们用两种方法来解决这个问题,第一种办法是深搜,深搜的思路就是用邻接表建图,然后搜索这个图一直更新步数的最小值,第二种是用优先队列优化的迪杰斯特拉算法,我们用dis[i][j]表示从点1到点i时花费为j的最短距离,然后用优先队列时路径小的先出队。
代码1(DFS):
#include <cstdio>#include <cstring>#include <cctype>#include <string>#include <set>#include <iostream>#include <stack>#include <map>#include <cmath>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 10000007#define N 100+20#define M 1000000+10#define ll long longusing namespace std;int first[N], vis[N];int k,n,m,len,sum;struct node{ int u,v,w,cost; int next;} g[10200];void add_edge(int u,int v,int w,int cost)//邻接表建图{ g[len].v=v; g[len].w=w; g[len].cost=cost; g[len].next=first[u]; first[u]=len++;}void dfs(int x,int step,int cost){ if(cost>k||step>sum)//剪枝条件,花费大于拥有的钱数和步数大于当前最小的步数 return; if(x==n) sum=min(sum,step);//更新步数最小值 for(int i=first[x]; i!=-1; i=g[i].next) { int v=g[i].v; if(!vis[v]) { vis[v]=1; dfs(v,step+g[i].w,cost+g[i].cost); vis[v]=0; } }}int main(){ len=0; int a,b,c,d; mem(vis,0); mem(first,-1); scanf("%d%d%d",&k,&n,&m); for(int i=0; i<m; i++) { scanf("%d%d%d%d",&a,&b,&c,&d); add_edge(a,b,c,d); } sum=inf; dfs(1,0,0); if(sum==inf) puts("-1"); else printf("%d\n",sum); return 0;}
代码2(Dijkstra):
#include <cstdio>#include <cstring>#include <cctype>#include <string>#include <set>#include <iostream>#include <stack>#include <map>#include <cmath>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 10000007#define N 100+20#define M 1000000+10#define ll long longusing namespace std;int k,n,m,len,sum;int first[N],dis[N][10005];struct node1{ int v,dis,cost,next;} g[10010];struct node{ int num,dis,cost; bool friend operator < (node a,node b)//重载运算符 { return a.dis>b.dis; }};void add_edge(int u,int v,int dis,int cost)//邻接表建图{ g[len].v=v; g[len].dis=dis; g[len].cost=cost; g[len].next=first[u]; first[u]=len++;}void dijkstra(){ for(int i=1; i<=n; i++) for(int j=0; j<=k; j++) dis[i][j]=inf;//dis[i][j]表示从点1到点i花费的钱数为j的最短距离 dis[1][0]=0; priority_queue<node>q; node now,to; now.num=1; now.cost=0; now.dis=0; q.push(now); while(!q.empty()) { now=q.top(); q.pop(); if(now.num==n)//找到的时候直接输出并返回 { printf("%d\n",now.dis); return; } for(int i=first[now.num]; i!=-1; i=g[i].next) { int cost=now.cost+g[i].cost; if(cost>k)continue; if(dis[g[i].v][cost]>now.dis+g[i].dis)//松弛条件 { dis[g[i].v][cost]=now.dis+g[i].dis; to.num=g[i].v; to.cost=cost; to.dis=dis[g[i].v][cost]; q.push(to); } } } puts("-1");}int main(){ len=0; int a,b,c,d; mem(first,-1); scanf("%d%d%d",&k,&n,&m); for(int i=0; i<m; i++) { scanf("%d%d%d%d",&a,&b,&c,&d); add_edge(a,b,c,d); } dijkstra(); return 0;}
0 0
- POJ1724 ROADS(深搜DFS,最短路,dijkstra,用优先队列优化)
- POJ1724---ROADS (最短路变形(邻接表+优先队列))
- 【poj 1724】 ROADS 最短路(dijkstra+优先队列)
- POJ1724 ROADS最短路问题
- hdu 2066最短路 dijkstra 及其优先队列优化
- [最短路]使用优先队列优化的Dijkstra算法
- 【原创】最短路模板 Floyd,优先队列优化dijkstra,SPFA
- POJ1724 ROADS(加限制条件的最短路,深搜)
- 【POJ1724】ROADS 某邪恶最短路
- poj1724--ROADS(最短路变形)
- POJ1724 ROADS 解题报告【最短路/SPFA】
- 优先队列Dijkstra实现最短路算法
- 最短路(Dijkstra+优先队列)
- hdu 2112 最短路 dijkstra优先队列
- hdu2544 最短路(dijkstra/优先队列)
- dijkstra + 优先队列 最短路 [sdut 2143]
- 最短路(floyd+bellman+dijkstra+dijkstra优先队列优化+spfa)
- usaco2.4 comehome 最短路 Floyd, Dijkstra, Dijkstra优先队列优化
- 摄像头和显示屏的接口协议
- 一个简易的web服务器:Tinywebserver
- Java开发环境搭建
- 5-7 12-24小时制(来自pta)
- 杭州android两年开发的面试经历体会
- POJ1724 ROADS(深搜DFS,最短路,dijkstra,用优先队列优化)
- d3.js中选择元素和绑定数据
- 表单验证
- Android Studio:Button控件中英文字母全部默认显示为大写的解决方法
- 如何优雅地使用Sublime Text3
- SQLYog快捷键大全
- laravel5个常用指令
- 【TensorFlow_Fold】深度探究 Blocks for Input
- 用word2016 写CSDN 博客