poj 3159-Candies
2013-07-22 12:49
337 查看
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, Band 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表示有n个同学,m表示m组数据
u,v,w,表示同学u要求同学v的糖果数不能多于他超过w个
输出:同学n和同学1的他糖果数最多相差几个??
题目分析:差分约束,条件很明显,运用类似最短路分析d[v] - d[u] <= w(u, v) ,也就是 d[v] <= w(u,v) + d[u];
用spfa求最长的路
代码:
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 20430 | Accepted: 5412 |
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, Band 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表示有n个同学,m表示m组数据
u,v,w,表示同学u要求同学v的糖果数不能多于他超过w个
输出:同学n和同学1的他糖果数最多相差几个??
题目分析:差分约束,条件很明显,运用类似最短路分析d[v] - d[u] <= w(u, v) ,也就是 d[v] <= w(u,v) + d[u];
用spfa求最长的路
代码:
#include <stdio.h> #include <iostream> #include <string.h> #include <queue> using namespace std; #define N 35000 #define INF 1 << 29 struct edge { int v; int w; int next; }e[150000]; int head , cnt; int d ; bool vis ; int s ; int n, m; void init () { memset (head, -1, sizeof (head)); cnt = 0; memset (d, 0, sizeof (d)); memset (vis, 0, sizeof (vis)); } void add (int u, int v, int w) { e[cnt].v = v; e[cnt].w = w; e[cnt].next = head[u]; head[u] = cnt ++; } void spfa () { for (int i = 2; i <= n; i ++) d[i] = INF; int h = 0; s[h ++] = 1; while (h) { int u = s[--h]; vis[u] = 0; for (int i = head[u]; i != -1; i = e[i].next) { if (d[e[i].v] > d[u] + e[i].w) { d[e[i].v] = d[u] + e[i].w; if (!vis[e[i].v]) { vis[e[i].v] = 1; s[h ++] = e[i].v; } } } } } int main () { scanf ("%d%d", &n, &m); init (); while (m --) { int u, v, w; scanf ("%d%d%d", &u, &v, &w); add (u, v, w); } spfa (); printf ("%d\n", d - d[1]); return 0; }
相关文章推荐
- poj 3159 Candies
- poj 3159 Candies
- poj 3159 Candies(差分约束 spfa stack实现)
- POJ 3159 Candies 差分约束-SPFA栈实现
- poj--3159--Candies(简单差分约束)
- poj 3159 Candies 差分约束
- POJ-3159 Candies( 差分约束 )
- 【POJ 3159】Candies(差分约束+SPFA)
- poj3159 Candies 2012-09-07
- POJ-3159 Candies(差分约束)
- POJ 3159 Candies(差分约束系统)
- POJ3159_Candies_差分约束_SPFA
- POJ 3159 Candies
- [poj 3159]Candies[差分约束详解][朴素的考虑法]
- poj 3159 Candies(优先队列 dijkstra+堆)
- POJ 3159 Candies(差分约束)
- poj_3159_Candies
- POJ 3159 Candies 差分约束系统
- poj 3159 Candies(差分约束)
- [poj 3159]Candies[差分约束详解][朴素的考虑法]