Poj 2822 & amp; hdu 4280 & lt; plan network stream & gt;

Source: Internet
Author: User

For Network streams with 10 ^ 5 points and 10 ^ 6 edges, it is generally not efficient. If all edges are bidirectional and the network can form a plan, it can be solved by solving the shortest circuit of the dual graph, the complexity is O (M * log (M), the conversion method is similar to the "minimum cut of the S-T of the Plan", unlike the minimum cut plan of S-T, the difficulty lies in finding the block of a graph. Perform the following steps:
① Split all edges into two directed edges and delete the self-ring.
② Each directed edge is represented by a vertex in another graph G.
③ Examine each vertex in the source image and sort all edge edges connected to it.
④ Traverse each inbound edge. Set its successor to an outbound edge adjacent to it clockwise. That is, in G', a directed edge is connected from the point of the inbound edge to its successor.
### Note that the new edge (S, T) must be specially processed.
⑤ In G' is some non-Intersecting directed loops. Each directed ring corresponds to a region. After finding out all the regions, the figure we want is simple.
⑥ According to the dual diagram, the shortest path between s-t is the minimum cut.
### As for the "Dead End" Problem (which cannot be a plane side), this will form a special area, which is equivalent to going into the dead end and then coming out. However, the answer will not be affected, so ignore it directly.
Hdu 4280 Island Transport code:
[Cpp]
# Pragma comment (linker, "/STACK: 16777216 ")
# Include <cmath>
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <algorithm>
# Include <vector>
# Include <set>
Using namespace std;
Const int maxn = 100010;
Const int maxm = 100010;
Const double inf = 1 <28;
Struct node {
Int be, ne;
Double val;
Void init (int B, int e, double v ){
Be = B;
Ne = e;
Val = v;
}
};
 
Int cmp (double a, double B ){
Double eps = 1e-8;
If (a-B> eps)
Return 1;
Else if (a-B> =-eps)
Return 0;
Else
Return-1;
}
Struct SPFA {
Node buf [maxm * 2];
Int len, E [maxn], queue [maxn];
Double d [maxn];
Void init (){
Memset (E,-1, sizeof (E ));
Len = 0;
}
Void add (int a, int B, double v ){
If (a = B)
Return;
Buf [len]. init (B, E [a], v );
E [a] = len ++;
}
Int vis [maxn];
Double solve (int s, int t ){
Int head = 0, tail = 0;
Memset (vis, 0, sizeof (vis ));
Memset (d, 255, sizeof (d ));
D [s] = 0;
Queue [(tail ++) % maxn] = s;
Vis [s] = true;
Int a, B;
While (head! = Tail ){
A = queue [(head ++) % maxn];
Vis [a] = 0;
For (int I = E [a]; I! =-1; I = buf [I]. ne ){
B = buf [I]. be;
If (cmp (d [a] + buf [I]. val, d [B]) =-1 ){
D [B] = d [a] + buf [I]. val;
If (! Vis [B]) {
Vis [B] = 1;
Queue [(tail ++) % maxn] = B;
}
}
}
}
Return d [t];
}
} Sp;
Struct arch {
Int in, out;
Double angle;
Arch (int a, int B, double c ){
In =;
Out = B;
Angle = c;
}
Bool operator <(const arch & oth) const {
Return cmp (angle, oth. angle) =-1;
}
};
Int n, m;
Double px [maxn], py [maxn], cap [maxm];
Vector <arch> vertex [maxn];
Void init (){
Scanf ("% d", & n, & m );
Double left = inf, right =-inf;
Int s = 0, t = 0;
For (int I = 1; I <= n; I ++ ){
Scanf ("% lf", & px [I], & py [I]);
Vertex [I]. clear ();
If (px [I] <left ){
S = I;
Left = px [I];
}
If (px [I]> right ){
Right = px [I];
T = I;
}
}
Int a, B;
For (int I = 0; I <m; I ++ ){
Scanf ("% d % lf", & a, & B, cap + I );
If (a = B ){
M --;
I --;
Continue;
}
// ① Split all edges into two directed edges and delete the self-ring (is there any influence ?).
Double agab = atan2 (py [B]-py [a], px [B]-px [a]);
Double agba = atan2 (py [a]-py [B], px [a]-px [B]);
// ② Represent each directed edge in another graph G' with a vertex.
Vertex [a]. push_back (arch (I <1, I <1 | 1, agab ));
Vertex [B]. push_back (arch (I <1 | 1, I <1, agba ));
}
A = m <1, B = a | 1;
// Note that the new edge (S, T) must be specially processed.
Vertex [s]. push_back (arch (a, B, acos (-1.0 )));
Vertex [t]. push_back (arch (B, a, 0 ));
}
Int nxt [maxm * 2], belong [maxm * 2], cnt;
Void find (int x, int root ){
If (nxt [x]! = Root)
Find (nxt [x], root );
Belong [x] = cnt;
}
Void build (){
// ③ Sort all edge edges connected to them.
For (int I = 1; I <= n; I ++ ){
Sort (vertex [I]. begin (), vertex [I]. end ());
Int MS = vertex [I]. size ();
// ④ Traverse each inbound edge. Set its successor to an outbound edge adjacent to it clockwise. That is, in G', a directed edge is connected from the point of the inbound edge to its successor.
For (int j = 0; j <MS-1; j ++)
Nxt [vertex [I] [j]. in] = vertex [I] [j + 1]. out;
Nxt [vertex [I] [MS-1]. in] = vertex [I] [0]. out;
}
Int MS = m <1 | 1;
Memset (belong,-1, sizeof (belong ));
Cnt = 0;
// ⑤ In G' is some non-Intersecting directed loops. Each directed ring corresponds to a region. After finding out all the regions, the figure we want is simple.
For (int I = 0; I <= MS; I ++)
If (belong [I] =-1 & ++ cnt> 0)
Find (I, I );
// A non-flat side is formed to form a special area, which is equivalent to a dead end. However, the answer will not be affected, so ignore it directly.
}
Double work (){
Int a, B;
Sp. init ();
// 6 according to the dual diagram, the shortest path between s-t is the minimum cut.
For (int I = 0; I <m; I ++ ){
A = belong [I <1];
B = belong [I <1 | 1];
Sp. add (a, B, cap [I]);
Sp. add (B, a, cap [I]);
}
A = belong [m <1];
B = belong [m <1 | 1];
Return sp. solve (a, B );
}
Int main (){
Int cas;
Scanf ("% d", & cas );
While (cas --){
Init ();
Build ();
Printf ("%. 0f \ n", work ());
}
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.