Problem  UVA - 11090 - Going in Cycle!!

Time Limit: 3000 mSec

Problem Description

You are given a weighted directed graph with n vertices and m edges. Each cycle in the graph has a weight, which equals to sum of its edges. There are so many cycles in the graph with different weights. In this problem we want to find a cycle with the minimum mean.

Input

The first line of input gives the number of cases, N. N test cases follow. Each one starts with two numbers n and m. m lines follow, each has three positive number a,b,c which means there is an edge from vertex a to b with weight of c.

Output

For each test case output one line containing Case #x: followed by a number that is the lowest mean cycle in graph with 2 digits after decimal place, if there is a cycle. Otherwise print No cycle found..
Constraints

• n ≤ 50

• a,b ≤ n

• c ≤ 10000000

Sample Input

2 2 1 1 2 1 2 2 1 2 2 2 1 3

Sample Output

Case #1: No cycle found.

Case #2: 2.50

题解:差分约束系统板子题,配上二分很容易解决,这里的spfa和一般的spfa稍有区别,原因我在UVA 11478的博客里解释过了,不再赘述,这种写法会比分别以每个点为源点跑高效不少,目前这份代码耗时50ms,但是分别以每个点为源点跑了200ms。

 #include <bits/stdc++.h>

 using namespace std;

 #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0); struct Edge
{
int to, next;
double w;
} edge[maxm << ]; int n, m;
int tot, head[maxn]; void init()
{
tot = ;
memset(head, -, sizeof(head));
} void AddEdge(int u, int v, double w)
{
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].w = w;
head[u] = tot++;
} double dist[maxn];
bool vis[maxn];
int cnt[maxn]; bool spfa()
{
queue<int> que;
for (int i = ; i < n; i++)
{
dist[i] = ;
cnt[i] = ;
vis[i] = true;
que.push(i);
} while (!que.empty())
{
int x = que.front();
que.pop();
vis[x] = false;
for (int i = head[x]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if (dist[v] > dist[x] + edge[i].w)
{
dist[v] = dist[x] + edge[i].w;
if (!vis[v])
{
que.push(v);
vis[v] = true;
if (++cnt[v] > n)
{
return true;
}
}
}
}
}
return false;
} bool Judge(double x)
{
for (int i = ; i < tot; i++)
{
edge[i].w -= x;
}
bool ok = true;
if(spfa())
ok = false;
for (int i = ; i < tot; i++)
{
edge[i].w += x;
}
return ok;
} int iCase; int main()
{
ios::sync_with_stdio(false);
cin.tie();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int T;
cin >> T;
while (T--)
{
cin >> n >> m;
init();
int u, v;
double w;
double lim = -inf;
for (int i = ; i < m; i++)
{
cin >> u >> v >> w;
u--, v--;
AddEdge(u, v, w);
lim = max(lim, w);
}
cout << "Case #" << ++iCase << ": ";
if (Judge(lim + ))
{
cout << "No cycle found." << endl;
}
else
{
double le = , ri = lim;
while (ri - le > 1e-)
{
double mid = (le + ri) / ;
if (Judge(mid))
{
le = mid;
}
else
{
ri = mid;
}
}
cout << fixed << setprecision() << le << endl;
}
}
return ;
}

UVA - 11090 - Going in Cycle!!(二分+差分约束系统)的更多相关文章

  1. UVA - 11478 - Halum(二分+差分约束系统)

    Problem  UVA - 11478 - Halum Time Limit: 3000 mSec Problem Description You are given a directed grap ...

  2. UVA 11090 - Going in Cycle!!(Bellman-Ford)

    UVA 11090 - Going in Cycle!! option=com_onlinejudge&Itemid=8&page=show_problem&category= ...

  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!!(二分答案+判负环)

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

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

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

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

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

  7. UVA 11090 Going in Cycle!!

    要求给定的图的中平均权值最小的环,注意处理自环的情况就能过了. 按照w1+w2+w3+….wn < n*ave的不等式,也就是(w1-ave) + (w2-ave) +…..(wn-ave) & ...

  8. UVA 11090 - Going in Cycle!! SPFA

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

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

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

随机推荐

  1. Windows Server 2016-图形化之客户端加域(一)

    要使用域环境,您必须将计算机加入域.只有加入域的计算机才能使用域资源.在连接期间,将在域中创建一个计算机帐户,以便将计算机作为成员进行身份验证.前边两节我们写到Windows Server 2016- ...

  2. Iterm2/Mac自带终端工具快速进入你想进入的虚拟机教程

    一.首先我们在终端本地要写一个登录的脚本,eg: 当然首先要touch login.sh 啦,下面就是脚本文件,比较low,大神勿喷,会更炫酷写法的小伙伴可以自己参考这个思路写,不会的直接复制就好啦 ...

  3. Followme Devops实践之路

    引言 天下武功,唯快不破 想要提高开发团队效率,势必要有一套完整而成熟的开发流程方案,除了sprint迭代开发模式之外,还有近几年流行的devops流程,都是可以大幅度提高开发效率的工具. 我们团队也 ...

  4. Netty1:初识Netty

    为什么使用Netty Netty是业界最流行的NIO框架之一,它的健壮性.功能.性能.可定制性.可扩展性在同类框架中都是首屈一指的,它已经得到了成百上千的商用项目的证明.对于为什么使用Netty这个话 ...

  5. [Inside HotSpot] C1编译器工作流程及中间表示

    1. C1编译器线程 C1编译器(aka Client Compiler)的代码位于hotspot\share\c1.C1编译线程(C1 CompilerThread)会阻塞在任务队列,当发现队列有编 ...

  6. 从零开始学习PYTHON3讲义(十六)(连载完)学习资源包下载

    <从零开始PYTHON3>学习资源包下载 课程连载已经完全结束. 经过整理校对,这里把在课程中出现过的源码和练习答案示例源码全部打包提供下载: https://pan.baidu.com/ ...

  7. 深入浅出NIO Socket实现机制

    前言 Java NIO 由以下几个核心部分组成: Buffer Channel Selector 以前基于net包进行socket编程时,accept方法会一直阻塞,直到有客户端请求的到来,并返回so ...

  8. 1.4部署到IIS「深入浅出ASP.NET Core系列」

    希望给你3-5分钟的碎片化学习,可能是坐地铁.等公交,积少成多,水滴石穿,谢谢关注. 很多人第一次在IIS中部署Asp.Net Core App的人都会遇到问题,会发现原来的部署方式无法运行Asp.N ...

  9. 痞子衡嵌入式:飞思卡尔i.MX RT系列MCU开发那些事 - 索引

    大家好,我是痞子衡,是正经搞技术的痞子.本系列痞子衡给大家介绍的是飞思卡尔i.MX RT系列微控制器相关知识. 飞思卡尔半导体(现恩智浦半导体)于2017年开始推出的i.MX RT系列开启了高性能MC ...

  10. sublime text3插件解决输入法不跟随的问题

    快捷键ctrl + shift +p 输入  install package 回车,调出插件搜索器, 在搜索栏中输入 IMESupport 回车安装插件. 即可解决问题.