poj 3159 Candies (spfa+stack)
2016-03-18 20:06
423 查看
http://poj.org/problem?id=3159
Candies
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 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
Sample Output
题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果
注意:数据较大,用邻接表存储,还有就是用stack600多MS过,用queue的话就TLE了
Candies
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 27564 | Accepted: 7593 |
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
题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果
注意:数据较大,用邻接表存储,还有就是用stack600多MS过,用queue的话就TLE了
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <stack> using namespace std; #define N 200000 #define INF 0x3f3f3f3f #define met(a, b) memset (a, b, sizeof (a)) int vis , dist , head , cnt, n, m; struct node { int u, v, flow, next; } edge[N*4]; void Init () { met (vis, 0); met (head, -1); for (int i=0; i<=n; i++) dist[i] = INF; cnt = 0; } void addedge (int u, int v, int flow) { edge[cnt].u = u; edge[cnt].v = v; edge[cnt].flow = flow; edge[cnt].next = head[u]; head[u] = cnt++; } void spfa () { stack <int> sta; while (!sta.empty()) sta.pop(); sta.push (1); dist[1] = 0; vis[1] = 1; while (!sta.empty()) { int u = sta.top(); sta.pop(); vis[u] = 0; for (int i=head[u]; i!=-1; i=edge[i].next) { int v = edge[i].v; int flow = edge[i].flow; if (dist[v] > dist[u] + flow) { dist[v] = dist[u] + flow; if (!vis[v]) { sta.push (v); vis[v] = 1; } } } } printf ("%d\n", dist ); } int main () { while (scanf ("%d %d", &n, &m) != EOF) { Init(); int u, v, flow; for (int i=1; i<=m; i++) { scanf ("%d %d %d", &u, &v, &flow); addedge (u, v, flow); } spfa (); } return 0; }
相关文章推荐
- poj 1002 487-3279(参考YSQ)
- Android小程序-标准体重计算器
- android dispatchTouchEvent方法查找包含点击坐标的view;
- 2015 房子距离
- Javascript 执行上下文 context&scope
- 【Chromium中文文档】跨进程通信 (IPC)
- 大国博弈与“一带一路”
- BeanFactory 和 ApplicationContext
- [国嵌攻略][160][SPI驱动程序设计]
- 四则运算3
- GUI学习
- U盘重装windows 10 系统教程
- js原生的ajax写法
- 12、c#中事务及回滚
- 腾讯、百度、阿里以及各小公司面试经历
- 【Matlab】图像裁剪函数imcrop的原点、长度、宽度问题
- Python 将Excel转为Xml
- PHP Object Injection(PHP对象注入)
- Java读写Windows共享文件夹 .
- 软件测试 junit的配置与使用