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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

POJ 3159 Candies

發布時間:2024/9/5 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 3159 Candies 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  題意是給A和B發糖果,B的糖果數?–?A的糖果數?<=?c,?也就是B?<=?A?+?c,最后求n比1最多多

幾個糖果。題目只有這一個約束條件,建圖不難。將AB看成有向圖的邊,然后c看成邊的權值,轉化成

最短路來求解,大牛們都說了SPFA?+?queue會超時,所以用了SPFA?+?stack。因為這道題沒有負

權的邊,也可以用堆優化的dij來求這個最短路。

?

SPFA + Stack

/*Accepted 2396K 532MS C++ 1363B 2012-08-06 15:32:00*/ #include<cstdio> #include<cstring> #include<cstdlib>const int inf = 0x3f3f3f3f; const int V = 30005; const int E = 150005; int pnt[E], cost[E], nxt[E]; int head[V], e, dist[V]; bool vis[V];int relax( int u, int v, int c) {if( dist[v] > dist[u] + c) {dist[v] = dist[u] + c;return 1;}return 0; }void addedge( int u, int v, int c) {pnt[e] = v; cost[e] = c; nxt[e] = head[u]; head[u] = e ++; }int SPFA( int src, int n) {int i;for(i = 1; i <= n; ++ i){vis[i] = false; dist[i] = inf;}dist[src] = 0;int S[E], top = 1;S[0] = src; vis[src] = true;while(top) {int u, v;u = S[ --top]; vis[u] = false;for(i = head[u]; i != -1; i = nxt[i]) {v = pnt[i];if( 1 == relax( u, v, cost[i]) && !vis[v]) {S[ top ++] = v; vis[v] = true;}}}return dist[n]; }int main() {int n, m;while( scanf( "%d%d", &n, &m) == 2){int a, b, c;e = 0;memset( vis, false, sizeof vis);memset( head, -1, sizeof head);while( m --){scanf( "%d%d%d", &a, &b, &c);addedge( a, b, c);}printf( "%d\n", SPFA( 1, n));}return 0; }

?

?Dijkstra

/*Accepted 2392K 610MS C++ 1376B 2012-08-06 15:27:31*/ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> using namespace std;const int MAXN = 30030, MAXM = 150150; const int inf = 0x3f3f3f3f; int first[MAXN], next[MAXM], v[MAXM], w[MAXM], dist[MAXN]; int n, m, e; typedef pair<int, int> pii;void addedge(int a, int b, int c) {v[e] = b, w[e] = c;next[e] = first[a], first[a] = e ++; }void ReadGraph() {int a, b, c;e = 0;memset(first, -1, sizeof first);while(m --){scanf("%d%d%d", &a, &b, &c);addedge(a, b, c);} }int Dijkstra(int src, int n) {int i, x;pii u;priority_queue<pii, vector<pii>, greater<pii> > q;for(i = 1; i <= n; i ++) dist[i] = inf;dist[src] = 0;q.push(make_pair(dist[src], src));while(!q.empty()){u = q.top(), q.pop();x = u.second;if(dist[x] != u.first) continue;if(n == x) break;for(i = first[x]; i != -1; i = next[i]){if(dist[v[i]] > dist[x] + w[i]){dist[v[i]] = dist[x] + w[i];q.push(make_pair(dist[v[i]], v[i]));}}}return dist[n]; }int main() {while(scanf("%d%d", &n, &m) == 2){ReadGraph();printf("%d\n", Dijkstra(1, n));}return 0; }

?

?

?

轉載于:https://www.cnblogs.com/Yu2012/archive/2012/08/06/2440719.html

總結

以上是生活随笔為你收集整理的POJ 3159 Candies的全部內容,希望文章能夠幫你解決所遇到的問題。

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