日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 1724 】ROADS (带限制的最短路 或 dfs 或 A*算法,双权值)

發布時間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 1724 】ROADS (带限制的最短路 或 dfs 或 A*算法,双权值) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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 2

Sample Output

11

題目大意:

? ?題意第一行給了一個k,代表你有k的錢數,下一行有一個n,代表n個點,然后一個m,代表m條邊,然后接下來m行,每行有四個數,分別代表起點、終點、路徑長度和要花費的錢數,題目想問在花的錢不超過k的情況下,從1---n的最短路徑是多少。

解題報告:

? ? 注意cost和length不能直接存到二維數組中,然后用min來去重邊,,因為可能一個大一個小,emmm對于這種兩個權值的還是要注意啊。。。

AC代碼1:(94ms)

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 500 + 5; const int INF = 0x3f3f3f3f; int k,n,m; struct Node {int v;int w,c;int ne;Node(){}Node(int v,int w,int c):v(v),w(w),c(c){} }e[MAX*MAX]; int head[MAX]; bool vis[MAX]; int ans = INF,tot; void add(int u,int v,int w,int c) {e[++tot].v = v;e[tot].w = w;e[tot].c = c;e[tot].ne = head[u];head[u] = tot; } void dfs(int cur,int c,int w) {//c花費 w路程 if(c > k || w > ans) return;if(cur == n) {ans = min(ans,w);return ;}for(int i = head[cur]; i!=-1; i=e[i].ne) {Node v = e[i];if(vis[v.v]) continue;vis[v.v] = 1;dfs(v.v,c+v.c,w+v.w);vis[v.v] = 0;} } int main() {cin>>k>>n>>m;int u,v,w,c;memset(head,-1,sizeof head);for(int i = 1; i<=m; i++) {scanf("%d%d%d%d",&u,&v,&w,&c);add(u,v,w,c);}dfs(1,0,0);if(ans == INF) puts("-1");else printf("%d\n",ans);return 0 ;}

AC代碼2:(32ms)

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 500 + 5; const int INF = 0x3f3f3f3f; int k,n,m; struct Node {int v;int dis,c;int ne;Node() {}Node(int v,int w,int c):v(v),dis(dis),c(c) {} } e[MAX*MAX]; struct Point {int id,dis,cost;bool friend operator < (Point a,Point b) {return a.dis>b.dis;} }; //vector<Node> vv[MAX]; int head[MAX]; bool vis[MAX]; int ans = INF,tot; void add(int u,int v,int w,int c) {e[++tot].v = v;e[tot].dis = w;e[tot].c = c;e[tot].ne = head[u];head[u] = tot; } ll dp[105][10005];//dp[i][j] : 到i號城市,花費為j,的最短路。 void Dijkstra() {for(int i=1; i<=n; i++)for(int j=0; j<=k; j++)dp[i][j]=INF;dp[1][0]=0;priority_queue<Point>pq;Point now,cur;cur.id=1;cur.cost=0;cur.dis=0;pq.push(cur);while(!pq.empty()) {cur=pq.top();pq.pop();if(cur.id==n) {printf("%d\n",cur.dis);return;}for(int i=head[cur.id]; i!=-1; i=e[i].ne) {int cost=cur.cost+e[i].c;if(cost>k)continue;if(dp[e[i].v][cost]>cur.dis+e[i].dis) { dp[e[i].v][cost]=cur.dis+e[i].dis;now.id=e[i].v;now.cost=cost;now.dis=dp[e[i].v][cost];pq.push(now);}}}puts("-1"); } int main() {cin>>k>>n>>m;int u,v,w,c;memset(head,-1,sizeof head);for(int i = 1; i<=m; i++) {scanf("%d%d%d%d",&u,&v,&w,&c);add(u,v,w,c);}Dijkstra();return 0 ; }

寫Dijkstra的時候就不需要if(vis[cur.id]) continue;了。。(其實這一句在一般的Dijkstra中也可以不寫,只是統計路徑條數的時候必須要寫上這個,不然就算重復了(比如那道qduoj的題))

AC代碼3:(47 - 32ms)

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 500 + 5; const int INF = 0x3f3f3f3f; int k,n,m; struct Node {int v;int dis,c;int ne;Node() {}Node(int v,int w,int c):v(v),dis(dis),c(c) {} } e[MAX*MAX]; struct Point {int id,dis,cost;bool friend operator < (Point a,Point b) {return a.dis>b.dis;} }; //vector<Node> vv[MAX]; int head[MAX]; bool vis[MAX]; int ans = INF,tot; void add(int u,int v,int w,int c) {e[++tot].v = v;e[tot].dis = w;e[tot].c = c;e[tot].ne = head[u];head[u] = tot; } ll dp[105][10005];//dp[i][j] : 到i號城市,花費為j,的最短路。 int bfs() {priority_queue<Point>pq;Point now,cur;cur.id=1;cur.cost=0;cur.dis=0;pq.push(cur);while(!pq.empty()) {cur=pq.top(); pq.pop();if(cur.id==n) {if(cur.cost <= k) return cur.dis;}for(int i=head[cur.id]; i!=-1; i=e[i].ne) {int nowcost=cur.cost+e[i].c;if(nowcost>k)continue;now.id=e[i].v;now.cost=nowcost;now.dis=cur.dis + e[i].dis;pq.push(now);}}puts("-1"); } int main() {cin>>k>>n>>m;int u,v,w,c;memset(head,-1,sizeof head);for(int i = 1; i<=m; i++) {scanf("%d%d%d%d",&u,&v,&w,&c);add(u,v,w,c);}printf("%d\n",bfs());return 0 ; }

總結:

? 對于poj我還能說什么,,換上vector立刻tle。。

?

AC代碼4:(A*算法,0ms)

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 150 + 5; const int INF = 0x3f3f3f3f; int k,n,m; struct Node {int v;int dis,c;int ne;Node() {}Node(int v,int w,int c):v(v),dis(dis),c(c) {} } e[MAX*MAX],r[MAX*MAX]; struct Point {int id,dis,cost;bool friend operator < (Point a,Point b) {return a.dis>b.dis;} }; //vector<Node> vv[MAX]; int head[MAX],read[MAX]; bool vis[MAX]; int Dis[MAX]; int ans = INF,tote,totr; void add(int u,int v,int w,int c) {e[++tote].v = v;e[tote].dis = w;e[tote].c = c;e[tote].ne = head[u];head[u] = tote; } void add2(int u,int v,int w,int c) {r[++totr].v = v;r[totr].dis = w;r[totr].c = c;r[totr].ne = read[u];read[u] = totr; } void Dijkstra() {memset(Dis,INF,sizeof Dis);priority_queue<Point>pq;Point now,cur;cur.id=n;cur.cost=0;cur.dis=0;Dis[n] = 0;pq.push(cur);while(pq.size()) {cur=pq.top(); pq.pop();if(vis[cur.id]) continue;vis[cur.id] = 1;for(int i=read[cur.id]; i!=-1; i=r[i].ne) {int cost=cur.cost+r[i].c; // if(cost>k)continue;因為就是求標準的最短路,所以這個就沒啥用了先 if(Dis[r[i].v]>cur.dis+r[i].dis) { //松弛條件Dis[r[i].v]=cur.dis+r[i].dis;now.id=r[i].v;now.cost=cost;now.dis=Dis[r[i].v];pq.push(now);}}} } struct A {int id,dis,c;A(){};A(int id,int dis,int c):id(id),dis(dis),c(c){}bool friend operator < (A a,A b) {return a.dis + Dis[a.id] > b.dis + Dis[b.id];} }; int Astar(int st,int ed) {priority_queue< A > pq;pq.push(A(st,0,0));while(pq.size()) {A cur = pq.top();pq.pop();if(cur.id == ed) return cur.dis;for(int i = head[cur.id]; i != -1; i = e[i].ne) {if(cur.c + e[i].c <= k) pq.push(A(e[i].v,cur.dis + e[i].dis,cur.c + e[i].c));}}return -1; } int main() {cin>>k>>n>>m;int u,v,w,c;memset(head,-1,sizeof head);memset(read,-1,sizeof read);for(int i = 1; i<=m; i++) {scanf("%d%d%d%d",&u,&v,&w,&c);add(u,v,w,c);add2(v,u,w,c);}Dijkstra();printf("%d\n",Astar(1,n));return 0 ; }

總結:

? 其實這個代碼求最短路的那個Point結構體都不需要帶上cost這個參數,,,因為沒啥用。

總結

以上是生活随笔為你收集整理的【POJ - 1724 】ROADS (带限制的最短路 或 dfs 或 A*算法,双权值)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。