uva 11090
|
I I U P C 2 0 0 6 |
|
|
Problem G: Going in Cycle!! |
|
|
Input: standard input Output: standard output |
|
|
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 |
Output for Sample Input |
|
2 |
Case #1: No cycle found. |
|
Problemsetter: Mohammad Tavakoli Ghinani Alternate Solution: Cho |
|
二分答案,判断是否有负权回路。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; const int MAX_N = ;
const double eps = 1e-;
const int edge = ;
int first[MAX_N],Next[edge],v[edge];
double w[edge];
bool inq[MAX_N];
int cnt[MAX_N];
double d[MAX_N];
int N,M;
double sum = ; void add_edge(int id,int u) {
int e = first[u];
Next[id] = e;
first[u] = id;
} bool bellman(double x) {
queue<int> q;
memset(inq,,sizeof(inq));
memset(cnt,,sizeof(cnt));
for(int i = ; i <= N; ++i) {
d[i] = ;
inq[i] = ;
q.push(i);
} while(!q.empty()) {
int u = q.front(); q.pop();
inq[u] = ;
for(int e = first[u]; e != -; e = Next[e]) {
if(d[ v[e] ] > d[u] + w[e] - x) {
d[ v[e] ] = d[u] + w[e] - x;
if(!inq[ v[e] ]) {
q.push( v[e] );
inq[ v[e] ] = ;
if(++cnt[ v[e] ] > N) return true;
}
}
}
} return false; } void solve() {
double l = ,r = sum;
while(r - l >= eps) {
//printf("l = %f r = %f\n",l,r);
double mid = (l + r) / ;
if(bellman(mid)) r = mid;
else l = mid;
}
if(bellman(sum + )) {
printf("%.2f\n",l);
} else {
printf("No cycle found.\n");
}
} int main()
{
//freopen("sw.in","r",stdin);
int t;
scanf("%d",&t);
for(int ca = ; ca <= t; ++ca) {
scanf("%d%d",&N,&M);
for(int i = ; i <= N; ++i) first[i] = -;
sum = ;
for(int i = ; i < M; ++i) {
int u;
scanf("%d%d%lf",&u,&v[i],&w[i]);
sum += w[i];
add_edge(i,u);
} //printf("sum = %f\n",sum);
printf("Case #%d: ",ca);
solve();
}
//cout << "Hello world!" << endl;
return ;
}
uva 11090的更多相关文章
- 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(最短路BellmanFord+ 二分判负环)
layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...
- 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 在环中
题目链接:http://vjudge.net/contest/143318#problem/A 题意: 求平均权值最小的回路. 分析: 平均权值不可能超过最大边,二分查,然后,由于是平均权值,就可以转 ...
- 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条边的带权有向图,求平均权值最小的回路的平均权值? 思路: 首先,图中得有环的存在才有解,其次再解决这个最小平均权值为多少.一般这种就是二分猜平均权值了,因为环在哪也难以找出来 ...
随机推荐
- openSUSE13.1安装搜狗输入法 for Linux
一句话总结:爽死我了!什么叫输入的快感终于体会到了,搜狗输入法,码农的好伙伴!!! 转自openSUSE论坛 女王陛下 https://forum.suse.org.cn/viewtopic.php? ...
- C 简单处理excel 转成 json
引言 工作中常需要处理excel转json问题. 希望这篇博文能简单描述这个问题.并提供一种解决思路.提升感悟. 今天我们处理的事就是为了把 xlsm => json. 一种方式是. 去 goo ...
- 安装SQL Server Management Studio遇到的29506错误
首先要在IIS里把internet 信息哪项选上.然后在安装SQL Server, 在安装的时候一直报 29506错误,装了几次,不知道什么原因.谷歌了一下说是权限的问题. 很纳闷,我当然用的是管理员 ...
- 如何在Eclipse中配置Tomcat
1.Eclipse EE 配置Tomcat Eclipse EE 主要用于Java Web开发和J2EE项目开发.Eclipse EE中配置Tomcat比较简单,新建一个Tomcat Server即可 ...
- 删除字符串第一个byte
删除字符串第一个byte 一种方式: char * mag; char buff[1000]; char number; memcpy((char *)msg,buff,len); strnc ...
- oracle12c不能进入到http://localhost:5500/em的解决办法
Oracle11g企业管理器无法打开——解决https://localhost:1158/em 页面无法打开的问题 常见的问题:https://localhost:1158/em 无法打开 解决办法: ...
- 对 Linux 新手非常有用的20个命令
你打算从Windows换到Linux上来,还是你刚好换到Linux上来?哎哟!!!我说什么呢,是什么原因你就出现我的世界里了.从我以往的经验来说,当我刚使用Linux,命令,终端啊什么的,吓了我一跳. ...
- git用法
chapter: 8 add 添加文件内容至索引 用法:git add [选项] [--] ... -n, --dry-run 演习 -v, --verbose 冗长输出 -i, --interact ...
- UIDatePicker倒计时 swift
// // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...
- JAVA素数分解
package test; import java.util.*; public class test1 { public static void main(String[] args){ long ...