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

思路:使用二分法求解。对于每个枚举值mid,推断每条边权值减去mid后有无负圈就可以。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#define eps 1e-4
#define LL long long
using namespace std; const int maxn = 100 + 5;
const int INF = 0x3f3f3f3f; //Bellman-Ford算法
struct Edge {
int from, to;
double dist;
Edge(int u = 0, int v = 0, double d = 0) : from(u), to(v), dist(d) {
}
};
struct BellmanFord{
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
bool inq[maxn];
double d[maxn];
int p[maxn];
int cnt[maxn]; void init(int n) {
this->n = n;
for(int i = 0; i < n; i++) G[i].clear();
edges.clear();
} void AddEdges(int from, int to, int dist) {
edges.push_back(Edge(from, to, dist));
m = edges.size();
G[from].push_back(m-1);
} bool negetiveCycle() {
queue<int> Q;
memset(inq, 0, sizeof(inq));
memset(cnt, 0, sizeof(cnt));
for(int i = 0; i < n; i++) {
d[i] = 0; inq[0] = true; Q.push(i);
}
while(!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = false;
for(int i = 0; i < G[u].size(); i++) {
Edge& e = edges[G[u][i]];
if(d[e.to]>d[u]+e.dist) {
d[e.to] = d[u] + e.dist;
p[e.to] = G[u][i];
if(!inq[e.to]) {
Q.push(e.to);
inq[e.to] = true;
if(++cnt[e.to] > n) return true;
}
}
}
}
return false;
} } solver; int n, m;
bool test(double w) {
for(int i = 0; i < m; i++) solver.edges[i].dist -= w;
int t = solver.negetiveCycle();
for(int i = 0; i < m; i++) solver.edges[i].dist += w;
return t;
} int kase;
int main() {
freopen("input.txt", "r", stdin);
int t; cin >> t;
while(t--) {
cin >> n >> m;
solver.init(n);
for(int i = 0; i < m; i++) {
int u, v, d;
cin >> u >> v >> d;
u--; v--;
solver.AddEdges(u, v, d);
}
double L = -10000001, R = 10000001;
if(!test(R)) printf("Case #%d: No cycle found.\n", ++kase);
else {
while(R-L>eps) {
double M = (R+L)/2;
if(test(M)) R = M;
else L = M;
}
printf("Case #%d: %.2lf\n", ++kase, R);
}
}
return 0;
}

UVA 11090 Going in Cycle!!(Bellman-Ford推断负圈)的更多相关文章

  1. UVA 11090 - Going in Cycle!!(Bellman-Ford)

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

  2. UVA - 11090 - Going in Cycle!!(二分+差分约束系统)

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

  3. 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)

    layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...

  4. uva 558 - Wormholes(Bellman Ford判断负环)

    题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...

  5. UVA 11090 Going in Cycle!! SPFA判断负环+二分

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

  6. UVA 11090 - Going in Cycle!! SPFA

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

  7. UVa 11090 Going in Cycle!!【Bellman_Ford】

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

  8. UVA 11090 Going in Cycle!!(二分答案+判负环)

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

  9. UVA 11090 Going in Cycle!! 环平均权值(bellman-ford,spfa,二分)

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

随机推荐

  1. hdu1234 开门人和关门人 (等价转换)

    开门人和关门人 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  2. 配置远程访问阿里云服务器的Redis

    1.默认情况Redis不是在后台运行,我们需要修改把redis放在后台运行:daemonize yes 2.Redis安全策略默认本机访问,所以远程访问的话需要将 bind 127.0.0.1加#注释 ...

  3. typeof 和 instanceof 的区别

    在JavaScript中我们想得到一个变量的类型,我们一般会用typeof 得到这个类型的 字符串,但是对于引用类型,typeof始终会返回一个"object",在我们js中有十个 ...

  4. logout命令用于退出当前登录的Shell

    logout命令用于退出当前登录的Shell

  5. Android中级第十讲--相机录像和查看系统相册图片

    博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 录像比较简单,开始录制: myCamera.unlock(); ...

  6. [Javascript] Classify JSON text data with machine learning in Natural

    In this lesson, we will learn how to train a Naive Bayes classifier and a Logistic Regression classi ...

  7. 正确理解Widget::Widget(QWidget *parent) :QWidget(parent)这句话(初始化列表中无法直接初始化基类的数据成员,所以你需要在列表中指定基类的构造函数)

    最近有点忙,先发一篇我公众号的文章,以下是原文. /********原文********/ 最近很多学习Qt的小伙伴在我的微信公众号私信我,该如何理解下面段代码的第二行QWidget(parent) ...

  8. 将bat批处理命令文件固定到任务栏

    将bat批处理命令文件固定到任务栏第一种方法:使用链接工具http://www.xstui.com/read/3451.在任务栏点击右键,移动到工具栏,勾选链接工具2.你会在通知栏左侧看到链接字样,将 ...

  9. 1.17 Python基础知识 - 迭代器和生成器初识

    可循环迭代的对象称为可迭代对象,迭代器和生成器函数是可迭代对象. 列表解析表达式:可以简单高效处理一个可迭代对象,并生成结果列表 示例代码: [ i ** 2 for i in range(10) ] ...

  10. Android HttpLoggingInterceptor的用法简介

    该拦截器用于记录应用中的网络请求的信息. 示例 OkHttpClient client = new OkHttpClient(); HttpLoggingInterceptor logging = n ...