POJ ROADS DFS BFS 优先队列 入门

来源:互联网 发布:朝鲜导弹知乎 编辑:程序博客网 时间:2024/05/17 23:24

ROADS
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14585 Accepted: 5288

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. 

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 : 
  • 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. 

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



用dfs去暴力,不用存路径,把所有结果遍历一遍就好。

存数据用的数组模拟链表,里面加个next,记录和他有相同起始点的位置,这样比用链表更快,但有时候会浪费内存,ac中内存一般都是够的。

ac代码:

#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>#include <queue>#define INF 0x3f3f3f3fusing namespace std;int k,n,r,ans;struct node{int s,d,l,t,next;}nn[1000005];int head[105],vis[105];void dfs(int id,int road,int cost){//printf("*********\n");if(road >= ans)return ;if(cost > k)//花费太高 return ; if(id==n && cost<=k && road < ans)//{ans=road;return ;}for(int i=head[id];i!=-1;i=nn[i].next){int v=nn[i].d;//printf("%d#######\n",v);if(!vis[v]){vis[v]=1;dfs(v,road+nn[i].l,cost+nn[i].t);vis[v]=0;}}}int main(){int i,j;while(~scanf("%d%d%d",&k,&n,&r)){memset(head,-1,sizeof(head));ans=INF;for(i=1;i<=r;i++){scanf("%d%d%d%d",&nn[i].s,&nn[i].d,&nn[i].l,&nn[i].t);nn[i].next = head[nn[i].s];head[nn[i].s]=i;}memset(vis,0,sizeof(vis));vis[1]=1;dfs(1,0,0);printf("%d\n",ans);}return 0;}


在说一下BFS+优先队列的解法,先用数组链表实现存储,然后用bfs去遍历,把每一个点的后继点都给遍历上,然后发放入优先队列中( 优先队列的原则是路径小的放前面,如果路径一样,那么花费少的放前面),每次取出最优的,就能达到最优的。

node2结构体里面的重载,有两种方法,其中一种注释掉了,两种都是可以的,有人问为什么用const,是因为都是静态的,不能修改用他们的值,另一种重载用friend 是因为他要用其他结构体的数据,所以必须friend.

ac代码:

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <queue>#define INF 0x3f3f3f3fusing namespace std;struct node1{int s,d,l,t,next;}nn[10010];struct node2{int d,l,t;/*friend bool operator < (node2 x,node2 y){    if(x.l==y.l)            return x.t > y.t;        return x.l > y.l;}*/bool operator < (const node2 &a)const{        if(a.l==l)            return a.t<t;        return a.l<l;}};int k,n,r,head[110];void bfs(){priority_queue<node2>q;node2 tmp;int ans;    while(!q.empty())        q.pop();tmp.d=1;tmp.l=0;tmp.t=0;q.push(tmp);    ans=INF;while(!q.empty()){tmp=q.top();q.pop();if(tmp.d == n && tmp.l<=ans && tmp.t<=k){ans=tmp.l;break ;}for(int i=head[tmp.d];i!=-1;i=nn[i].next){node2 now;now.d=nn[i].d;now.l=tmp.l + nn[i].l;now.t=tmp.t + nn[i].t;if(now.t <= k){                q.push(now);}}}if(ans == INF){    printf("-1\n");}else{printf("%d\n",ans);}}int main(){while(~scanf("%d%d%d",&k,&n,&r)){memset(head,-1,sizeof(head));for(int i=1;i<=r;i++){scanf("%d%d%d%d",&nn[i].s,&nn[i].d,&nn[i].l,&nn[i].t);nn[i].next = head[nn[i].s];head[nn[i].s]=i;}bfs();}return 0;}



1 0