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, NN test cases follow. Each one starts with two numbers n and mm 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
2 1
1 2 1
2 2
1 2 2
2 1 3

Case #1: No cycle found.
Case #2: 2.50

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的更多相关文章

  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. UVA 11090 Going in Cycle!! SPFA判断负环+二分

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

  5. UVA 11090 - Going in Cycle!! SPFA

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

  6. Uva 11090 在环中

    题目链接:http://vjudge.net/contest/143318#problem/A 题意: 求平均权值最小的回路. 分析: 平均权值不可能超过最大边,二分查,然后,由于是平均权值,就可以转 ...

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

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

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

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

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

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

随机推荐

  1. 《Prism 5.0源码走读》UnityBootstrapper

    UnityBootstrapper (abstract class)继承自Bootstrapper(abstract)类, 在Prism.UnityExtensions.Desktop project ...

  2. 线程操作API

    线程操作API 1.currentThread 2.getId() .getName().getPriority().getStart.isAlive().isDaemon().isInterrupt ...

  3. afddaf

    //import javax.swing.*; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JL ...

  4. Microsoft SqlServer2008技术内幕:T-Sql语言基础-读书笔记1

    一.理论背景:关系模型,其数学理论是集合论和谓词逻辑. 1.集合论:集合定义是把我们直观或思维中确定的,相互间有明确区别的那些对象视为一个整体,这个整体就是集合. 2.谓词逻辑:谓词是判断对象是否有某 ...

  5. Java入门到精通——基础篇之static关键字

    一.概述        static 关键字是声明静态变量,静态方法用的.static的含义是属于类且不属于类对象的变量和函数. 二.static的产生.         在创建对象的时候除非用new ...

  6. Intent Android 详解

    Intents and Intent Filters 三种应用程序基本组件 activity, service和broadcast receiver——是使用称为intent的消息来激活的. Inte ...

  7. Go中的指针与函数接收器

    Go中使用*号表示指针,但是没有指针算数,不能对其进行加减.同时内存管理都由Go来负责,不需要拖动释放内存. Go中的函数接收者,可以为值类型,也可以是引用类型. 看代码: package main ...

  8. 1 通过JNI混合使用Java和C++ -----> 操作字符串

    JNI(Java Native Interface)是Java语言的一部分,可以访问非Java语言编写的程序,也可以用于在C++程序中执行Java代码. 步骤: 1>  编写带有native声明 ...

  9. 读TCP-IP详解卷1:协议(1)

    1.TCP传给IP的数据单元称作TCP报文段或简称为TCP段(TCP segment)IP传给网络接口层的数据单元称作IP数据报(IPdatagram).通过以太网传输的比特流称作帧(Frame).

  10. C++ 学习笔记(一)

    只是记录自己学习C++ 笔记实例来自<c++ primer> 1.static: static 局部对象确保不迟于在程序执行流程第一次经过该对象的定义语句时进行初始化.这种对象一旦被创建, ...