要求给定的图的中平均权值最小的环,注意处理自环的情况就能过了。

按照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!!的更多相关文章

  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 Going in Cycle!! SPFA判断负环+二分

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

  4. UVA 11090 - Going in Cycle!! SPFA

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

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

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

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

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

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

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

  8. UVa 11090 Going in Cycle!! (Bellman_Ford)

    题意:给定一个加权有向图,求平均权值最小的回路. 析:先十分答案,假设答案是 ans,那么有这么一个回路,w1+w2+w3+...+wk < k*ans,这样就是答案太大,然后移项可得,(w1- ...

  9. UVA 11090 Going in Cycle!!(Bellman-Ford推断负圈)

    题意:给定一个n个点m条边的加权有向图,求平均权值最小的回路. 思路:使用二分法求解.对于每个枚举值mid,推断每条边权值减去mid后有无负圈就可以. #include<cstdio> # ...

随机推荐

  1. asp.net Hierarchical Data

    Introduction A Hierarchical Data is a data that is organized in a tree-like structure and structure ...

  2. HTML招聘简历解析

    使用 jsoup 对 HTML 文档进行解析和操作 Jsoup解析html简历与dom4j解析xml是一个道理:首先必须知道html的格式,不知道格式,无法解析.根据格式,再将需要的内容通过下面的方法 ...

  3. WCF编程系列(六)以编程方式配置终结点

    WCF编程系列(六)以编程方式配置终结点   示例一中我们的宿主程序非常简单:只是简单的实例化了一个ServiceHost对象,然后调用open方法来启动服务.而关于终结点的配置我们都是通过配置文件来 ...

  4. iss 防火墙

    控制面板\系统和安全\Windows 防火墙\允许的程序 如下图 将万维网服务(HTTP)打勾即可访问你的网站. 转自:http://bbs.pcbeta.com/viewthread-604506- ...

  5. (已实现)相似度到大数据查找之Mysql 文章匹配的一些思路与提高查询速度

    需求,最近实现了文章的原创度检测功能,处理思路一是分词之后做搜索引擎匹配飘红,另一方面是量化词组,按文章.段落.句子做数据库查询,功能基本满足实际需求. 接下来,还需要在海量大数据中快速的查找到与一句 ...

  6. OC4_NSString操作

    // // main.m // OC4_NSString操作 // // Created by zhangxueming on 15/6/10. // Copyright (c) 2015年 zhan ...

  7. 2.MySQL入门基本操作初体验

    启动和关闭mysql服务器: 一.启动方式 1.使用 mysqld 脚本启动:/etc/inint.d/mysqld start 2.使用 守护进程safe_mysqld 启动:safe_mysqld ...

  8. 【转】如何编译安装PHP扩展

    本文参考 一开始安装PHP的时候,我们并不知道需要哪些扩展,所以只有等到我们真正用到的时候才想办法去安装. 安装PHP扩展最简单的办法就是 sudo apt-get install php5-xxx ...

  9. JQuery上传控件 jUploader 使用

    jUploader 1.0 Demo Download: jquery.jUploader-1.01.js 9.75kb Download: jquery.jUploader-1.01.min.js ...

  10. 重新开始学习c#啦,希望能坚持下去!

    过了这么多年,还是感觉自己喜欢C#,喜欢编程,虽然自己什么技术也没有:做的项目也不算是项目: