UVa 11090 Going in Cycle!! (Bellman_Ford)

时间:2023-02-16 17:42:32

题意:给定一个加权有向图,求平均权值最小的回路。

析:先十分答案,假设答案是 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;
}

  

UVa 11090 Going in Cycle!! (Bellman_Ford)的更多相关文章

  1. UVA 11090 - Going in Cycle&excl;&excl;(Bellman-Ford)

    UVA 11090 - Going in Cycle!! option=com_onlinejudge&Itemid=8&page=show_problem&category= ...

  2. UVA - 11090 - Going in Cycle&excl;&excl;(二分&plus;差分约束系统)

    Problem  UVA - 11090 - Going in Cycle!! Time Limit: 3000 mSec Problem Description You are given a we ...

  3. UVa 11090 Going in Cycle&excl;&excl;【Bellman&lowbar;Ford】

    题意:给出n个点m条边的加权有向图,求平均值最小的回路 自己想的是用DFS找环(真是too young),在比较找到各个环的平均权值,可是代码实现不了,觉得又不太对 后来看书= =好巧妙的办法, 使用 ...

  4. UVA 11090 Going in Cycle&excl;&excl;

    要求给定的图的中平均权值最小的环,注意处理自环的情况就能过了. 按照w1+w2+w3+….wn < n*ave的不等式,也就是(w1-ave) + (w2-ave) +…..(wn-ave) & ...

  5. UVA 11090 Going in Cycle&excl;&excl; SPFA判断负环&plus;二分

    原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. UVA 11090 - Going in Cycle&excl;&excl; SPFA

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  7. UVA 11090 Going in Cycle&excl;&excl;(二分答案&plus;判负环)

    在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...

  8. UVA 11090 Going in Cycle&excl;&excl; 环平均权值(bellman-ford,spfa,二分)

    题意: 给定一个n个点m条边的带权有向图,求平均权值最小的回路的平均权值? 思路: 首先,图中得有环的存在才有解,其次再解决这个最小平均权值为多少.一般这种就是二分猜平均权值了,因为环在哪也难以找出来 ...

  9. UVA 11090 Going in Cycle&excl;&excl;&lpar;Bellman-Ford推断负圈&rpar;

    题意:给定一个n个点m条边的加权有向图,求平均权值最小的回路. 思路:使用二分法求解.对于每个枚举值mid,推断每条边权值减去mid后有无负圈就可以. #include<cstdio> # ...

随机推荐

  1. Java动态代理全面分析

    代理模式 解说:给某一个对象提供一个代理,并由代理对象控制对原对象的引用: 代理模式需要以下几个角色: 1  主题:规定代理类和真实对象共同对外暴露的接口: 2  代理类:专门代理真实对象的类: 3 ...

  2. Auty自动化测试框架第六篇——垃圾代码回收、添加suite支持

    [本文出自天外归云的博客园] 垃圾代码回收 添加脚本恢复机制,因为框架会自动生成一些代码,如果代码生成后出现问题导致代码没有正常删除掉,则会造成代码垃圾,在auty目录添加recovery.py文件: ...

  3. Android Studio-导入External Libraries

    1.导入本地Libraries 1 拷贝 gson-2.3.1.jar(gson-2.3.1.jar为例)到 app/libs 目录下 2 在app/build.gradle的 dependencie ...

  4. semantic-ui使用gulp执行build-css报错

    1.执行gulp build-css报错 [09:40:49] Starting 'build-css'... Building CSS Potentially unhandled rejection ...

  5. CF GYM 100703G Game of numbers

    题意:给n个数,一开始基数为0,用这n个数依次对基数做加法或减法,使基数不超过k且不小于0,输出最远能运算到的数字个数,输出策略. 解法:dp.dp[i][j]表示做完第i个数字的运算后结果为j的可能 ...

  6. unlocker208安装之后看不到Apple macos选项&comma;解决办法&period;

    前段时间升级了win10,最新的unlocker208安装以后看不到mac os的选项,为什么呢?我们在管理员允许win-install.cmd的过程中,会在cmd中看到这么一句话:LookupErr ...

  7. 给想上MIT的牛学生说几句

    [来信] 老师您好! 非常冒昧的来打搅您,仅仅是在学习上实在有些困惑才来向您求教一番. 我是计算机科学与技术的大一学生,我非常喜欢我自己的专业,可是学校里讲的东西太慢,太浅,所以我一般都是自学,我在自 ...

  8. SpringBoot19 集成SpringSecurity01 -&gt&semi; 环境搭建、SpringSecurity验证

    1 环境搭建 1.1 创建一个SpringBoot项目 项目脚手架 -> 点击前往 1.2 创建一个Restful接口 新建一个Controller类即可 package com.example ...

  9. 考前停课集训 Day7 嘞

    Day7 正如一个大佬提醒的那样,棕名是会被嘲讽的 果然…… 在洛谷里…… 算了. 不必在意. 马上就要退役了. NOIP,开始的地方,也是结束的地方. 如果一群OIer比你小 还会嘲讽你, 你就该退 ...

  10. Autohotkey window 下宏键盘、宏命令开发入门