【spfa(栈)或者 堆优dijkstra】poj3159
来源:互联网 发布:淘宝怎么提高店铺权重 编辑:程序博客网 时间:2024/06/05 07:39
Description
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 throughN. 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 21 2 52 1 4
Sample Output
5
</pre><pre class="sio" style="font: 14px/26px 'Courier New', Courier, monospace; text-align: left; color: rgb(54, 46, 43); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: pre-wrap; -ms-word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;" name="code"><pre class="sio" style="font: 14px/26px 'Courier New', Courier, monospace; text-align: left; color: rgb(54, 46, 43); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: pre-wrap; -ms-word-wrap: break-word; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;" name="code">差分约束,但是用spfa会超时(priority_queue和手工循环队列都会tle)然后看了poj的discuss说是可以用栈……这个道理都差不多但是确实是想不到这么干
当然还可以用堆优的dijkstra
</pre><pre class="sio" style="font: 14px/26px 'Courier New', Courier, monospace; text-align: left; color: rgb(54, 46, 43); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: pre-wrap; -ms-word-wrap: break-word; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;" name="code">spfa(栈):
</pre><pre class="sio" style="font: 14px/26px 'Courier New', Courier, monospace; text-align: left; color: rgb(54, 46, 43); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: pre-wrap; -ms-word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;" name="code"><pre class="cpp" name="code">#include<iostream>#include<cstdio>#include<cstring>using namespace std;;const long N = 30010, M = 150010, Q = N * 2;const long INF = 0x3f3f3f3f;struct ty{ long t, w, next;};long n, t, m;long head[N];ty edge[M];long q[Q];bool v[N];long dist[N];void insertedge(long x, long y, long z, long k){ edge[k].t = y; edge[k].w = z; edge[k].next = head[x]; head[x] = k;}void init(){ memset(head, 0, sizeof(head)); memset(edge, 0, sizeof(edge)); m = 0; for (long i = 1; i <= t; i++) { long x, y, z; scanf("%d%d%d", &x, &y, &z); insertedge(x, y, z, ++m); }}void spfa(){ memset(v, 0, sizeof(v)); memset(dist, 127, sizeof(dist)); long r = 0; q[++r] = 1; v[1] = true; dist[1] = 0; while (r) { long x = q[r--]; v[x] = false; for (long i = head[x]; i != 0; i = edge[i].next) { long t = edge[i].t; if (dist[t] > dist[x] + edge[i].w) { dist[t] = dist[x] + edge[i].w; if (!v[t]) { q[++r] = t; v[t] = true; } } } } cout << dist[n] << endl;}int main(){ while(scanf("%d%d", &n, &t) != EOF) { init(); spfa(); } return 0 ;}
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;;const long N = 30010, M = 150010, Q = N * 2;const long INF = 0x3f3f3f3f;struct ty{ long t, w, next; bool operator < (const ty &a) const { if (w > a.w) return true;else return false; }};priority_queue<ty> q;long n, t, m;long head[N];ty edge[M];bool v[N];long dist[N];void insertedge(long x, long y, long z, long k){ edge[k].t = y; edge[k].w = z; edge[k].next = head[x]; head[x] = k;}void init(){ memset(head, 0, sizeof(head)); memset(edge, 0, sizeof(edge)); m = 0; for (long i = 1; i <= t; i++) { long x, y, z; scanf("%d%d%d", &x, &y, &z); insertedge(x, y, z, ++m); }}void dijkstra_priority_queue(){ memset(v, 0, sizeof(v)); memset(dist, 127, sizeof(dist)); v[1]=true; dist[1]=0; for (long i = head[1]; i != 0; i = edge[i].next) { if (edge[i].w < dist[edge[i].t]) { dist[edge[i].t] = edge[i].w; q.push(edge[i]); } } ty t1, t2; while(!q.empty() ) { t1 = q.top(); q.pop(); if (v[t1.t]) continue; v[t1.t] = true; for (long j = head[t1.t]; j != 0; j = edge[j].next) { long u = edge[j].t; if ((!v[u]) && (dist[u] > edge[j].w + dist[t1.t])) { dist[u] = edge[j].w + dist[t1.t]; t2.t = u; t2.w = dist[u]; q.push(t2); } } } cout << dist[n] << endl;}int main(){ while(scanf("%d%d", &n, &t) != EOF) { init(); dijkstra_priority_queue(); } return 0 ;}
</pre><pre>
- 【spfa(栈)或者 堆优dijkstra】poj3159
- poj3159(spfa+栈)
- poj3159(spfa)
- POJ3159 Candies(差分约束+SPFA的栈实现)
- POJ3159::SPFA
- spfa+栈+邻接表模板POJ3159
- poj3159-Candies差分约束-spfa+栈
- POJ3159差分约束(SPFA)
- (poj3159)dijkstra优先队列实现
- TOJ 2217. Invitation Cards【最短路dijkstra+堆优化或者SPFA】
- TOJ 2217. Invitation Cards【最短路dijkstra+堆优化或者SPFA】
- hdu 1546 Idiomatic Phrases Game (SPFA或者Dijkstra)
- hdu 2680 Choose the best route(SPFA或者Dijkstra)
- POJ3159---Candies(最短路:spfa+栈or队列)(邻接表)
- poj3159 Candies(SPFA+stack)
- poj3159 candies dijkstra
- 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)
- POJ3159 Candies(最短路径:SPFA+链表+栈)
- 理解文件的属性
- Palindromic Subsequence(最长回文字符串 输出路径)
- Unique Paths
- 强烈的范围
- union
- 【spfa(栈)或者 堆优dijkstra】poj3159
- 海量数据框架变迁——阿里巴巴上市背后的技术力量
- 变量被删除
- 再谈hashCode和equals
- 状态机的实现
- 内存位置不在堆叠
- 编程人员必须会用的软件
- 行动中的栈
- 马踏棋盘