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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

POJ3159 Candies 差分约束

發(fā)布時(shí)間:2024/1/18 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ3159 Candies 差分约束 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、內(nèi)容

During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?

Input

The input contains a single test cases. The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 through N. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers A, B and c in order, meaning that kid A believed that kid B should never get over c candies more than he did.

Output

Output one line with only the largest difference desired. The difference is guaranteed to be finite.

Sample Input

2 2 1 2 5 2 1 4

Sample Output

5

二、思路

  • 差分約束
  • 由于B的糖與A的糖的差不會(huì)超過(guò)c, B-A <= c 那么求解這類不等式可使用差分約束。B<= A + c 建立一條A–>B 權(quán)值為c的邊。
  • 求解1和n的最大差值。 那么就是求 n - 1 <= c1 + c2 …cn 后面的C的最大值。轉(zhuǎn)化為求到的是1到n的最短距離。可以使用spfa 或 djkstra求解。
  • 使用spfa時(shí)候注意會(huì)超時(shí),這時(shí)候我們可以使用棧優(yōu)化一下
  • 由于數(shù)據(jù)量較大,可以使用快讀加快速度。

三、代碼

queue換做棧:

#include <cstdio> #include <cstring> #include <stack> using namespace std; const int N = 3e4 + 5, M = 15e4 + 5; struct E {int v, w, next; } e[M]; int n, m, a, b, w, len = 1, h[N], d[N]; bool vis[N]; inline int read(){int s=0,w=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w; } void add(int u, int v, int w) {e[len].v = v;e[len].w = w;e[len].next = h[u];h[u] = len++; } int spfa() {memset(d, 0x3f, sizeof(d));d[1] = 0; //求1到n的最短距離就是1和n的最大差值stack<int> q;q.push(1);while (!q.empty()) {int u = q.top();q.pop();vis[u] = false;for (int j = h[u]; j; j = e[j].next) {int v = e[j].v;int w = d[u] + e[j].w;if (w < d[v]) {d[v] = w;if (!vis[v]) q.push(v), vis[v] = true;}}} return d[n]; } int main() {scanf("%d%d", &n, &m);for (int i = 1; i <= m; i++) {a = read(); b = read(); w = read();//scanf("%d%d%d", &a, &b, &w);add(a, b, w);} printf("%d", spfa());return 0; }

djkstra:

#include <cstdio> #include <cstring> #include <queue> using namespace std; const int N = 3e4 + 5, M = 15e4 + 5; struct E {int v, w, next; } e[M]; struct Node {int v, d;Node(int d, int v): d(d), v(v){}bool operator < (const Node &w) const {return d > w.d;} }; int n, m, a, b, w, len = 1, h[N], d[N]; bool vis[N]; void add(int u, int v, int w) {e[len].v = v;e[len].w = w;e[len].next = h[u];h[u] = len++; } int djkstra() {memset(d, 0x3f, sizeof(d));d[1] = 0; //求1到n的最短距離就是1和n的最大差值priority_queue<Node> q;q.push(Node(0, 1));while (!q.empty()) {int u = q.top().v;q.pop();if (vis[u]) continue;vis[u] = true;for (int j = h[u]; j; j = e[j].next) {int v = e[j].v;int w = d[u] + e[j].w;if (w < d[v]) {d[v] = w;q.push(Node(d[v], v));}}} return d[n]; } int main() {scanf("%d%d", &n, &m);for (int i = 1; i <= m; i++) {scanf("%d%d%d", &a, &b, &w);add(a, b, w);} printf("%d", djkstra());return 0; }

總結(jié)

以上是生活随笔為你收集整理的POJ3159 Candies 差分约束的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。