School route (recursive sap-incomplete)

Source: Internet
Author: User

Problem 2 route (route. cpp/c/pas)

[Description]

Coco and kakaka live in the eastern suburbs of mosaic city. They need to switch to a car multiple times every day to reach the school in the west of the city. It was not until one day that they joined the school's informatics Olympic competition team that they found that the daily route to school was not necessarily the best.

Mosaic City has a total of N bus stations, you may wish to number them as 1... N is a natural number, and it is believed that Coco and kakana live near bus station 1, while their school is at Bus Station N. There are M direct bus routes in the city, and the bus carrying out the I route is between the station pi and qi, the time required from the start point to the end point is ti. (1 <= I <= M, 1 <= pi, qi <= N)

Two people sat in front of the computer and quickly programmed the best ride plan based on the above information. However, Coco suddenly came up with a ghost idea. He wanted to cut some routes in the input data of Kaka while KaKa was not ready, so that the answer obtained by Kaka's program would be greater than the actual shortest time. In fact, there is a price for each line I: the larger the number of lines to be deleted, the more likely KaKa will find this joke, coco wants to know what kind of deletion scheme can achieve his goal, and minimizes the sum of the ci of the deleted bus route.

The bus network in mosaic city is very developed. You can think that any two stations can arrive at each other through a direct connection or a shuttle bus. Of course, if the deletion solution you provide cannot reach each other, therefore, the shortest required for attending school is positive infinity: this is obviously a legal solution.

 

[Input format]

In the input file, the first line has two positive integers N and M, indicating the number of bus stops and bus routes of mosaic City, respectively. The following M rows: each line (line I, total line (I + 1) describes the I route using four positive integers (separated by a space): pi, qi, ti and ci. For more information, see the description above.

[Output format]

The output file can contain up to two rows. There is only one integer In the first line, which indicates the shortest time required from Coco and kakana to school. The second row outputs an integer C, indicating the sum of Ci

[Example input]

6 7

1 21 3

2 61 5

1 31 1

3 41 1

4 61 1

5 61 2

1 51 4

[Sample output]

2

5

[Data Scope]

2 <= N <= 124, 1 <= M <= 750, 1 <= ti, ci <= 10 000

 

 


I am using recursive sap (). Who knows crazy Wa.

When using recursive sap, NTR is often inexplicable.

Conclusion: The correctness of Z in // dfs_sap () remains to be verified...

My experience is summarized as follows:

1. Exit condition:

-PS: it is possible that the extended flow is 0, but the distance label can be changed to continue the flow.

-PS: sometimes it is infinitely augmented. The answer can tell the above error.

2. Modify the distance label:

-PS: We recommend that you directly add 1 to the distance label, and use minj to optimize the distance label.

3. flow During dfs

-PS: Be sure to return in advance, or change the distance label ~

4. Review:

-PS: This is a qualitative difference. Is recursion not stuck?

5. unique focus:

PS: when it is not running, remove the distance label to optimize it! T to death, not WA to explosion

6. The only benefit of recursive sap is convenience ...... (What is your 1 minute BFS_sap ()? If not)

 

 

[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <cstdlib>
# Include <iostream>
# Include <algorithm>
# Include <functional>
# Define MAXN (500 + 10)
# Define MAXM (124750 + 10)
# Define MAXCi (10000 + 10)
# Define INF (2139062143)
# Define For (I, n) for (int I = 1; I <= n; I ++)
# Define Forp (p, u) for (int p = pre [u]; p = next [p])
Using namespace std;
Int n, m, edge [2 * MAXM], weight [2 * MAXM], c [2 * MAXM], next [2 * MAXM], pre [MAXN], size = 1;
Void addedge (int u, int v, int w, int w2)
{
Edge [++ size] = v;
Weight [size] = w;
C [size] = w2;
Next [size] = pre [u];
Pre [u] = size;
}
Int d [MAXN], q [MAXN * 8];
Void spfa ()
{
Memset (d, 127, sizeof (d ));
D [1] = 0;
Int head = 1, tail = 1; q [1] = 1;
While (head <= tail)
{
Int & u = q [head];
Forp (p, u)
{
Int & v = edge [p];
If (d [v] = INF | d [u] + weight [p] <d [v])
{
Q [++ tail] = v; d [v] = d [u] + weight [p];
}
}
Head ++;
}
}
Int d2 [MAXN], cnt [MAXN];
Void spfa2 ()
{
Memset (d2, 127, sizeof (d2 ));
Memset (cnt, 0, sizeof (cnt ));
D2 [n] = 0; cnt [0] = 1;
Int head = 1, tail = 1; q [1] = n;
While (head <= tail)
{
Int & u = q [head];
Forp (p, u)
{
Int & v = edge [p];
If (d [u]-weight [p]! = D [v]) continue;
If (d2 [v] = INF)
{
Q [++ tail] = v; d2 [v] = d2 [u] + 1;
Cnt [d2 [v] ++;
}
}
Head ++;
}
}
Bool sap_T = 1, B [2 * MAXM] = {0 };
Int sap (int u, int flow)
{
If (u = n) return flow;
Int tot = 0, minj = INF;
Forp (p, u)
{
Int & v = edge [p];
/*
If (d2 [v] = INF) continue;
If (! (D [u] + weight [p] = d [v] | d [u]-weight [p] = d [v]) continue;
*/
If (! B [p]) continue;
If (c [p]> 0 & d2 [u] = d2 [v] + 1)
{
Int w = sap (v, min (flow-tot, c [p]);
C [p]-= w; c [p ^ 1] + = w; tot + = w;
If (flow = tot) return tot;
} Else if (c [p]) minj = min (minj, d2 [v]);
}
If (-- cnt [d2 [u] = 0)
{
D2 [1] = n, sap_T = 0;/* return tot ;*/
} // D2 [1] = n + 1;
// If (minj ^ INF) cnt [d2 [u] = minj + 1] ++;
/* Else */cnt [++ d2 [u] ++;
Return tot;
}
Int main ()
{
Freopen ("route. in", "r", stdin );
Freopen ("route. out", "w", stdout );
Memset (pre, 0, sizeof (pre ));
Scanf ("% d", & n, & m );
For (I, m)
{
Int u, v, w1, w2;
Scanf ("% d", & u, & v, & w1, & w2 );
Addedge (u, v, w1, w2 );
Addedge (v, u, w1, w2 );
}
Spfa ();
// For (int I = 1; I <= n; I ++) cout <d [I] <''; cout <endl;
Cout <d [n] <endl;
Int ans = 0;
Spfa2 ();
Int de_bug_1 = 0;
For (I, n)
Forp (p, I)
{
If (d [I]> d [edge [p]) c [p] = 0;
// * 0
// If (d2 [I]! = INF & d2 [edge [p]! = INF & c [p]) cout <I <''<edge [p] <endl, de_bug_1 ++;
If (d [I] + weight [p] = d [edge [p]) B [p] = B [p ^ 1] = 1, /* cout <I <''<edge [p] <endl, */de_bug_1 ++;
}
// Cout <de_bug_1 <endl;
B [0] = 1;
For (I, n)
Forp (p, I)
{
While (next [p] &! B [next [p]) next [p] = next [next [p];
}
 
While (/* sap_T */1)
{
If (d2 [1]> = n) break;
Int w = sap (1, INF );
// If (! W) break;
// Cout <ans <''<d2 [1] <endl;
Ans + = w;
}
Cout <ans <endl;
Return 0;
}

# Include <cstdio>
# Include <cstring>
# Include <cstdlib>
# Include <iostream>
# Include <algorithm>
# Include <functional>
# Define MAXN (500 + 10)
# Define MAXM (124750 + 10)
# Define MAXCi (10000 + 10)
# Define INF (2139062143)
# Define For (I, n) for (int I = 1; I <= n; I ++)
# Define Forp (p, u) for (int p = pre [u]; p = next [p])
Using namespace std;
Int n, m, edge [2 * MAXM], weight [2 * MAXM], c [2 * MAXM], next [2 * MAXM], pre [MAXN], size = 1;
Void addedge (int u, int v, int w, int w2)
{
Edge [++ size] = v;
Weight [size] = w;
C [size] = w2;
Next [size] = pre [u];
Pre [u] = size;
}
Int d [MAXN], q [MAXN * 8];
Void spfa ()
{
Memset (d, 127, sizeof (d ));
D [1] = 0;
Int head = 1, tail = 1; q [1] = 1;
While (head <= tail)
{
Int & u = q [head];
Forp (p, u)
{
Int & v = edge [p];
If (d [v] = INF | d [u] + weight [p] <d [v])
{
Q [++ tail] = v; d [v] = d [u] + weight [p];
}
}
Head ++;
}
}
Int d2 [MAXN], cnt [MAXN];
Void spfa2 ()
{
Memset (d2, 127, sizeof (d2 ));
Memset (cnt, 0, sizeof (cnt ));
D2 [n] = 0; cnt [0] = 1;
Int head = 1, tail = 1; q [1] = n;
While (head <= tail)
{
Int & u = q [head];
Forp (p, u)
{
Int & v = edge [p];
If (d [u]-weight [p]! = D [v]) continue;
If (d2 [v] = INF)
{
Q [++ tail] = v; d2 [v] = d2 [u] + 1;
Cnt [d2 [v] ++;
}
}
Head ++;
}
}
Bool sap_T = 1, B [2 * MAXM] = {0 };
Int sap (int u, int flow)
{
If (u = n) return flow;
Int tot = 0, minj = INF;
Forp (p, u)
{
Int & v = edge [p];
/*
If (d2 [v] = INF) continue;
If (! (D [u] + weight [p] = d [v] | d [u]-weight [p] = d [v]) continue;
*/
If (! B [p]) continue;
If (c [p]> 0 & d2 [u] = d2 [v] + 1)
{
Int w = sap (v, min (flow-tot, c [p]);
C [p]-= w; c [p ^ 1] + = w; tot + = w;
If (flow = tot) return tot;
} Else if (c [p]) minj = min (minj, d2 [v]);
}
If (-- cnt [d2 [u] = 0)
{
D2 [1] = n, sap_T = 0;/* return tot ;*/
} // D2 [1] = n + 1;
// If (minj ^ INF) cnt [d2 [u] = minj + 1] ++;
/* Else */cnt [++ d2 [u] ++;
Return tot;
}
Int main ()
{
Freopen ("route. in", "r", stdin );
Freopen ("route. out", "w", stdout );
Memset (pre, 0, sizeof (pre ));
Scanf ("% d", & n, & m );
For (I, m)
{
Int u, v, w1, w2;
Scanf ("% d", & u, & v, & w1, & w2 );
Addedge (u, v, w1, w2 );
Addedge (v, u, w1, w2 );
}
Spfa ();
// For (int I = 1; I <= n; I ++) cout <d [I] <''; cout <endl;
Cout <d [n] <endl;
Int ans = 0;
Spfa2 ();
Int de_bug_1 = 0;
For (I, n)
Forp (p, I)
{
If (d [I]> d [edge [p]) c [p] = 0;
// * 0
// If (d2 [I]! = INF & d2 [edge [p]! = INF & c [p]) cout <I <''<edge [p] <endl, de_bug_1 ++;
If (d [I] + weight [p] = d [edge [p]) B [p] = B [p ^ 1] = 1, /* cout <I <''<edge [p] <endl, */de_bug_1 ++;
}
// Cout <de_bug_1 <endl;
B [0] = 1;
For (I, n)
Forp (p, I)
{
While (next [p] &! B [next [p]) next [p] = next [next [p];
}

While (/* sap_T */1)
{
If (d2 [1]> = n) break;
Int w = sap (1, INF );
// If (! W) break;
// Cout <ans <''<d2 [1] <endl;
Ans + = w;
}
Cout <ans <endl;
Return 0;
}

 

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.