Advertisement poj-1752 **

Source: Internet
Author: User
1 /*
2 * a natural differential constraint system. If there is no specific write, convert it to one ..
3 *
4 *
5 * http://www.cnblogs.com/ihainan/archive/2011/07/23/2114884.html
6 * solution: the following constraints are available for the question:
7 *IfB-A> = K
8 *ThenS [B]-s [a]> = K
9 *OtherwiseS [B]-s [a]> = DIS (A, B)
10 *AndS [B]-s [a] <= DIS (A, B)
11 *At the same time0 <= s [I]-s [I-1] <= 1
12 *
13 * a graph can be created based on constraints. spfa calculates the longest path.
14 *
15 * it is better not to use things in STL. The written spfa is even slower than Bellman !!!
16 */
17
18 # include <stdio. h>
19 # include <string. h>
20 # define n 1200
21 # define maxint 0 xfffffff
22 # define num 40000
23 void Init (), spfa (), print (), addedge (INT, Int, INT );
24 struct edge {
25 int X, Y, W, next;
26} e [5 * num];
27 int head [3 * num], n, k, m = 0, DIS [3 * num], vis [3 * num], ANS = 0, minnum = maxint, maxnum =-maxint, queue [num * 300];
28 int main ()
29 {
30 Init ();
31 spfa ();
32 print ();
33 return 0;
34}
35 void Init ()
36 {
37 int I, j, A, B, C, ch;
38 memset (Head,-1, sizeof (head ));
39 memset (DIS, 0, sizeof (DIS ));
40 scanf ("% d", & K, & N );
41 for (I = 1; I <= N; ++ I ){
42 scanf ("% d", & A, & B );
43 If (A> B ){
44 CH =;
45 A = B;
46 B = CH;
47}
48 A + = num-1;
49 B + = num;
50 minnum = minnum <? Minnum:;
51 maxnum = maxnum> B? Maxnum: B;
52 c = B-;
53 If (C <k ){
54 addedge (A, B, C );
55 addedge (B, A,-C );
56}
57 else
58 addedge (a, B, k );
59}
60}
61 void addedge (int x, int y, int W)
62 {
63 + + m;
64 E [M]. x = X;
65 E [M]. Y = y;
66 E [M]. W = W;
67 E [M]. Next = head [x];
68 head [x] = m;
69}
70
71 void spfa ()
72 {
73 int I, j, X, Y, tail = 0, front = 0;
74 for (I = minnum; I <= maxnum; ++ I ){
75 if (I! = 0 ){
76 queue [++ tail] = I;
77 addedge (I, I + 1, 0 );
78 addedge (I + 1, I,-1 );
79}
80 dis [I] =-maxint;
81 vis [I] = 0;
82}
83 dis [minnum] = 0;
84 vis [minnum] = 1;
85 while (front <tail ){
86 x = queue [++ front];
87 vis [x] = 0;
88 for (I = head [X]; I! =-1; I = E [I]. Next ){
89 y = E [I]. Y;
90 if (DIS [x] + E [I]. W> dis [y]) {
91 dis [y] = E [I]. W + dis [x];
92 If (! Vis [y]) {
93 vis [y] = 1;
94 queue [++ tail] = y;
95}
96}
97}
98}
99}
100
101 void print ()
102 {
103 int I;
104 printf ("% d \ n", DIS [maxnum]);
105 for (I = minnum; I <= maxnum; ++ I)
106 If (DIS [I]-Dis [I-1] = 1)
107 printf ("% d \ n", I-num );
108}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.