题意:给定一个加权有向图,求平均权值最小的回路。
析:先十分答案,假设答案是 ans,那么有这么一个回路,w1+w2+w3+...+wk < k*ans,这样就是答案太大,然后移项可得,(w1-ans)+(w2-ans)+(w3-ans) + ..+(wk-ans) < 0,这样的话就判断是不是有负图就好了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include <bitset> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a, b, sizeof a) #define sz size() #define pu push_up #define pd push_down #define cl clear() #define all 1,n,1 #define FOR(x,n) for(int i = (x); i < (n); ++i) #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 50 + 10; const int mod = 1000; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c) { return r > 0 && r <= n && c > 0 && c <= m; } struct Edge{ int from, to; double dist; }; struct Bellman_Ford{ int n, m; vector<Edge> edges; vector<int> G[maxn]; bool inq[maxn]; double d[maxn]; int cnt[maxn]; void init(int n){ this->n = n; for(int i = 0; i < n; ++i) G[i].cl; edges.cl; } void addEdge(int from, int to, double c){ edges.pb((Edge){from, to, c}); G[from].pb(edges.sz-1); } bool bfs(){ queue<int> q; ms(inq, 0); ms(cnt, 0); inq[0] = 1; for(int i = 0; i < n; ++i){ d[i] = 0.0; q.push(i); } while(!q.empty()){ int u = q.front(); q.pop(); inq[u] = false; for(int i = 0; i < G[u].sz; ++i){ Edge &e = edges[G[u][i]]; if(d[e.to] > d[u] + e.dist){ d[e.to] = d[u] + e.dist; if(!inq[e.to]){ q.push(e.to); inq[e.to] = 1; if(++cnt[e.to] > n) return true; } } } } return false; } }; Bellman_Ford bell; bool judge(double m){ for(int i = 0; i < bell.edges.sz; ++i) bell.edges[i].dist -= m; bool ans = bell.bfs(); for(int i = 0; i < bell.edges.sz; ++i) bell.edges[i].dist += m; return ans; } int main(){ int T; cin >> T; for(int kase = 1; kase <= T; ++kase){ scanf("%d %d", &n, &m); bell.init(n); double l = 0.0, r = 0.0; for(int i = 0; i < m; ++i){ int u, v, c; scanf("%d %d %d", &u, &v, &c); --u, --v; bell.addEdge(u, v, c); r = max(r, c * 1.0); } printf("Case #%d: ", kase); if(!judge(r + 1.0)){ puts("No cycle found."); continue; } for(int i = 0; i < 30; ++i){ double m = (l + r) / 2.0; if(judge(m)) r = m; else l = m; } printf("%.2f\n", l); } return 0; }
时间: 2024-10-13 00:45:07