ROADS POJ - 1724(限制条件的最短路)【邻接表+深搜】
?
思路:先說下題意,題意第一行給了一個(gè)k,代表你有k的錢數(shù),下一行有一個(gè)n,代表n個(gè)點(diǎn),然后一個(gè)m,代表m條邊,然后接下來m行,每行有四個(gè)數(shù),分別代表起點(diǎn)、終點(diǎn)、路徑長(zhǎng)度和要花費(fèi)的錢數(shù),題目想問在花的錢不超過k的情況下,從1---n的最短路徑是多少。
我們用兩種方法來解決這個(gè)問題,第一種辦法是深搜,深搜的思路就是用鄰接表建圖,然后搜索這個(gè)圖一直更新步數(shù)的最小值,第二種是用優(yōu)先隊(duì)列優(yōu)化的迪杰斯特拉算法,我們用dis[i][j]表示從點(diǎn)1到點(diǎn)i時(shí)花費(fèi)為j的最短距離,然后用優(yōu)先隊(duì)列時(shí)路徑小的先出隊(duì)。
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
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 2Sample Output
11 #include<iostream> #include<string.h> #define inf 0x3f3f3f3f using namespace std;int a,b,c,ans,s[10010],d[10010],l[10010],t[10010],u[10010],v[10010],flag,book[10010];void dfs(int x,int y,int z)/*x:城市;y:長(zhǎng)度;z:money*/ {if(y>=ans||z>a)/*達(dá)到錢夠用,路程最小,循環(huán)遍歷,找最小*/return ;if(x==b){ans=y;flag=1;return;}for(int i=u[x]; i!=-1; i=v[i]) /*遍歷該節(jié)點(diǎn)的所有邊,知道節(jié)點(diǎn),即可知邊的長(zhǎng)度,錢,以及下個(gè)節(jié)點(diǎn)*/{if(!book[d[i]]){book[d[i]]=1;dfs(d[i],y+l[i],z+t[i]);book[d[i]]=0;}}return ; } int main() {while(cin>>a>>b>>c){flag=0;ans=inf;memset(book,0,sizeof(book));memset(u,-1,sizeof(u));/*care與上面調(diào)用領(lǐng)接表i!=-1有關(guān)*/for(int i=0; i<c; i++){cin>>s[i]>>d[i]>>l[i]>>t[i];v[i]=u[s[i]];//插入鏈表,表示每個(gè)節(jié)點(diǎn)連接的所有邊,將節(jié)點(diǎn)插到鏈表首部u[s[i]]=i;}dfs(1,0,0);if(flag)cout<<ans<<endl;elsecout<<"-1"<<endl;}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 long using 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)//重載運(yùn)算符{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]表示從點(diǎn)1到點(diǎn)i花費(fèi)的錢數(shù)為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)//找到的時(shí)候直接輸出并返回{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; }?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的ROADS POJ - 1724(限制条件的最短路)【邻接表+深搜】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 针灸减肥的利弊是什么
- 下一篇: 经期可以拔罐吗