题目
Candies
Time Limit: 1500MS Memory Limit: 131072K
Total Submissions: 27843 Accepted: 7674
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
2 2
1 2 5
2 1 4
Sample Output
5
题意
班上有n个同学,每个人想有p[i]个糖果,但是第i个人要求第j个人不能超过自己k个,即p[j]-p[i]<=k,求p[n]-p[1]的最大值
题解
差分约束系统,用spfa维护,由于数据原因。用了stack和输入挂才刚刚擦过。 。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <string>
#include <set>
#include <cmath>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
#include <iomanip>
#define m0(a) memset(a,0,sizeof(a))
#define mm(a) memset(a,0x3f,sizeof(a))
#define m_1(a) memset(a,-1,sizeof(a))
#define f(i,a,b) for(i = (a);i<=(b);i++)
#define fi(i,a,b) for(i = a;i>=b;i--)
#define lowbit(a) ((a)&(-a))
#define FFR freopen("data.in","r",stdin)
#define FFW freopen("data.out","w",stdout)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef long double ld;
const ld PI = acos(-1.0);
using namespace std;
#define SIZE ( )
template<typename T> inline void read(T &x) {
x = 0; T f = 1; char ch = getchar();
while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
x *= f;
}
struct Edge {
int y, v;
int next;
Edge() { y = v = next = 0; }
};
int head[30000 + 10];
struct Edge1 {
Edge G[150000 + 10];
int cnt;
Edge1() { cnt = 0; };
void push_back(int x, int y, int v) {
G[cnt].y = y; G[cnt].v = v;
G[cnt].next = head[x];
head[x] = cnt++;
}
};
int d[30000 + 10];
Edge1 a;
int main() {
//ios_base::sync_with_stdio(false); cin.tie(0);
int n, m ;
while (~scanf("%d%d",&n,&m)) {
int i,j;
m_1(head);
a.cnt = 0;
f(i, 1, m) {
int x, y, v;
read(x); read(y); read(v);
a.push_back(x, y, v);
}
mm(d);
d[1] = 0;
stack<int>qu;
qu.push(1);
while (!qu.empty()) {
int s = qu.top(); qu.pop();
for(i = head[s];i != -1;i = a.G[i].next)
if (d[a.G[i].y] > d[s] + a.G[i].v) {
d[a.G[i].y] = d[s] + a.G[i].v;
qu.push(a.G[i].y);
}
}
printf("%d\n", d[n] - d[1]);
}
return 0;
}