Going in Cycle!! UVA - 11090(二分+判断环路 )
Posted wtsruvf
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Going in Cycle!! UVA - 11090(二分+判断环路 )相关的知识,希望对你有一定的参考价值。
题意:
给定一个n个点m条边的加权有向图,求平均权值最小的回路
解析:
首先肯定是想到找出环路 然后。。呵。。呵。。呵呵。。。
显然不现实!!
二分大法好 。。。。去猜结果 然后带入验证 。。。真是的。。很过分!
嗯! 是的!
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #define rap(i, a, n) for(int i=a; i<=n; i++) #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 10010, INF = 0x7fffffff; int head[maxn], vis[maxn], ans[maxn]; double d[maxn]; int cnt, n, m; struct node { int v, next; double w; }Node[maxn]; void add(int u, int v, double w) { Node[cnt].v = v; Node[cnt].w = w; Node[cnt].next = head[u]; head[u] = cnt++; } int spfa(int s) { for(int i=0; i<=n; i++) d[i] = INF; mem(ans, 0); mem(vis, 0); queue<int> Q; Q.push(s); vis[s] = 1; d[s] = 0; while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = 0; for(int i=head[u]; i!=-1; i=Node[i].next) { node e = Node[i]; if(d[e.v] > d[u] + e.w) { d[e.v] = d[u] + e.w; if(!vis[e.v]) { Q.push(e.v); vis[e.v] = 1; if(++ans[e.v] >= n) return 1; } } } } return 0; } bool check(double x) { bool flag = 0; for(int i=0; i<cnt; i++) Node[i].w -= x; for(int i=1; i<=n; i++) if(spfa(i)) flag = 1; for(int i=0; i<cnt; i++) Node[i].w += x; return flag; } void init() { mem(head, -1); cnt = 0; } int main() { int T, kase = 0; scanf("%d", &T); while(T--) { init(); int u, v; double w, x = 0, y = 0; scanf("%d%d", &n, &m); for(int i=0; i<m; i++) { scanf("%d%d%lf", &u, &v, &w); add(u, v, w); y = max(y, w); } printf("Case #%d: ",++kase); if(!check(y+1)) printf("No cycle found. "); else { while(y - x > 1e-3) { double mid = x + (y-x)/(double)2; if(check(mid)) y = mid; else x = mid; } printf("%.2lf ",x); } } return 0; }
以上是关于Going in Cycle!! UVA - 11090(二分+判断环路 )的主要内容,如果未能解决你的问题,请参考以下文章
UVA11090 Going in Cycle!! [spfa负环]
Going in Cycle!! UVA - 11090(二分+判断环路 )