bzoj 4283 magical girl Yi Liya

Original link: http://www.cnblogs.com/chmwt/p/10648549.html

Subject to the effect

  Weight with a given positive n-$ $ $ m $ points undirected graph edges. Find a path that satisfies:

  It is a simple path

  It is a strictly short-circuits

  For any one side may be present in the shortest path can not contain its reverse side.

  1 - This output path does not exist.

answer

  Conscience title shortest properties, covering the most commonly used properties shortest path and the shortest path tree of FIG.

  Then he began to talk about the topic.

The basic nature of the shortest path of FIG.

  The $ d \ left (u, v \ right) $ $ U $ point referred to as the point of the shortest length of $ V $.
  Note $ d_ {s} \ left ( x \ right) = d \ left (s, x \ right), d_ {t} \ left (x \ right) = d \ left (x, t \ right) $.

  FIG shortest path $ s $ is the shortest path to all of $ t $ union.

  For example will help illustrate:

  

  On the left is the original image on the right is the shortest road map.

  Note that the shortest path is a directed graph in FIG.

  Here is the original stamp $ G = (V, E) $, is the shortest path stamp $ G * = (V *, E *) $.

FIG basic nature of the shortest path I (Definition 1.1)

  For any $ e \ in E * $, if $ e = (u, v) $, then $ d_ {s} (u) + w (e) + d_ {t} (v) = d (s, t) $.

  Clearly the shortest road map must be a DAG.

FIG basic properties shortest path II (Theorem 1.1)

  For any $ x \ in V * $, then there $ d_ {s} (x) + d_ {t} (x) = d (s, t) $.

  Show that if $ s = x $, then the conclusion is clearly established.

  Now consider the $ s \ neq x $ in the case. Temporarily referred $ L = d (s, t) $

  FIG defined by the shortest path found $ d_ {s} (x) \ geqslant L - d_ {t} (x) $. Because the $ x $ is not a starting point, so there must be a precursor $ x '$.

  I have basic properties $ d_ {s} (x ') + w (x', x) + d_ {t} (x) = L $. A $ d_ {s} (x) $ defined understood $ d_ {s} (x) \ leqslant d_ {s} (x ') + w (x, x') = L - d_ {t} (x) $ .

  Therefore $ d_ {s} (x) = L - d_ {t} (x) $.

  Therefore Theorem is proved.

FIG basic nature of the shortest path III (Corollary 1.2)

  For $ e = (x, y) \ in E * $, then there $ d_ {s} (x) + w (x, y) = d_ {s} (y) $.

  The proof of Theorem 1.1 has $ d_ {s} (y) = d (s, t) - d_ {t} (y) $. By definition there are 1.1 $ d_ {s} (x) + w (x, y) = d (s, t) - d_ {t} (y) $. Then Theorem is proved.

The basic properties of IV of FIG shortest path (Corollary 1.3)

  If a $ X $ to simple path $ Y $ present in the shortest path drawing, then $ d_ {s} (x) + l (P * (x, y)) + d_ {t} (y) = L $. Wherein P * (x, y) represents a path * $ G $ on a, l (P * (x, y)) represents the length of this path.

  Inference can be reused 1.2 $ d_ {s} (x) + l (P * (x, y)) = d_ {s} (y) $. Then according to Theorem 1.1 is easy to prove. For more proof for the reader.

FIG basic nature of the shortest path V (Corollary 1.4)

  FIG shortest path to a $ X $ $ Y $ simple path, the corresponding one of the original $ X $ $ Y $ to the shortest.

  With 1.3 inferences can use reductio ad absurdum. For more proof for the reader. The above can be introduced simultaneously $ l (P * (x, y)) = d (x, y) $.

Theorem 1.5

  If $ x, y \ in V * $, and satisfy $ x \ neq y, d_ {s} (x) \ leqslant d_ {x} (y) $, then $ G * $ in $ s $ to $ x the shortest and $ Y $ $ $ t $ to disjoint shortest path.

  Proof (Definition 1.1) seen from the definition of the shortest path map, $ s $ to process $ X $ of $ d_ {s} (x ') $ incremented, $ y $ to $ t $ during $ d_ {s} (y ') $ increments. And because $ x \ neq y, d_ {s} (x) \ leqslant d_ {x} (y) $, so they do not intersect.

Theorem 2

  Then in terms of some of the conventions it.

  Forward edge: it has for a $ E * $, then we call it a directed edge $ (u, v) $, the positive side.

  Reverse side: For a directed edge $ (u, v) $, if $ (v, u) \ in E * $, then we call it a reverse side.

  Internal edge: forward side and reverse side collectively referred to as the internal side.

  External side: in the $ E $, but not part of the side $ E * $.

  Now to clarify what notational conventions path.

  $ P (u, v) $, U represents a $ $ $ V $ of the path.

  $ P * (u, v) $, represents one of the $ G * $ U $ $ $ V $ of the path.

  $ P (x, y) + Q (y, z) $, $ P represents a path along the $ X $ $ $ Y $ come, and then along $ Q $, $ Y $ come from $ z the $ path.

  $ P ^ {- 1} (x, y) $, $ P $ represents the path along a reverse side (not the reverse edge defined above), a path from the $ X $ $ $ Y's.

  $ P ^ {0} (x ', y') on the $, $ P $ path, a $ x '$ to $ y' of the sub-path $.

  $ L (P) $, $ P $ denotes the length of the path.

  The path set request $ S $.

  External path: start and end points $ G * $, after the change is a simple path to the external side. The starting point is called Abduction point this path, the end point is a return to this path.

Theorem 2.1

  At least one external path in a path $ $ S.

  If it does not prove that the external path, the path of the $ S $ edges are forward edge (because you can not go the reverse side). Since $ G * $ is a DAG, so $ l (S) = d (s, t) $, does not meet the requirements of the subject.

Theorem 2.2

  S $ $ path comprising an external path of a point Po $ x $, the return point $ y $, then there $ d_ {s} (x) \ leqslant d_ {s} (y) $.

  prove:

    Conclusion assumption does not hold, then there $ d_ {s} (x)> d_ {s} (y) $.

    Provided $ S = P (s, x) + Q (x, y) + R (y, t) $. Then let $ U = P * (s, y) + Q ^ {- 1} (x, y) + R * (x, t) $. Since the path $ Q $ is a simple path, P * and R * are composed of a positive side, it can be obtained from the theorem 1..5 P * and R * does not intersect. So $ U $ is a simple path.

    And because $ U $ contains at least one external side, so it's not the shortest. Thus a path to meet requirements of the subject.

    And because $ P * (s, y) <P * (s, x) \ leqslant P (s, x), R * (x, t) <R * (y, t) \ leqslant R (y, t) , Q (x, y) = Q ^ {- 1} (x, y) $, so $ l (U) <l (S) $.

    And $ S $ optimality contradiction.

Theorem 2.3

  S $ $ path comprising an external path exactly.

prove:

  The number of the external path is not included in a hypothesis.

  If no external route, apparently contradictory.

  If contains more than one external path, set $ S = P * (s, x) + Q (x, y) + R (y, t) $, so $ U = P * (s, x) + Q (x, y) + R * (y, t) $, where $ Q (x, y) $ is an external path.

  According to Theorem 1.5 is easy to prove $ the U-$ is a simple path with $ R * (y, t) <R (y, t) $ as defined by the shortest path map, so $ l (U) <l (S) $, and $ S $ optimality contradiction.

Nature shortest path tree

  S $ $ agreed point of Abduction Abduction point outside the path contains $ S $, its return path to the external point of return points it contains.

Definition 3.1

  $ P $ defined in a shortest path tree rooted at $ T_ {p} = (V, E_ {T}) $ is the picture to $ p $ root of a directed path spanning tree. Wherein an edge $ e (v, u) $ satisfies $ d (u, p) + w (e) = d (v, p) $.

  Because only use $ T_ {t} $ below, therefore, less likely it directly abbreviated $ T $.

note

  Here shortest path tree is a directed graph.
  All sides have to point to the root node.
  No map to a unique shortest path map, but the shortest path spanning a given point may not be unique.
  Then again Definitions subtree.

3.2 Definitions

  In order to $ p $ is the root of the shortest path spanning tree:

    After the point $ X $ subtree, the $ T_ {p} $ site $ X $ $ X $ broken point out only one side, to the remaining $ X $ tree rooted at a point $ X $ sub tree. Referred to as $ T_ {p} (x) $.

    $ X $ true point subtree, $ X $ true point subtree is $ T_ {p} (x) $ a subgraph. In the $ T_ {p} (x) $ present in the point $ X $ point true subtree, if and only if its to $ X $ path does not pass through any part of $ G except $ X $ a * $ point. That is a     not a $ X $ point, but are $ G * $, it must not exist in the real $ X $ subtree. Referred to as $ T * _ {p} ( x) $.

  The true definition of sub-tree may not be well understood (plus my language is not good), then give an chestnuts:

 

  After the third image, the right side are omitted. In the fourth and fifth figures FIG intermediate web orange dot is marked at the point of $ G * $.

  Here is a very basic truth about the nature of the sub-tree.

Theorem 3.1

  For any $ x, y \ in G * $, has $ T * (x) \ cap T * (y) = \ varnothing $.

  It is easy to prove by definition Mako tree.

Then again agreed to a token.

  $ P_ {T} (x, y) $, tree $ T_ {t} $ in a $ X $ $ Y $ of the path.

The basic properties of the shortest path tree (Theorem 3.2)

  The shortest path tree $ T_ {t} $, any point $ X $ a simple path to an ancestor of $ Y $, corresponding to an original in the $ X $ $ Y $ is the shortest.

prove

  When the card statement is true when $ y = t $.

  Consider $ y \ neq t $ the situation.

  Still assuming not the shortest. Then there is a more optimal path from $ to $ x $ y $, then the $ t $ path. And $ T_ {t} $ defined in contradiction.

  Therefore Theorem is proved.

  Note that if these reverse path, may correspond to the $ X $ $ $ Y in a picture in the shortest.

Definitions 3.3

  In the $ T $, $ X $ true path ancestors $ P_ {T} (x, t) $, the nearest point on a $ X $ $ G * $ in. It will be recorded as $ prt (x) $.

  Another words it is to walk along the side of the $ x $, until it encounters a $ G * $ in the points. Note that it may also be $ x $ $ t $.

  Abduction point set $ S $ of $ x $, return to point $ z $, obviously $ x \ in T * (x), z \ in T * (z) $, according to Theorem 3.1, then $ T * ( X) $ and $ T * (z) $ intersection does not exist. So the $ S $ must exist an outer edge $ (w, w ') $ such that $ w \ in T * (x) $, and $ S ^ {0} (w', z) of each point $-average not $ T * (x) $ in. There is a very obvious fact is that $ prt (w) = x $.

Theorem 3.3

  $d_{s}(x) \leqslant d_{s}(prt(w'))$。

Note the following proved to be very cumbersome, please drink water to continue reading.

prove

  Still assuming that conclusion does not hold. Then there is $ d_ {s} (x)> d_ {s} (prt (w ')) $. Therefore $ d_ {t} (x) <d_ {t} (prt (w ')) $. According to Theorem 2.2 has $ d_ {s} (x) <= d_ {s} (z) $, so $ d_ {s} (prt (w ')) <d_ {s} (z) $, so $ PRT ( w ') \ neq z $. According to Theorem 3.1, it is found, there must be an edge $ (y, y ') $ such that $ y \ in T * (prt (w')) $ and $ S ^ {0} (y ', z) $ of each in point of not $ T * (prt (w ')) $. Obviously $ prt (w ') = prt (y) $.

  Order $ Q = P * (s, prt (w ')), R = P_ {T} (prt (w'), y), P = Q + R + S ^ {0} (y, t) $ ( See below).

 

  R & lt prove $ $ actually $ T * (prt (w ')) $ in. After a further $ G * $ point of the assumed path, it found $ prt (y) $ is not equal to $ prt (w ') $, contradictory.

  Because $ d_ {s} (prt (w ')) <d_ {s} (z) $, by Theorem 1.5 understood $ Q $ does not $ S ^ {0} (z, t) $ intersect, and because $ R $ at $ T * (prt (w ')) $, the edges are so after the external side, the internal sides of $ P $ do not intersect. And because $ S ^ {0} (y '. Z) $ is a simple path outside, not in $ T * (prt (w')) $. $ P $ external path so that the edges do not intersect. Thus the path $ P $ is a simple path. (Note: this refers to the intersection of the presence of the ring, instead there is a common point between the sides.)

  Because $ P $ and comprising at least one outer edge, so $ P $ is a path to meet the requirements (except Strictly short).

  Because $ S $ is the required path, so there are:

    $l(S) \leqslant l(P)$

    $l(S^{0}(s, x)) + l(S^{0}(x, y)) + l(S^{0}(y, t)) \leqslant l(Q) + l(R) + l(S^{0}(y, t))$

    $l(S^{0}(s, x)) + l(S^{0}(x, y)) \leqslant l(Q) + l(R)$

    $d_{s}(x) + l (S^{0}(x, y)) \leqslant d_{s}(prt (w')) + l (R)$(推论 1.4)

  又因为 $prt (w') < d_{s}(x)$,所以 $l (S^{0}(x, y)) < l (R)$。

  令 $U = S^{0}(x, y)^{-1} + P*(x, t)$。那么有:

    $l(U) = l(S^{0}(x, y)^{-1}) + l(P*(x, y))\\=l(S^{0}(x,y)) + d_{t}(x)\\<l(R) + d_{t}(prt(w'))\\=l(R^{-1}) + d_{t}(prt(w'))\\=l(P_{t}(y,t))$

  但是 $U$ 经过了至少一条外部边,所以有 $l (U) > l (P_{T}(y, t)) = d_{t}(y)$。但是刚刚却推出了与之矛盾的式子。

  所以假设不成立,定理得证:$d_{s}(x) \leqslant (prt (w'))$。

定理 3.4

  必然存在一个满足所有条件的路径 S*,满足$S* = P_{T}(s, w) + (w, w') + P_{T}(w', t)$ 

  其中 $(w, w')$ 是定理 3.3 中涉及到的一条边。

证明

  令 $Q = P_{T}(s, w), R = P_{T}(w', t)$,根据定理 3.1 易证 $S*$ 的外部边不相交,根据定理 3.3 和定理 1.5 易证 $S*$ 的内部边不会相交。又因为 $(w, w')$ 一定是一条外部边。所以 $S*$ 是一条简单路径但不是最短路径。

  因为 $S$ 中包含至少一条非树边,然后用反证法易证存在一个 $S*$ 是满足题目所有要求的路径(除非原问题不存在解)。

  于是定理 3.4 创造了无限可能。

做法

  我们只需要枚举一条非内部边,非树边 $e (x, y)$,且满足 $prt (x) \neq prt (y)$ 且 $d_s (prt (x)) \leqslant d_s (prt (y))$,然后用 $d_{s}(x) + w (e) + d_{t}(y)$ 去更新答案即可。

  虽然证明很复杂,但是算法却异常简单。

代码

 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 #define pb push_back
 4 #define _(d) while(d(isdigit(ch=getchar())))
 5 using namespace std;
 6 int R(){
 7     int x;bool f=1;char ch;_(!)if(ch=='-')f=0;x=ch^48;
 8     _()x=(x<<3)+(x<<1)+(ch^48);return f?x:-x;}
 9 const int N=5e5+5;
10 int n,m,head[N],cnt,flag[N<<1],fa[N],ans=2e9;
11 struct edge{int fro,to,nex,w;}e[N<<1];
12 void add(int s,int t,int w){e[++cnt]=(edge){s,t,head[s],w},head[s]=cnt;}
13 struct node{
14     int x,w;
15     bool friend operator <(node a,node b){return a.w>b.w;}
16 };priority_queue<node>q;
17 int dis[2][N];
18 void Dij(int s,int f){
19     memset(dis[f],0x3f,sizeof dis[f]);
20     dis[f][s]=0;
21     q.push((node){s,0});
22     while(!q.empty()){
23         node now=q.top();q.pop();
24         if(dis[f][now.x]!=now.w)continue;
25         for(int k=head[now.x],v;k;k=e[k].nex)
26             if(dis[f][now.x]+e[k].w<dis[f][v=e[k].to])
27                 dis[f][v]=dis[f][now.x]+e[k].w,q.push((node){v,dis[f][v]});
28     }
29     return;
30 }
31 vector<int> g[N];
32 void dfs(int u){
33     for(int i=g[u].size()-1,v;~i;i--){
34         if(dis[0][v=g[u][i]]+dis[1][v]!=dis[0][n])fa[v]=fa[u];
35         else fa[v]=v;
36         dfs(v);
37     }
38 }
39 int main(){
40     n=R(),m=R();
41     for(int i=1,u,v,w;i<=m;i++)
42         u=R(),v=R(),w=R(),add(u,v,w),add(v,u,w);
43     Dij(1,0),Dij(n,1);
44     for(int i=1;i<=n;i++)
45         for(int k=head[i],v;k;k=e[k].nex)
46             if(dis[1][i]==dis[1][v=e[k].to]+e[k].w){
47                 flag[k]=1,g[v].pb(i);
48                 break;
49             }
50     fa[n]=n,dfs(n);
51     for(int k=1,u,v,w;k<=cnt;k++)
52         if(!flag[k]&&dis[0][u=e[k].fro]<=dis[0][v=e[k].to]&&fa[u]!=fa[v]&&dis[0][u]+(w=e[k].w)+dis[1][v]!=dis[0][n])
53             ans=min(ans,dis[0][u]+w+dis[1][v]);
54     printf("%d\n",ans);
55     return 0;
56 }
View Code

 

转载至:http://www.cnblogs.com/yyf0309/p/8563071.html

转载于:https://www.cnblogs.com/chmwt/p/10648549.html

Guess you like

Origin blog.csdn.net/weixin_30919571/article/details/94788973