UVA 11090 going in cycle!! Ring average weight (BELLMAN-FORD,SPFA, two points)

Source: Internet
Author: User

Test instructions: Given a weighted graph with a n-point M-edge, the average weight of the loop with the lowest average weight is calculated.

Idea: First of all, the figure has to have the existence of a ring, and then solve the minimum average weight of how much. In general, this is the second guess of the average weight, because the ring is difficult to find out where, there may be a side belonging to multiple rings. For each guessed average, if the corresponding ring exists, then the weight of each edge of the ring minus the average value, the SPFA algorithm can be used to determine whether the existence of a ring.

Assume that the weighted value of each edge on the ring is: W1+w2+...+wk.

Formula: W1+w2+...+wk<k*even equivalent (W1-even) + (W2-even) + ... (Wk-even) < 0. That is, after updating the edge right should I have a ring exists.

For a guessing average weight of mid, if the ring cannot be found, the mid should be larger.

1#include <bits/stdc++.h>2 #defineLL Long Long3 #definePII pair<int,int>4 #defineINF 0x7f7f7f7f5 using namespacestd;6 Const intn=60606;7vector<int>Vect[n];8 structnode9 {Ten     int  from, to; One     DoubleCost ; A node () {}; -Nodeint  from,intTo,intCost): from( from), to and cost (cost) {}; - }edge[n]; the intedge_cnt; - intbig, small; - voidAdd_node (int  from,intTo,DoubleCost ) - { +Edge[edge_cnt]=node ( from, to, cost); -vect[ from].push_back (edge_cnt++); + } A  at intInq[n], cnt[n]; - DoubleDist[n]; - BOOLSPFA (intNDoubleq) - { -memset (INQ,0,sizeof(INQ)); -memset (CNT,0,sizeof(CNT)); indeque<int>que; -      for(intI=1; i<=n; i++) dist[i]=0.0, inq[i]=1, Que.push_back (i);//because the negative ring is judged, the dist is initialized to 0.  to      while(!que.empty ()) +     { -         intx=Que.front (); Que.pop_front (); theinq[x]=0; *          for(intI=0; I<vect[x].size (); i++) $         {Panax NotoginsengNode e=Edge[vect[x][i]]; -             if(dist[e.to]>dist[x]+e.cost-q) the             { +dist[e.to]=dist[x]+e.cost-Q; A                 if(!inq[e.to]) the                 { +inq[e.to]=1; - Que.push_back (e.to); $                     if(++cnt[e.to]>N) $                         return true; -                 } -             } the         } -     }Wuyi     return false; the } -  Wu DoubleCalintN) - { About     DoubleL=small, R=big, ans=0.0; $      while(r-l>1e-3) -     { -         DoubleMid= (L+R)/2; -         if(SPFA (n, mid)) R=mid;//have negative ring A         ElseL=mid; +     } the     returnl; - } $  the intMain () the { the    //freopen ("Input.txt", "R", stdin); the     intN, M, T, a, B, C, j=0; -Cin>>T; in      while(t--) the     { thescanf"%d%d",&n,&m); AboutEdge_cnt=0; the          for(intI=0; i<=n; i++) vect[i].clear (); theMemset (Edge,0,sizeof(Edge)); thebig=0; +Small=INF; -  the          for(intI=0; i<m; i++)Bayi         { thescanf"%d%d%d",&a,&b,&c); the Add_node (a,b,c); -Small=min (small,c); -big=Max (big, c); the         } the         if(!SPFA (N, big+1)) printf ("Case #%d:no Cycle found.\n", ++j); the         Elseprintf"Case #%d:%.2f\n", ++J, Cal (N)); the     } -     return 0; the}
AC Code

UVA 11090 going in cycle!! Ring average weight (BELLMAN-FORD,SPFA, two points)

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.