poj 1201 差分约束+spfa

非常经典的差分约束系统的建模。求最小值需要转化为求最长路。

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdio>
 5 using namespace std;
 6
 7 const int INF = 99999999;
 8 const int N = 50002;
 9 const int M = 200000;
10 int head[N];
11 int dist[N];
12 bool visit[N];
13 int n, m, e;
14 int maxn, minn;
15
16 struct Edge
17 {
18     int v, next, w;
19 } edge[M];
20
21 void addEdge( int u, int v, int w )
22 {
23     edge[e].v = v;
24     edge[e].w = w;
25     edge[e].next = head[u];
26     head[u] = e++;
27 }
28
29 void spfa( int s )
30 {
31     for ( int i = minn; i <= maxn; i++ )
32     {
33         dist[i] = -INF;
34         visit[i] = false;
35     }
36     dist[s] = 0;
37     visit[s] = true;
38     int q[N], top = 0;
39     q[top++] = s;
40     while ( top )
41     {
42         int u = q[--top];
43         visit[u] = false;
44         for ( int j = head[u]; j != -1; j = edge[j].next )
45         {
46             int v = edge[j].v, w = edge[j].w;
47             if ( dist[v] < dist[u] + w )
48             {
49                 dist[v] = dist[u] + w;
50                 if ( !visit[v] )
51                 {
52                     q[top++] = v;
53                     visit[v] = true;
54                 }
55             }
56         }
57     }
58 }
59
60 int main ()
61 {
62     while ( scanf("%d", &n) != EOF )
63     {
64         e = 0;
65         memset( head, -1, sizeof(head) );
66         minn = INF, maxn = -INF;
67         for ( int i = 1; i <= n; i++ )
68         {
69             int a, b, c;
70             scanf("%d%d%d", &a, &b, &c);
71             addEdge( a, b + 1, c );
72             maxn = max( maxn, b + 1 );
73             minn = min( minn, a );
74         }
75         for ( int i = minn + 1; i <= maxn; i++ )
76         {
77             addEdge( i, i - 1, -1 );
78             addEdge( i - 1, i, 0 );
79         }
80         spfa(minn);
81         printf("%d\n", dist[maxn]);
82     }
83     return 0;
84 }
时间: 2024-11-07 15:33:45

poj 1201 差分约束+spfa的相关文章

poj 1201(差分约束)

Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24948   Accepted: 9491 Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: reads the number of intervals, their end po

poj 3159 差分约束+spfa

由于此题数据特殊,队列优化的spfa会超时,可以改成用栈来优化. 1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 7 const int INF = 9999999; 8 const int N = 30001; 9 const int M = 150000; 10 int head[N

POJ 3169 Layout (差分约束+SPFA)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6832   Accepted: 3292 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

poj 3169 差分约束

3169 差分约束的是满足多组形如xi-yj<=bk{i,j<n k<m}不等式极值问题,可以转化为单源最短路来求. 在最短路中 d[v]<=d[u]+w(u,v) 可以看出跟上面的不等式很像 通常有两种一种是求满足所有不等式的最大值,还有是最小值. 这篇博客可以参考一下 分析: 题目给出了两种不等式  d[u]+dl >=d[v]       d[u]+dd<=d[v]  要求的是最大值,也就是最短路 对于d[u]+dl>=d[v] 建边(u,vdl) 对于d[

ZOJ 2770 Burn the Linked Camp 差分约束+SPFA

第一道正儿八经的差分约束题 有排成一列的n个点,首先告诉你每个点的值最多是多少(最少显然要大于0),然后告诉你m段i,j,k,表示第i个点到第j个点的值的和至少有k,问你总和至少为多少. 要注意的是,告诉你的所有关系式都不要忘记建边,一开始漏了大于0的条件调半天o(╯□╰)o 不等式的形式是a-b<=c这样的= = 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <

poj 1201 Intervals【差分约束+spfa】

设s为前缀和,首先显然的条件是\[ s_{bi}-s_{ai-1}>=c \],然后隐含的是\[ s_i-s_{i-1}>=0 s_i-s_{i-1}<=1 \] 然后根据差分约束,就是连边(bi,ai-1,-li),(i-1,i,1),(i,i-1,0) spfa跑最长路最后输出相反数即可,注意n是起点,min是终点,跑最短路(不会有负环) #include<iostream> #include<cstdio> #include<queue> usi

POJ 3159 Candies(差分约束+spfa+链式前向星)

题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A的糖果数<=C . 最后求n 比 1 最多多多少颗糖果. 解题思路:经典差分约束的题目,具体证明看这里<数与图的完美结合——浅析差分约束系统>. 不妨将糖果数当作距离,把相差的最大糖果数看成有向边AB的权值,我们得到 dis[B]-dis[A]<=w(A,B).看到这里,我们可以联想到

(简单) POJ 3169 Layout,差分约束+SPFA。

Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbe

POJ 3159[差分约束]

题目链接:[http://poj.org/problem?id=3159] 题意:有N个小朋友,编号为1-N,每个小朋友将分的一些糖果,给出一些关系A.B.C .表示B最多比A多C个,然后问你盆友1和盆友N的糖果数最大差多少.保证有解. 题解:差分约束求最短距离:DIJ+对优化||SPAF+栈优化 #include<queue> #include<cstdio> #include<cstring> #include<algorithm> using name