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> # ...
随机推荐
- Java 简单算法--打印乘法口诀(只使用一次循环)
package cn.magicdu.algorithm; /** * 九九乘法口诀表 * * @author xiaoduc * */ public class NineNineMulitTable ...
- 自定义控件ViewPagae<
学习知识点 onTouch事件传递机制. Lisenter监听 ImageView的src background scaleType不同属性的显示情况. onTouch滑动抬起调用的MotionEve ...
- dagger和butterknife使用冲突
两者会冲突的主要原因是因为两者都有:javax.annotation.processing.Processor 于是在build.gradle中添加如下配置即可: // 注释冲突 packagingO ...
- MFC多视图共用一文档实现
项目过程中有定制视图的需求,具体为打开程序默认双视图并且共用一个文档.在网上找了许多类似的资料,都不尽完善.本文直接从达到目的的角度叙述,实现原理不再多说. 1.在自己的App类中定义public变量 ...
- MongoDB下载安装
MongoDB官方下载地址:http://www.mongodb.org/ 一.在Windows平台下的安装 1.下载MongoDB数据库 2.设置MongoDB程序存放目录 下载完数据库后,直接解压 ...
- 隐藏NavigationBar 带来的坑
一.场景介绍 现在大多数APP 都有一个需求,就是隐藏某一个页面的NavigationBar.很多开发者直接 [self.navigationController setNavigationBar ...
- 2) LINQ编程技术内幕--yield return
yield return 使用.NET的状态机生成器 yield return关键词组自动实现IDisposable,使用这个可枚举的地方, 还存在一个隐含的try finally块. 示例代码: c ...
- [.Net MVC] 使用SQL Server数据库代替LocalDb
之前开发的时候一直用的VS2013,所以数据库也用的LocalDb,这给开发带来很大便利.不过由于开发后还要进行部署,就改用了SQL Server 2012,这里总结下过程. 基本环境:VS2013, ...
- 标准C++中string类的用法
转自博客园:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非 ...
- OpenJudg / Poj 1363 Rails
1.链接: http://poj.org/problem?id=1363 http://bailian.openjudge.cn/practice/1363 2.题目: Rails Time Limi ...