POJ 1201 Intervals 差分约束系统
2013-06-06 00:25
387 查看
/** * @file main.cpp * @brief 差分约束系统,基本不等式如下,求s * 1. s[b] - s[a] >= c 直观理解是s[b] - s[a - 1] >= c,s[x]表示集合中选自[0,x]的数字总数,程序对下标做了处理 * 2. s[i] - s[i - 1] >= 0 * 3. s[i - 1] - s[i] >= -1 * 因此,由此可得松弛方法: * 1. if (s[a] + c > s[b]) * s[b] = s[a] + c * 2. if (s[i - 1] + 0 > s[i]) * s[i] = s[i - 1] + 0 * 3. if (s[i] + (-1) > s[i - 1]) * s[i - 1] = s[i] + (-1) * 因此,转换为最短路径的构图方式为: * 1. <a, b>添加一条权为c的边 * 2. <i - 1, i>添加一条权为0的边 * 3. <i, i - 1>添加一条权为-1的边 * * @author yekeren * @version 1.0.0 * @date 2013-06-05 */ #include <iostream> #include <queue> #define MAX_INT 0x7fffffff #define LIMIT 51001 ///(M)临接表 struct edge_t { int e, c; struct edge_t *next; }; edge_t *g[LIMIT] = { NULL }; edge_t pool[LIMIT * 4]; int npool = 0; /** * @brief 临接表添加边 * @param s * @param e * @param c */ void add_edge(int s, int e, int c) { pool[npool].e = e; pool[npool].c = c; pool[npool].next = g[s]; g[s] = &pool[npool++]; } /** * @brief shortest path faster algorithm * @param imin * @param imax * @return */ int spfa(int imin, int imax) { unsigned char visit[LIMIT] = { 0 }; std::queue<int> q; int dist[LIMIT] = { 0 }; for (int i = imin; i <= imax; ++i) { dist[i] = - MAX_INT; } dist[imin] = 0; visit[imin] = 1; q.push(imin); while (!q.empty()) { int u = q.front(); q.pop(); visit[u] = 0; for (edge_t *p = g[u]; p != NULL; p = p->next) { int v = p->e; int c = p->c; if (dist[u] + c > dist[v]) { dist[v] = dist[u] + c; if (!visit[v]) { visit[v] = 1; q.push(v); } } } } return dist[imax]; } int main(int argc, char *argv[]) { int n; scanf("%d", &n); int imin = MAX_INT; int imax = 0; for (int i = 0; i < n; ++i) { int a, b, c; scanf("%d%d%d", &a, &b, &c); ++b; add_edge(a, b, c); if (a < imin) { imin = a; } if (b > imax) { imax = b; } } for (int i = imin; i < imax; ++i) { add_edge(i, i + 1, 0); add_edge(i + 1, i, -1); } printf("%d\n", spfa(imin, imax)); return 0; }
相关文章推荐
- poj 1201 Intervals(差分约束系统)
- POJ 1201 && HDU 1384 Intervals 差分约束系统
- POJ 1201 Intervals (差分约束系统)
- POJ 1201 Intervals [差分约束系统]
- 【差分约束系统】POJ 1201--Intervals
- POJ -- 1201--Intervals (差分约束系统)
- poj 1201 Intervals(差分约束系统)(中等)
- POJ 1201 Intervals 差分约束系统
- 差分约束系统_bellman_ford_poj 1201 Intervals
- POJ 1201 && HDU 1384 Intervals(差分约束系统)
- POJ 1201 Intervals <差分约束系统 + SPFA / 贪心 + 树状数组>
- [POJ 1201] Intervals 差分约束系统
- POJ1201 Intervals[差分约束系统]
- POJ 1201 Intervals(差分约束系统)
- POJ 1201-Intervals(差分约束系统)
- 图论(差分约束系统):POJ 1201 Intervals
- POJ 1201 Intervals(差分约束系统)
- POJ 1201-Intervals(差分约束系统)
- POJ 1201 Intervals (差分约束系统)
- POJ 1201 Intervals 差分约束系统