Uva11090 going in cycle !! --- Binary + spfa negative ring

Source: Internet
Author: User

For a directed graph with weights, check whether a ring exists. If so, the average value of the weighted average value at the top of the output ring is the smallest.

It can be very violent .. But it obviously times out.

It seems that the method is clever. The binary average value is used to subtract all edge weights from the binary value, and then spfa determines whether there is a negative ring.

If a negative ring exists, the mean value of Edge Weight of all rings in the graph must be greater than that of enumeration values.

The opposite is small. If most of the enumerated values do not have a negative ring, it indicates that the graph does not have a ring.


# Include <iostream> # include <cstring> # include <string> # include <cstdio> # include <cmath> # include <algorithm> # include <vector> # include <queue> # include <map> # define INF 0x3f3f3f # define EPS 1e-6 # define ll _ int64using namespace STD; struct node {int V, next; double W;} e [10000]; double d [110]; int INQ [110], outq [110], head [110], h, n, m; void Init () {memset (Head,-1, sizeof head); H = 0;} void addedge (int A, int B, do Uble c) {e [H]. V = B; E [H]. W = C; E [H]. next = head [a]; head [a] = H ++;} int spfa (INT St, double cut) {for (INT I = 0; I <= N; I ++) d [I] = 1e15; memset (INQ, 0, sizeof INQ); memset (outq, 0, sizeof outq); D [st] = 0; INQ [st] = 1; queue <int> q; q. push (ST); While (! Q. empty () {int x = Q. front (); q. pop (); INQ [x] = 0; outq [x] ++; If (outq [x]> N) return 0; // there is a negative ring for (INT I = head [X]; I! =-1; I = E [I]. next) {If (d [E [I]. v]> d [x] + E [I]. w-cut) {d [E [I]. v] = d [x] + E [I]. w-cut; If (! INQ [E [I]. v]) {INQ [E [I]. v] = 1; q. push (E [I]. v) ;}}}return 1 ;}int main () {int A, B, I, T, T = 1; double C, mid, RI, le; scanf ("% d", & T); While (t --) {printf ("case # % d:", t ++); Init (); scanf ("% d", & N, & M); double mMax = 0; while (M --) {scanf ("% d % lf ", & A, & B, & C); addedge (A, B, C); mMax = max (mMax, c) ;}/ * If (spfa (1, mMax + 1) // determine whether to connect // here the source point is only 1 {printf ("No cycle found. \ n "); continue;} */Le = 0; rI = mMax + 5; while (ri-Le> EP S) {// printf ("Le: % lf Ri: % lf \ n", le, RI); int flag = 0; Mid = (RI + le) * 0.5; for (I = 1; I <= N; I ++) // wa... {If (! Spfa (I, mid) {flag = 1; break ;}} if (! Flag) Le = mid; else rI = mid;} If (Ri = mMax + 5) printf ("No cycle found. \ n "); else printf (" %. 2lf \ n ", RI);} 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.