UVA 11090 Going in Cycle!!(Bellman-Ford推断负圈)
题意:给定一个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推断负圈)的更多相关文章
- UVA 11090 - Going in Cycle!!(Bellman-Ford)
UVA 11090 - Going in Cycle!! option=com_onlinejudge&Itemid=8&page=show_problem&category= ...
- UVA - 11090 - Going in Cycle!!(二分+差分约束系统)
Problem UVA - 11090 - Going in Cycle!! Time Limit: 3000 mSec Problem Description You are given a we ...
- 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)
layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...
- uva 558 - Wormholes(Bellman Ford判断负环)
题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...
- UVA 11090 Going in Cycle!! SPFA判断负环+二分
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 11090 - Going in Cycle!! SPFA
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 11090 Going in Cycle!!【Bellman_Ford】
题意:给出n个点m条边的加权有向图,求平均值最小的回路 自己想的是用DFS找环(真是too young),在比较找到各个环的平均权值,可是代码实现不了,觉得又不太对 后来看书= =好巧妙的办法, 使用 ...
- UVA 11090 Going in Cycle!!(二分答案+判负环)
在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...
- UVA 11090 Going in Cycle!! 环平均权值(bellman-ford,spfa,二分)
题意: 给定一个n个点m条边的带权有向图,求平均权值最小的回路的平均权值? 思路: 首先,图中得有环的存在才有解,其次再解决这个最小平均权值为多少.一般这种就是二分猜平均权值了,因为环在哪也难以找出来 ...
随机推荐
- 为什么我们须要复杂的password
前两天我打开邮箱一看.收到公司1331一封要求改动邮箱password的邮件. 为什么我们须要一个复杂的password呢?尽管我一直以来设置的password都非常复杂.可是公司这次要求改动pass ...
- git --- ! [rejected] master -> master (non-fast-forward)
如何解决failed to push some refs to git Administrator@PC-20150110FGWU /K/cocos2d/yc (master) $ git push ...
- Hive里的分区、分桶、视图和索引再谈
福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 Java全栈大联盟 ...
- Fragment-传递参数
在关Fragment间参数的传递,有两种情况: 第一种情况:同一个container中不同fragment间的参数传递.这种情况一般发生在fragment跳转时,上一个Fragment将参数传递给下一 ...
- pytest使用问题总结
问题一.AttributeError: module 'pytest' has no attribute 'allure'解决方法:pip3 uninstall pytest-allure-adapt ...
- C++集合运算函数总结 & 需要有序集合的操作
前提:两个集合已经有序.merge() //归并两个序列,元素总个数不变,只是将两个有序序列归并为一个有序序列.set_union() //实现求集合A,B的并.set_difference()//实 ...
- hdoj-1289-A Bug's Life【种类并查集】
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- javaEE之------Spring-----》 AspectJ注解
前面介绍了下Spring中的切面技术.如今说下採用注解的方式进行切面 首先肯定和之前的一样.须要一个自己主动代理的注解类 AnnotationAwareAspectJAutoProxyCreator ...
- Codeforces Round #316 (Div. 2) A B C
A. Elections time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- java.lang.ClassNotFoundException: org.springframework.web.content.ContextLoaderListener
1.错误描写叙述 严重: Error configuring application listener of class org.springframework.web.content.Context ...