题目链接:http://vjudge.net/contest/143318#problem/A

题意: 求平均权值最小的回路。

分析:

平均权值不可能超过最大边,二分查,然后,由于是平均权值,就可以转换一下。

是否存在平均权值 < mid 的回路: w1 + w2 +.. +wk < k*mid

(w1-mid) + (w2-mid) +... + (wk-mid)<0;

只要精度够。 这个式子,就是一个负环,只要改变一下各条边的权值。

#include <bits/stdc++.h>
using namespace std; const int maxn = ; struct Edge
{
int from,to;
double dist;
}; 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 = ; i < n; i++) G[i].clear();
edges.clear();
} void AddEdge(int from, int to, double dist)
{
edges.push_back((Edge)
{
from, to, dist
});
m = edges.size();
G[from].push_back(m-);
} bool negativeCycle()
{
queue<int> Q;
memset(inq, , sizeof(inq));
memset(cnt, , sizeof(cnt));
for(int i = ; i < n; i++)
{
d[i] = ;
inq[] = true;
Q.push(i);
} while(!Q.empty())
{
int u = Q.front();
Q.pop();
inq[u] = false;
for(int i = ; 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;
}
}; BellmanFord solver; bool test(double x)
{
for(int i=; i<solver.m; i++)
{
solver.edges[i].dist -= x;
} bool ret = solver.negativeCycle();
for(int i=; i<solver.m; i++)
{
solver.edges[i].dist+=x;
}
return ret;
} int main()
{
int t;
scanf("%d",&t);
for(int kase = ; kase<=t; kase++)
{
int n,m;
scanf("%d%d",&n,&m);
solver.init(n);
double ub = ;
for(int i=; i<m; i++)
{
int u,v;
double w;
scanf("%d%d%lf",&u,&v,&w);
u--;
v--;
ub = max(ub,w);
solver.AddEdge(u,v,w);
}
printf("Case #%d: ",kase);
if(!test(ub+)) printf("No cycle found.\n"); else
{
double L = ,R=ub;
while(R-L>1e-)
{
double M = L +(R-L)/; if(test(M)) R = M;
else L = M; }
printf("%.2f\n",L);
}
} return ;
}

Uva 11090 在环中的更多相关文章

  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(最短路BellmanFord+ 二分判负环)

    layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...

  4. 在环中(Going in Cycle!!, UVa 11090)

    [题目描述] 给定一个 n 个点 m 条边的加权有向图,求平均权值最小的回路. [输入格式] 输入第一行为数据组数 T .每组数据第一行为图的点数 n 和边数 m (n ≤ 50).以下 m 行每行3 ...

  5. UVA 11090 Going in Cycle!! SPFA判断负环+二分

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

  6. UVA 11090 - Going in Cycle!! SPFA

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

  7. uva 11090

    I I U P C 2 0 0 6 Problem G: Going in Cycle!! Input: standard input Output: standard output You are ...

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

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

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

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

随机推荐

  1. log4net 配置应用

    (一). WinForm 或者 WPF 中的配置和应用 <?xml version="1.0" encoding="utf-8" ?> <co ...

  2. 关于ASP.NET Web API 客户端的请求报文中添加 Authorization

    当你使用客户端发送请求 Web API 的时候,因为API 有验证,所以你的请求报文中必须有”Authorization“,那么就需要手动添加了! HttpClient client = new Ht ...

  3. JAVA6开发WebService (二)——JAX-WS例子

    转载自http://wuhongyu.iteye.com/blog/807836 上一篇写了个最简单的小例子,只是为了说明JAVA6开发Web Service很方便,这一篇稍微深入一点,写个稍微有点代 ...

  4. Lua修改文件名

    local lfs = require "lfs"local curPath = "..\\new\\"local  totalFile = 0 local s ...

  5. 用vs2013编译lua源码方法(一)

    用vs2013编译lua源码方法 来源:网络    编辑:admin 1.下载lua源码:lua-5.2.3.tar.gz,解压 2.用vs2013建立一个win32工程: 1)下载后解压到一个目录下 ...

  6. JS读写cookie以及中文乱码解决

    本文地址:http://www.cnblogs.com/PiaoMiaoGongZi/p/4092489.html 转载请注明. Js获取所有的cookie信息: var cookiename = d ...

  7. Leetcode | Linked List Cycle I && II

    一.判断链表是否存在环,办法为: 设置两个指针(fast, slow),初始值都指向头,slow每次前进一步,fast每次前进二步,如果链表存在环,则fast必定先进入环,而slow后进入环,两个指针 ...

  8. PHP 上传大文件

  9. SPARK支持的常见文件格式

    SequenceFile读写文件Scala\java类型对应表

  10. Hibernate n+1问题

    转自: http://www.blogjava.net/RoyPayne/archive/2012/01/30/369017.htmlhttp://msi110.iteye.com/blog/7101 ...