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, 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

题意:

在一个有向图中找一个平均距离最小的环。

思路:

二分枚举平均最小距离,每次每条边减去这个距离,然后spfa(或者bellmanFord找负环)假设找到,说明平均最小距离比这个值要小。假设没找到。则说明平均最小距离比这个值大。

注意可能是不连通图~

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
#define eps 1e-6
#define maxn 55
#define MAXN 10005
using namespace std; int n,m,ans,cnt,sx;
double le,ri,mid,res;
bool vis[maxn];
double dist[maxn];
int head[maxn],num[maxn];
struct Node
{
int v,next;
double w;
}edge[MAXN]; void addedge(int u,int v,double w)
{
cnt++;
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt;
}
bool SPFA(int k)
{
int i,j,nx,v;
memset(vis,0,sizeof(vis));
memset(num,0,sizeof(num));
for(i=1;i<=n;i++) dist[i]=INF;
sx=k;
queue<int>q;
dist[sx]=0;
vis[sx]=num[sx]=1;
q.push(sx);
while(!q.empty())
{
nx=q.front();
vis[nx]=0;
q.pop();
for(i=head[nx];i;i=edge[i].next)
{
v=edge[i].v;
if(dist[v]>dist[nx]+edge[i].w-mid)
{
dist[v]=dist[nx]+edge[i].w-mid;
if(!vis[v])
{
num[v]++;
if(num[v]>n) return true ;
vis[v]=1;
q.push(v);
}
}
}
}
return false ;
}
int main()
{
int i,j,u,v,t,test=0;
double w;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
cnt=0;
memset(head,0,sizeof(head));
for(i=1;i<=m;i++)
{
scanf("%d%d%lf",&u,&v,&w);
addedge(u,v,w);
}
le=0; ri=10000005;
int flag;
while(ri-le>eps)
{
mid=(le+ri)/2.0;
flag=0;
for(i=1;i<=n;i++)
{
if(SPFA(i))
{
flag=1; break ;
}
}
if(flag) ri=mid;
else le=mid;
}
res=le;
if(res<=10000001) printf("Case #%d: %.2f\n",++test,res);
else printf("Case #%d: No cycle found.\n",++test);
}
return 0;
}

UVA11090 Going in Cycle!! (二分+SPFA推断有无负权)的更多相关文章

  1. poj 3259 bellman最短路推断有无负权回路

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36717   Accepted: 13438 Descr ...

  2. Wormholes 最短路判断有无负权值

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  3. SPFA穿越虫洞——负权回路得判断

    poj3259 题目大意:穿越虫洞可以回到过去(时间--)所以能不能让时间倒流呢,就是判断有没有负权回路这次尝试用SPFA算法,也可以复习一下链式前向星 准备工作,队列q,spfa算法得有点就在于这个 ...

  4. Spfa 求含负权边的最短路 + 判断是否存在负权回路

    在Bellman-Ford算法之后,我们总算迎来了spfa算法,其实就如同堆优化Dijkstra算法之于朴素版Dijkstra算法,spfa算法仅仅是对Bellman-Ford算法的一种优化,但是在形 ...

  5. UVA11090 Going in Cycle!! 【SPFA】

    题意:求一个无向图的边权平均值最小的环 思路:假设环中Σwi/t<ans 那变形一下就是Σwi<ans*t → Σ(wi-ans)< 0 这样就可以二分答案做了 #include & ...

  6. SPFA 求带负权的单源最短路

    int spfa_bfs(int s) { ///s表示起点. queue <int> q; memset(d,0x3f,sizeof(d)); ///d数组中存下的就是最短路径(存在的话 ...

  7. UVA11090 Going in Cycle!!(二分判负环)

    UVA11090 Going in Cycle!! 二分答案,用spfa判负环. 注意格式:图不一定连通. 复杂度$O(nmlog(maxw-minw))$ #include<iostream& ...

  8. POJ-3259 Wormholes---SPFA判断有无负环

    题目链接: https://vjudge.net/problem/POJ-3259 题目大意: 农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的 ...

  9. SPFA 最短路 带负权边的---- 粗了解

    SPFA(Shortest Path Faster Algorithm)是Bellman-Ford算法的一种队列实现,减少了不必要的冗余计算. 算法大致流程是用一个队列来进行维护. 初始时将源加入队列 ...

随机推荐

  1. ArrayList集合-[习题]--C#

    :向集合中添加10个元素,计算平均值,求最大.最小值. ; list.AddRange(, , , , , , , , }); int Max, Min; Max = Min = (]; ; i &l ...

  2. 【CSDN博客之星评选】我为什么坚持写博客

    今天无意中在CSDN的博客之星评选活动看到自己竟然是候选人之一,真的是十分的惊讶也十分的高兴.对于喜欢写东西.喜欢分享的我来说,已经忍不住用文字来记录一下今天的美好心情,同时也让我回想起我是如何开始在 ...

  3. 高级UIKit-06(UIImagePickerController)

    [day07-1-getSystemImage]:获取系统相册 UIImagePickerController图片采集控制器 picker采集者,采摘者 该方法继承自:UINavigationCont ...

  4. IntelliJ IDEA 开发swing(二)

    原文:idea开发swing(二) 闲话少说,书接idea开发swing(一). 程序编译完成后,需要打包发布,如果有fat_jar的同学可以通过该插件打包,这里是使用ant来打包,步骤如下: 一.编 ...

  5. Android studio gradle配置

    什么是Gradle? Gradle是一种依赖管理工具,基于Groovy语言,面向Java应用为主,它抛弃了基于XML的各种繁琐配置,取而代之的是一种基于Groovy的内部领域特定(DSL)语言. gr ...

  6. 用PowerMockito来mock私有方法(转)

    话说我们做的所谓的接口测试真的是不伦不类啊,测的是controller层,那叫接口木??!!可是老大们说写的是接口测试,那就接口吧! 自接手写这个接口测试不久,很多人,包括一个关系比较好的架构师就跟我 ...

  7. μC/OS学习资料(附Ebook)

    注意:下载地址位于文末. μC/OS-各版本源码 <嵌入式实时操作系统μC/OS-II> <嵌入式实时操作系统μC/OS-III> <μC/OSII2.52源码中文译注- ...

  8. 怎样实现cocos2d-x之文字渲染

    // 1.创建一段文本 // create函数的三个参数分别为:文本内容.字体和字体大小 CCLabelTTF *font=CCLabelTTF::create("Hello World&q ...

  9. asp.net错误.在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错

    解决办法:将该项目所在目录设置为虚拟目录,右键-转为应用程序.

  10. Unity Interface Serialization-Expose Interface field In Inspector

    Unity has some quirks about their inspector, so as a preface they are listed here: If you add a [Ser ...