POJ3159 Candies 差分约束
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                POJ3159  Candies  差分约束
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                一、內容
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 4Sample Output
5二、思路
- 差分約束
- 由于B的糖與A的糖的差不會超過c, B-A <= c 那么求解這類不等式可使用差分約束。B<= A + c 建立一條A–>B 權值為c的邊。
- 求解1和n的最大差值。 那么就是求 n - 1 <= c1 + c2 …cn 后面的C的最大值。轉化為求到的是1到n的最短距離。可以使用spfa 或 djkstra求解。
- 使用spfa時候注意會超時,這時候我們可以使用棧優化一下。
- 由于數據量較大,可以使用快讀加快速度。
三、代碼
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; }總結
以上是生活随笔為你收集整理的POJ3159 Candies 差分约束的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 非常牛逼的关闭win10家庭版自动更新问
- 下一篇: 并发编程之ReentrantLock--
