UVa 11090 Going in Cycle!! (Bellman_Ford)
题意:给定一个加权有向图,求平均权值最小的回路。
析:先十分答案,假设答案是 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)的更多相关文章
- 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 Going in Cycle!!【Bellman_Ford】
题意:给出n个点m条边的加权有向图,求平均值最小的回路 自己想的是用DFS找环(真是too young),在比较找到各个环的平均权值,可是代码实现不了,觉得又不太对 后来看书= =好巧妙的办法, 使用 ...
- UVA 11090 Going in Cycle!!
要求给定的图的中平均权值最小的环,注意处理自环的情况就能过了. 按照w1+w2+w3+….wn < n*ave的不等式,也就是(w1-ave) + (w2-ave) +…..(wn-ave) & ...
- 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!!(二分答案+判负环)
在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...
- UVA 11090 Going in Cycle!! 环平均权值(bellman-ford,spfa,二分)
题意: 给定一个n个点m条边的带权有向图,求平均权值最小的回路的平均权值? 思路: 首先,图中得有环的存在才有解,其次再解决这个最小平均权值为多少.一般这种就是二分猜平均权值了,因为环在哪也难以找出来 ...
- UVA 11090 Going in Cycle!!(Bellman-Ford推断负圈)
题意:给定一个n个点m条边的加权有向图,求平均权值最小的回路. 思路:使用二分法求解.对于每个枚举值mid,推断每条边权值减去mid后有无负圈就可以. #include<cstdio> # ...
随机推荐
- npm 自身的升级
npm 是随 NodeJS 一起发布的包管理工具,默认采用的并不一定是最新版本,某些情况下(比如使用了某个IDE)需要最新版本的 npm , 咋办?单独升级,官方的几种说法的操作比较复杂,本经验就是说 ...
- combiner中使用状态模式
mapreduce中的combine过程 hadoop的map过程执行完成后,每一个map都可能会产生大量的本地输出,Combiner的作用就是对map端的输出先做一次合并,减少在map和reduce ...
- 04:Sysbench压测-innodb_flush_log_at_trx_commit,sync_binlog参数对性能的影响
目录 sysbench压测-innodb_flush_log_at_trx_commit,sync_binlog参数对性能的影响 一.OLTP测试前准备 二.MySQL 数据落盘的过程 三.参数说明 ...
- java后台读取配置文件中key与value -----demo2
/** * * @Title: getValue * @Description: TODO * @param key * @return import java.util.Properties; * ...
- windows下使用SQLPLUS制作BAT执行SQL文件
假如你把需要的SQL操作信息等均放入到一个SQL文件中,需要制作一个bat文件来执行这个sql文件,那么你的bat文件中,在sqlplus登录语句后的信息不能换行,换行的话则执行登录sqlplus后就 ...
- VB导入Excel到数据库软件(持续更新中。)
1.选择Excel文件版本 电脑上用的 Office2010 引用:Mircosoft Excel 14.0 Object Library 2.选择Excel文件 '选择文件公共变量 Public D ...
- Python 多人聊天工具 ( 多线程 )
程序实现: 1.单或多客户端使用 telnet 登陆服务端 ( 可远程 ) 进行会话 2.服务端实现登陆.注册.退出功能 3.客户端发送的消息会被广播到已经登陆的其他用户界面 4.连接到服务端后,可以 ...
- 增强MyEclipse提示功能
当我们使用MyEclipse编辑代码的时候按住“Ctrl+/”,就能获得代码提示,或者使用“.”的时候就会“点”出属性或方法等,而且提示还会有延迟. 如果我们想无论是按下“.”.“Ctrl+/”甚至是 ...
- 前端开发之JavaScript基础篇三
主要内容: 1.创建对象的几种方式 2.JavaScript内置对象 3.JavaScript错误--Throw.Try 和 Catch 4.JavaScript 表单验证 一.创建对象的几种方式 1 ...
- Linux实战教学笔记43:squid代理与缓存实践(二)
第6章 squid代理模式案例 6.1 squid传统正向代理生产使用案例 6.1.1 squid传统正向代理两种方案 (1)普通代理服务器 作为代理服务器,这是SQUID的最基本功能:通过在squi ...