"Differential constraint system/dfs version SPFA" bzoj3436-Small K Farm

Source: Internet
Author: User

"The main topic"

A total of n farms, the following three kinds of description: Farm a more than farm b at least planted a C units of the crop, farm a than farm B to grow more than a C units of the crop, farm A and farm B planted as many crops. Ask if there is a possibility.

Ideas

Farm A has at least more than farm B plants planted with C units: A>=b+c→b<=a-c, A to B with a-C edge.

Farm A has grown more than farm B with a C-unit crop: A<=b+c, which is connected by B to a side of C.

Farm A has as many crops as farm B: a=b→b<=a<=b, with a 0 edge to each other.

SPFA to judge negative ring, to use the DFS version. DFS version of the direct reference to the Rausen seniors ... It's really fast to fly (; ′⌒ ')

1#include <bits/stdc++.h>2 using namespacestd;3 Const intmaxn=10000+ -;4 structEdge5 {6     intTo,len;7 };8 intn,m;9Vector<edge>E[MAXN];Ten intVis[maxn],dis[maxn],flag; One  A voidAddedge (intUintVintW) - { - E[u].push_back (Edge) {v,w}); the } -  - voidInit () - { +scanf"%d%d",&n,&m); -      for(intI=1; i<=m;i++) +     { A         intop,a,b,c; atscanf"%d",&op); -         if(op==1)//A-b>=c -         { -scanf"%d%d%d",&a,&b,&c); -Addedge (a,b,-c); -         } in         if(op==2) -         { toscanf"%d%d%d",&a,&b,&c); + Addedge (b,a,c); -         } the         if(op==3) *         { $scanf"%d%d",&a,&b);Panax NotoginsengAddedge (A, B,0); Addedge (B,a,0); -         } the     } + } A  the voidSPFA (intfr) + { -     if(VIS[FR]) $     { $flag=1; -         return; -     } thevis[fr]=1; -      for(intI=0; I<e[fr].size (); i++)Wuyi     { the         intto=e[fr][i].to,len=E[fr][i].len; -         if(dis[fr]+len<Dis[to]) Wu         { -dis[to]=dis[fr]+Len; About SPFA (to); $             if(flag)return; -         } -     } -vis[fr]=0; A } +  the voidSolve () - { $memset (Vis,0,sizeof(Vis)); the      for(intI=1; i<=n;i++) the     { the SPFA (i); the         if(flag) Break; -     } inPuts (flag?)"No":"Yes"); the } the  About intMain () the { the init (); the solve (); +     return 0; -}

"Differential constraint system/dfs version SPFA" bzoj3436-Small K Farm

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.