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. STSR round#1

    乱搞玩出新高度.....#1

  2. Hibernate 一对一关联映射

    package com.entity; import javax.persistence.Entity; import javax.persistence.OneToOne; @Entity publ ...

  3. [转]Centos6.5使用yum安装mysql—配置MySQL允许远程登录

    一.mysql安装 第1步.yum安装mysql[root@stonex ~]#  yum -y install mysql-server安装结果:Installed:    mysql-server ...

  4. VC2008如何生成及使用DLL(图文并茂,完整版)

    博客分类: Dot net VC2008 DLL Dot net   生成.使用DLL看起来简单,但做起来才发现还是有一些地方需要注意的. 1. 打开VS2008,新建一个VC工程,选择Win32类型 ...

  5. Hibernate 3中如何获得库表所有字段的名称

    15问:Hibernate 3中如何获得库表所有字段的名称 答:可以使用以下的程序获得. Configuration conf = new Configuration(); conf.configur ...

  6. java操作Excel处理数字类型的精度损失问题验证

    java操作Excel处理数字类型的精度损失问题验证: 场景: CELL_TYPE_NUMERIC-->CELL_TYPE_STRING--->CELL_TYPE_NUMERIC POI版 ...

  7. ExcelParser ,Excel解析的工具类(正对解析xlsx)

    package cn.com.css.common.util; import java.io.File; import java.io.FileInputStream; import java.io. ...

  8. Android事件模型之interceptTouchEvnet ,onTouchEvent关系正解

    首先,看Android的官方文档正解 onInterceptTouchEvent()与onTouchEvent()的机制: 1. down事件首先会传递到onInterceptTouchEvent() ...

  9. MSSQL - Sqlcommand

    Command对象:·Command对象也称为数据库连接对象,Command对象主要执行包括添加.删除.修改.查询数据的操作命令.也可以用来执行存储过程. 属性:CommandType属性. 执行存储 ...

  10. ASIHTTPRequest-插件的使用

    链接地址:http://blog.sina.com.cn/s/blog_7b9d64af0101e5uf.html 一.什么是ASIHTTPRequest   ASIHTTPRequest 插件是一个 ...