Candies SPFA + 栈
来源:互联网 发布:威盾软件 编辑:程序博客网 时间:2024/05/29 06:58
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 integersN 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 followM lines each holding three integers A, B and c in order, meaning that kidA 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
Hint
Source
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <vector>#define inf 0x3f3f3f3f#define ms(x) memset(x,false,sizeof(x))using namespace std;const int N = 2000020;struct Edge{ int v; int w; int next;};Edge q[N];int n, cnt;int head[4*N], dis[N];int que[N*10];bool vis[N];void addedge(int u, int v, int w){ q[cnt].v = v, q[cnt].w = w; q[cnt].next = head[u]; head[u] = cnt++;}template <typename T>inline bool scan_d (T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; //EOF while (c != '-' && (c < '0' || c > '9') ) { if((c = getchar()) == EOF) return 0; } sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1;}void SPFA(int s){ ms(vis); for(int i=1;i<=n;i++) dis[i] = inf; dis[s] = 0; vis[s] = true; int top,tail; top = tail = 0; que[top++] = s; //tail++; while(top!=0) { int u = que[--top]; vis[u] = false; int i; for(i=head[u];i!=-1;i=q[i].next) { int v = q[i].v; if(dis[v] > dis[u] + q[i].w) { dis[v] = dis[u] + q[i].w; if(!vis[v]) { vis[v] = true; que[top++] = v; //if(++cnt[v] > n) return false; } } } } //return true;}int main(){ int T, m; scan_d(n); scan_d(m); memset(head, -1, sizeof(head)); cnt=0; for(int i=1;i<=m;i++) { int u, v, w; scan_d(u); scan_d(v); scan_d(w); // scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); }SPFA(1); printf("%d\n",dis[n]); return 0;}
- Candies SPFA + 栈
- poj 3159 -- Candies ( spfa + 栈 )
- POJ-3159-Candies(SPFA+模拟栈)
- Candies(差分约束_栈+SPFA)
- POJ 3159Candies 栈模拟SPFA
- poj3159-Candies差分约束-spfa+栈
- poj Candies(差分约束,spfa栈)
- 3159 Candies SPFA
- poj3159 Candies(SPFA+stack)
- poj 3159 Candies (spfa+栈)
- POJ 3159 Candies 差分约束 spfa+栈+邻接表
- POJ 3159 Candies(SPFA+栈)差分约束
- POJ3159 Candies(最短路径:SPFA+链表+栈)
- POJ3159 Candies(差分约束+SPFA的栈实现)
- POJ 3159 Candies 差分约束-SPFA栈实现
- poj 3159 Candies------spfa算法
- POJ 3159 Candies(SPFA+stack)
- poj 3159 Candies (spfa+stack)
- Winform(XtraReport)实现打印方法(转载)
- 比特率与波特率的区别与联系
- hdu-5924-Mr. Frog’s Problem
- CVTE前端面试
- (三)几种排序算法的学习总结(快速排序)
- Candies SPFA + 栈
- text-justification
- 【ESP32视频分享3】Ubuntu使用 Eclipse开发ESP32程序
- Eclipse控制台log4j日志级不同颜色显示
- 微信小程序学习笔记 2.3.1 视图与渲染
- NPOI之Excel——合并单元格、设置样式、输入公式
- C语言读写mysql
- Servlet工作原理解析
- 99乘法表1