【差分约束】 HDOJ 1384 Intervals

差分约束的入门题。。。

#include <iostream>  
#include <queue>  
#include <stack>  
#include <map>  
#include <set>  
#include <bitset>  
#include <cstdio>  
#include <algorithm>  
#include <cstring>  
#include <climits>  
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 50005
#define maxm 200005
#define eps 1e-10
#define mod 2012
#define INF 1e9
#define lowbit(x) (x&(-x))  
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid  
#define rson o<<1 | 1, mid+1, R  
typedef long long LL;
//typedef int LL;
using namespace std;

queue<int> q;
int dis[maxn], inq[maxn];
int cost[maxm], h[maxn], v[maxm], next[maxm];
int n, m, mi, mx;

void init(void)
{
	memset(dis, -1, sizeof dis);
	memset(inq, 0, sizeof inq);
	memset(h, -1, sizeof h);
	mx = 0, mi = INF;
}
void spfa(int s, int t)
{
	dis[s] = 0, q.push(s), inq[s] = 1;
	while(!q.empty()) {
		int u = q.front(); q.pop(), inq[u] = 0;
		for(int e = h[u]; ~e; e = next[e])
			if(dis[u] + cost[e] > dis[v[e]]) {
				dis[v[e]] = dis[u] + cost[e];
				if(!inq[v[e]]) inq[v[e]] = 1, q.push(v[e]);
			}
	}
}
void read(void)
{
	int a, b, c, cnt = 0;
	for(int i = 1; i <= n; i++) {
		scanf("%d%d%d", &a, &b, &c);
		b++;
		if(a < mi) mi = a;
		if(b > mx) mx = b;
		next[cnt] = h[a], h[a] = cnt, v[cnt] = b, cost[cnt] = c, cnt++;
	}
	for(int i = mi; i <= mx; i++) {
		next[cnt] = h[i], h[i] = cnt, v[cnt] = i+1, cost[cnt] = 0, cnt++;
		next[cnt] = h[i+1], h[i+1] = cnt, v[cnt] = i, cost[cnt] = -1, cnt++;
	}
}
int main(void)
{
	while(scanf("%d", &n)!=EOF) {
		init();
		read();
		spfa(mi, mx);
		printf("%d\n", dis[mx]);
	}
	return 0;
}


你可能感兴趣的:(HDU)