POJ 1716(差分约束系统+快读)

POJ 1716

(1)思路:

求出最小值,就是转化为A-B>=C的式子,然后求出最长路径。

(2)注意:

快读,要不超时。

(3)代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e4+10;
const int INF = 1e9+10;
int head[maxn],vis[maxn],dis[maxn],n,m,tot,que[maxn];
struct Node{
	int v,nxt,w;
}cur[maxn<<2];
void Init(){
	memset(head,-1,sizeof(head));
	tot = 0;
}
void Add(int x,int y,int z){
	cur[tot].v = y;
	cur[tot].nxt = head[x];
	cur[tot].w = z;
	head[x] = tot++;
}
int MIN(int x,int y){
	return x<y?x:y;
}
int MAX(int x,int y){
	return x>y?x:y;
}

inline void read(int &x){
	x = 0; char ch = getchar();
	while (!isdigit(ch)) ch = getchar();
	while (isdigit(ch)) x = (x * 10) + (ch & 15), ch = getchar();
}
void spfa(int st,int ed){
	for(int i=st;i<=ed;i++){
		dis[i] = -INF;vis[i] = 0;
	}
	int top = 0;
	que[top++] = st;dis[st] = 0;
	while(top!=0){
		int x = que[--top];vis[x] = 0;
		for(int i=head[x];i!=-1;i=cur[i].nxt){
			int y = cur[i].v;
			if(dis[y]<dis[x]+cur[i].w){
				dis[y] = dis[x]+cur[i].w;
				if(vis[y]==0){
					vis[y] = 1;
					que[top++] = y;
				}
			}
		}
	}
	printf("%d\n",dis[ed]);
}
int main(void){
	Init();
	int mi,mx;
	read(m);
	for(int i=0;i<m;i++){
		int x,y;read(x);read(y);
		if(i==0){
			mi = x;mx = y+1;
		}
		else{
			mi = MIN(x,mi);
			mx = MAX(y+1,mx);
		}
		Add(x,y+1,2);
	}
	for(int i=mi;i<=mx;i++){
		Add(i+1,i,-1);
		Add(i,i+1,0);
	}
	spfa(mi,mx);
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41829060/article/details/91904693