Maximum stream of HDU 4975 and determination of its uniqueness (directed graph ring judgment algorithm upgrade)

Source: Internet
Author: User

Let's review the largest stream at that time .. Try again... I don't want to talk about the classic solution anymore .. This question mainly refers to the pitfall time, with 10 submissions of 7 TLE.

Ring judgment, once using a simple DFS method, this time it's TLE! I felt that I could not time out to use a very embarrassing dinic. I firmly believed that the ring was slow. So I learned how to delete a vertex or edge when breaking the ring! If you go in from a certain point, if all the edges of the point have been traversed and still fail to return, then the store will no longer enter (this simple way should be thought! Stupid !) Delete edges only at the beginning, or TLE! NB! So I deleted and deleted the edge myself. It took a moment to 156 ms, and the first 5 were there!

# Include <cstdio> # include <iostream> # include <queue> # include <cstring> # include <string> using namespace STD; const int maxv = 1200; const int MaxE = 2*501*501 + 2000; const int INF = 0x3f3f3f; int n, m; int allsumn = 0, allsumm = 0; int nume = 0; int e [MaxE] [3]; int head [maxv]; bool flag; void inline Adde (int I, Int J, int C) {e [nume] [0] = J; E [nume] [1] = head [I]; head [I] = nume; E [nume ++] [2] = C; E [nume] [0] = I; E [nume] [1] = head [J]; head [J] = nu Me; E [nume ++] [2] = 0;} int lev[ maxv]; int vis [maxv]; int Ss = 0; int TT = 0; bool BFS () {memset (lev, 0, sizeof (lev)); memset (VIS, 0, sizeof (VIS); queue <int> q; q. push (SS); vis [ss] = 1; while (! Q. Empty () {int cur = Q. Front (); q. Pop (); For (INT I = head [cur]; I! =-1; I = E [I] [1]) {int v = E [I] [0]; If (E [I] [2]> 0 &&! Vis [v]) {lev[ v] = lev[ cur] + 1; // If (V = TT) return 1; // This sentence is not added, faster Q. push (V); vis [v] = 1 ;}} return vis [TT] ;}int DFS (int u, int minf) {If (u = TT | minf = 0) return minf; int sumf = 0, F; For (INT I = head [u]; I! =-1 & minf; I = E [I] [1]) {int v = E [I] [0]; if (lev_v] = lev_u] + 1 & E [I] [2]> 0) {f = DFS (V, E [I] [2] <minf? E [I] [2]: minf); sumf + = f; E [I] [2]-= f; E [I ^ 1] [2] + = F; minf-= f ;}} if (! Sumf) lev[ u] =-1; return sumf;} int dinic () {int sum = 0; while (BFS () {sum + = DFS (SS, INF);} return sum;} int hasv [maxv]; void Init () {allsumm = allsumn = nume = 0; scanf ("% d", & N, & M); SS = N + m; TT = N + m + 1; for (INT I = 0; I <= tt; I ++) {head [I] =-1; hasv [I] = 0 ;}} void read_build () {int temps = 0; For (INT I = 0; I <N; I ++) {scanf ("% d", & temps); allsumn + = temps; Adde (SS, I, temps); For (Int J = 0; j <m; j ++) Adde (I, j + N, 9) ;}for (Int J = 0; j <M; j ++) {scanf ("% d", & temps); allsumm + = temps; Adde (J + N, TT, temps );}} bool dfs_getother_ans (int u, int FA) {If (hasv [u]) return 0; For (INT I = head [u]; I! =-1; I = E [I] [1]) {int v = E [I] [0]; if (V = fa | E [I] [2] <= 0 | V = SS | V = TT) continue; If (! Vis [v]) {vis [v] = 1; if (dfs_getother_ans (v, u) return 1; vis [v] = 0; // mark the directed graph when it comes out!} Else {return 1;} e [I] [2] = 0; // delete an edge! -50 ms} hasv [u] = 1; // delete a vertex, key! -900 ms return 0;} int main () {int t; scanf ("% d", & T); For (int ii = 1; II <= T; II ++) {Init (); read_build (); printf ("case # % d:", ii); If (allsumm! = Allsumn) {printf ("so naive! \ N "); continue;} int ans = dinic (); If (ANS! = Allsumm) {printf ("so naive! \ N ") ;}else {flag = 0; For (INT I = 0; I <n; I ++) {vis [I] = 1; if (dfs_getother_ans (I,-1) {flag = 1; break;} vis [I] = 0;} If (FLAG) printf ("so young! \ N "); else {printf (" so simple! \ N ") ;}} return 0 ;}




Maximum stream of HDU 4975 and determination of its uniqueness (directed graph ring judgment algorithm upgrade)

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.