UVA 11478 halum (differential constraint)

Source: Internet
Author: User

Title Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651

Ideas

Differential constraint system.

Set the operation on node U and Sum[u], then the Edge (u,v) weight is d-sum[v]+sum[u]. For the biggest problem with the minimum value we think of the two-point answer, set two points to X, then the problem becomes the question whether there is a solution to determine the minimum value of x. For weights we have inequalities d-sum[v]+sum[u]>=x = = sum[v]<=sum[u]+ (d-x), so that a differential constraint system can be established.

No solution: If the minimum value of 1 o'clock is still not true.

Arbitrary solution: True if the minimum value is r+1.

Otherwise, the maximum value of the two-part answer, when the graph has a negative weight loop time Division constraint system without solution that is two-point answer is not established.

Code

1#include <cstdio>2#include <cstring>3#include <vector>4#include <queue>5 using namespacestd;6 7 Const intMAXN = ++Ten;8 9 intn,m;Ten structEdge {intu,v,w; One }; Avector<int>G[MAXN]; -Vector<edge>es; - voidAddedge (intUintVintW) { the Es.push_back (Edge) {u,v,w}); -     intM=es.size (); G[u].push_back (M-1); - } - BOOLSPFA () { +queue<int>Q; -     intINQ[MAXN],D[MAXN],CNT[MAXN]; +memset (INQ,0,sizeof(INQ)); Amemset (CNT,0,sizeof(CNT)); at      for(intI=1; i<=n;i++) -d[i]=0, inq[i]=1, Q.push (i); -      while(!Q.empty ()) { -         intU=q.front (); Q.pop (); inq[u]=0; -          for(intI=0; I<g[u].size (); i++) { -Edge e=Es[g[u][i]]; in             intv=e.v; -             if(d[v]>d[u]+e.w) { tod[v]=d[u]+E.W; +                 if(!Inq[v]) { -inq[v]=1, Q.push (v); the                     if(++cnt[v]> (n))return false; *                 } $             }Panax Notoginseng         } -     } the     return true; + } A BOOLCanintx) { the      for(intI=0; I<es.size (); i++) es[i].w-=x; +     BOOLans=SPFA (); -      for(intI=0; I<es.size (); i++) es[i].w+=x; $     returnans; $ } -  - intMain () { the      while(SCANF ("%d%d", &n,&m) = =2) { - es.clear ();Wuyi          for(intI=1; i<=n;i++) g[i].clear (); the         intu,v,w; -         intL=0, r=0; Wu          for(intI=0; i<m;i++) { -scanf"%d%d%d",&u,&v,&W); AboutAddedge (U,V,W); R=Max (r,w); $         } -         if(Can (r+1)) printf ("infinite\n"); -         Else if(!can (1)) printf ("No solution\n"); -         Else { A              while(l<R) { +                 intm=l+ (r-l+1)/2; the                 if(Can (M)) L=m;Elser=m-1; -             } $printf"%d\n", L); the         } the     } the     return 0; the}

UVA 11478 halum (differential constraint)

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.