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> # ...
随机推荐
- List的add方法与addAll方法的区别
add是将传入的参数作为当前List中的一个Item存储,即使你传入一个List也只会另当前的List增加1个元素addAll是传入一个List,将此List中的所有元素加入到当前List中,也就是当 ...
- ACM——五位以内的对称素数
http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1026 五位以内的对称素数 时间限制(普 ...
- [python] 高效使用assert
Places to consider putting assertions: checking parameter types, classes, or values checking data st ...
- JavaScript学习笔记(1)——JavaScript简介
JavaScript一种解释性脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,该引擎为浏览器的一部分.JavaScript最早是用 ...
- 【Java咬文嚼字】关键字(一):super和this
这段时间一直在学Java,看了辣么多书以及博客,心痒也是着写写自己的学习心得. 这也算是新手篇:咬文嚼字Java中的关键字. 以关键字为第一篇博文也是考虑再三:1.本人基础也是薄弱 2.集跬步至千里 ...
- Android-ViewPagerIndicator-master 、Android-PullToRefresh 学习篇
最近在学习android,然后看到了很多有用的开源库.其中一个就是Android-ViewPagerIndicator :. 这是与ViewPager兼容的一个分页指示器库.分页指示器(Friends ...
- async:false同步请求,浏览器假死
// 异步请求导致数据错乱 // function get_num(){ // $("input[name='monitor']").eq(1).attr('checked',tr ...
- js清空web用户控件的值
假设你的用户控件里面有: <asp:DropDownList ID="DropDownList1" runat="server"> <asp: ...
- A-Frame 简介03
如果你想开始使用A-Frame可以通过以下几种方式: Play with CodePen Grab the Boilerplate Include the JS Build Install from ...
- Unity3D--学习太空射击游戏制作(三)
步骤四:创建敌人 创建敌人的方式与创建主角类似,不过敌人的行为需要由计算机来控制,它将从上方迎着主角缓慢飞出来,并左右来回移动: 01:创建Enemy.cs脚本,添加代码: using UnityEn ...