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. Ambari 常用的 REST API 介绍

    源码文档路径:ambari\ambari-server\docs\api\v1 swagger风格api文档:https://www.cnblogs.com/felixzh/p/10694724.ht ...

  2. 使用 whistle 替代本地 nginx/webpack 服务

    加入鹅厂之后,我发现团队都在用一款叫做 Whistle 的工具,起初我以为这只是一款类似 Fiddler/Charles 的普通货色.然鹅,发现下面这两种用法之后,我把自己的膝盖摘下来献给了制作这款工 ...

  3. javascript中apply、call和bind的区别及方法详解

    文章目录   apply.call apply.call 区别 apply.call实例 数组之间追加 获取数组中的最大值和最小值 验证是否是数组(前提是toString()方法没有被重写过) 类(伪 ...

  4. 使用 Moq 测试.NET Core 应用 -- Mock 行为

    第一篇文章, 关于Mock的概念介绍: https://www.cnblogs.com/cgzl/p/9294431.html 第二篇文章, 关于方法Mock的介绍: https://www.cnbl ...

  5. Docker & ASP.NET Core (1):把代码连接到容器

    和这种蛋糕一样,Docker的容器和镜像也是使用类似的分层文件系统构建而成的. 这样做的好处就是可以节省硬盘空间,也利于复用等等.因为Docker基于镜像创建容器的时候,其镜像是共享的:而且镜像里面的 ...

  6. Harbor配置https认证

    Harbor配置https认证由于Harbor不附带任何证书,它默认使用HTTP来提供注册表请求.但是,强烈建议为任何生产环境启用安全性.因为测试使用,使用自签名证书: 1.创建CA证书 首先创建个目 ...

  7. PreferencesUtils【SharedPreferences操作工具类】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 可以替代ACache用来保存用户名.密码. 相较于Acache,不存在使用猎豹清理大师进行垃圾清理的时候把缓存的数据清理掉的问题. ...

  8. springboot~Compiler时开启插件的注解功能

    对于IJ这个IDE工具来说,我们会安装一些插件来帮助我们更好的进行开发,像lombok就是一款不错的插件,使用注解的方式在项目编译时帮助我们生成代码,像getter,setter,tostring等等 ...

  9. Python编程从入门到实践笔记——操作列表

    Python编程从入门到实践笔记——操作列表 #coding=utf-8 magicians = ['alice','david','carolina'] #遍历整个列表 for magician i ...

  10. 基于IdentityServer的系统对接微信公众号

    业务需求 公司有两个业务系统,A和B,AB用户之间属于多对一的关系,数据库里面也就是两张表,A表有个外键指向B.现在需要实现以下几个功能. A用户扫描B的二维码,填写相关的注册信息,注册完成之后自动属 ...