ZOJ 1508 poj 1201 Intervals 差分约束系统
2013-04-04 19:51
381 查看
比较巧妙的是令s[i]为集合Z中小于等于i的元素个数。
则得到不等式组s[ai-1] - s[bi] <= -ci;
还有隐含条件s[i] - s[i-1] <=1 && s[i]-s[i-1] >= 0
用Bellman写感觉还不错,清晰明了。抄书上的思路的。
贴代码:
View Code
则得到不等式组s[ai-1] - s[bi] <= -ci;
还有隐含条件s[i] - s[i-1] <=1 && s[i]-s[i-1] >= 0
用Bellman写感觉还不错,清晰明了。抄书上的思路的。
贴代码:
View Code
#include <cstdio> #include <cstring> #define MAXN 50005 int n,min,max; struct Arc { int u,v,w; } edge[MAXN]; int dist[MAXN]; int Bellman() { bool f = true; int i,t; memset(dist,0,sizeof(dist)); while(f) { f = false; for(i=0; i<n; i++) { t = dist[edge[i].u]+edge[i].w; if(dist[edge[i].v] > t) { dist[edge[i].v] = t; f = true; } } for(i=min; i <= max; i++) { t = dist[i-1] + 1; if(dist[i] > t) { dist[i] = t; f = true; } } for(i = max; i>= min; i--) { t = dist[i]; if(dist[i-1] > dist[i]) { dist[i-1] = t; f= true; } } } return -dist[min-1]; } int main() { // freopen("in.cpp","r",stdin); while(~scanf("%d",&n)) { max = -1; min = 100000; for(int i=0; i<n; i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); if(v > max) max = v; if(u < min) min = u; edge[i].u = v; edge[i].v = u-1; edge[i].w = -w; } printf("%d\n",Bellman()); } return 0; }
相关文章推荐
- poj 1201/zoj 1508 intervals 差分约束系统
- zoj 1508 | poj 1201 Intervals
- zoj 1508 && poj 1201 Intervals
- zoj 1508 poj 1201 Intervals
- poj 1201/ZOJ 1508 Intervals 差分约束
- POJ 1201 & HDU1384 & ZOJ 1508 Intervals(差分约束+spfa 求最长路径)
- POJ 1201 & HDU1384 & ZOJ 1508 Intervals 差分约束
- POJ 1201 & HDU1384 & ZOJ 1508 Intervals(差分约束+spfa 求最长路径)
- POJ 1201 Intervals 差分约束系统
- POJ 1201 Intervals 差分约束系统
- 【POJ 1201】 Intervals(差分约束系统)
- POJ 1201 Intervals [差分约束系统]
- POJ 1201 Intervals (差分约束系统)
- POJ -- 1201--Intervals (差分约束系统)
- zoj 1508 Intervals 差分约束系统
- POJ 1201 Intervals(差分约束系统)
- zoj 1508 Intervals 差分约束系统
- POJ1201 Intervals[差分约束系统]
- POJ 1201 Intervals 差分约束系统
- POJ 1201 Intervals (差分约束系统)