UVA 11090 Going in Cycle!!
要求给定的图的中平均权值最小的环,注意处理自环的情况就能过了。
按照w1+w2+w3+….wn < n*ave的不等式,也就是(w1-ave) + (w2-ave) +…..(wn-ave) < 0,将每条边减去ave看是否存在负权回路,然后不断二分,由于保留两位小数,所以至少二分log(10^7)= 30次。
代码:
#include <iostream>
#include <sstream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <string>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define pi acos(-1.0)
#define pb push_back
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mp(a, b) make_pair((a), (b))
#define in freopen("in.txt", "r", stdin);
#define out freopen("out.txt", "w", stdout);
#define print(a) printf("%d\n",(a));
#define bug puts("********))))))");
#define stop system("pause");
#define Rep(i, c) for(__typeof(c.end()) i = c.begin(); i != c.end(); i++)
#define inf 0x0f0f0f0f using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> pii;
typedef vector<pii> VII;
typedef vector<pii, int> VIII;
typedef VI:: iterator IT;
const int maxn = ; struct Edge
{
int from, to;
double dist;
}; struct Bellman_Ford
{
int n, m;
vector<Edge> edges;
VI G[maxn];
double d[maxn];
int p[maxn];
int inq[maxn];
int cnt[maxn]; void init(int n)
{
this->n = n;
for(int i = ; i < n; i++)
{
G[i].clear();
}
edges.clear();
} void add(int from, int to, double dist)
{
edges.pb((Edge)
{
from, to, dist
});
m = edges.size();
G[from].pb(m-);
}
bool negativeCircle(double L)
{
memset(cnt, , sizeof(cnt));
memset(inq, , sizeof(inq));
queue<int> q; for(int i = ; i < n; i++)
{
d[i] = ;
inq[] = ;
q.push(i);
}
while(!q.empty())
{
int u = q.front();
q.pop();
inq[u] = ;
for(int i = ; i < G[u].size(); i++)
{
Edge &e = edges[G[u][i]];
if(d[e.to] > d[u] + e.dist - L)
{
d[e.to] = d[u] + e.dist - L;
if(!inq[e.to])
{
q.push(e.to);
inq[e.to] = ;
}
if(++cnt[e.to] > n)
{
return true;
}
}
}
}
return false;
}
} solver; int main(void)
{ int T;
for(int t = scanf("%d", &T); t <= T; t++)
{
printf("Case #%d: ", t);
int n, m;
scanf("%d%d", &n, &m);
solver.init(n);
double ans = inf + ; double l = (double)inf*, r = ;
while(m--)
{
int u, v;
double w;
scanf("%d%d%lf", &u, &v, &w);
u--, v--;
if(u == v)
ans = min(ans, w);
solver.add(u, v, w);
l = min(l, w);
r = max(r, w);
}
int flag= ;
double mid = (l+r)/;
for(int i = ; i < ; i++)
{
if(solver.negativeCircle(mid))
flag = , r = mid-0.001;
else l = mid+0.001;
mid = (l+r)/;
}
if(flag)
printf("%.2f\n", min(ans, mid));
else if(ans < inf)
printf("%.2f\n", ans);
else puts("No cycle found.");
}
return ;
}
UVA 11090 Going in Cycle!!的更多相关文章
- 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!! 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条边的带权有向图,求平均权值最小的回路的平均权值? 思路: 首先,图中得有环的存在才有解,其次再解决这个最小平均权值为多少.一般这种就是二分猜平均权值了,因为环在哪也难以找出来 ...
- UVa 11090 Going in Cycle!! (Bellman_Ford)
题意:给定一个加权有向图,求平均权值最小的回路. 析:先十分答案,假设答案是 ans,那么有这么一个回路,w1+w2+w3+...+wk < k*ans,这样就是答案太大,然后移项可得,(w1- ...
- UVA 11090 Going in Cycle!!(Bellman-Ford推断负圈)
题意:给定一个n个点m条边的加权有向图,求平均权值最小的回路. 思路:使用二分法求解.对于每个枚举值mid,推断每条边权值减去mid后有无负圈就可以. #include<cstdio> # ...
随机推荐
- ASP连接11种数据库的常用语法
1.Access数据库的DSN-less连接方法: 以下为引用的内容: set adocon=Server.Createobject("adodb.connection") ado ...
- .Net获取iis版本
有以下办法获取iis版本. DirectoryEntry getEntity = new DirectoryEntry("IIS://localhost/W3SVC/INFO"); ...
- 崩溃信息:Message from debugger: Terminated due to signal 9
是因为你在调试的时候主动了结束了程度,如上滑结束了程序
- IOS 应用程序启动加载过程(从点击图标到界面显示)
今天帮同事解决问题的时候发现,程序BUG是由加载过程引起的.所以当局部代码没有问题,但是程序一运行却总不是我们想要结果的时候,我们应该想想是不是因为我们忽略了试图加载过程的原因.下面我们用一个例子来简 ...
- 2) LINQ编程技术内幕--yield return
yield return 使用.NET的状态机生成器 yield return关键词组自动实现IDisposable,使用这个可枚举的地方, 还存在一个隐含的try finally块. 示例代码: c ...
- Json字符与Json对象的相互转换
Json字符与Json对象的相互转换方式有很多,接下来将为大家一一介绍下,感兴趣的朋友可以参考下哈,希望可以帮助到你 1>jQuery插件支持的转换方式: 复制代码 代码如下: $.parseJ ...
- 在不同版本的 IIS 上使用 ASP.NET MVC
ASP.NET MVC Framework 依赖于 URL 路由.为了利用 URL 路由,可能不得不在 Web 服务器上执行额外的配置步骤.这些步骤取决于 Internet Information S ...
- Java实战之03Spring-03Spring的核心之AOP(Aspect Oriented Programming 面向切面编程)
三.Spring的核心之AOP(Aspect Oriented Programming 面向切面编程) 1.AOP概念及原理 1.1.什么是AOP OOP:Object Oriented Progra ...
- 用Python进行语音信号处理
1.语音信号处理之时域分析-音高追踪及其Python实现 2.语音信号处理之时域分析-音高及其Python实现 参考: 1.NumPy
- fsockopen/curl/file_get_contents效率比较
前面小节 PHP抓取网络数据的6种常见方法 谈到了 fsockopen,curl与file_get_contents 的使用方法,虽然它们都能达到同一个使用目的,但是它们之间又有什么区别呢? 先谈谈c ...