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. Home键的获取监听,安卓4.0后就不能在onkeydown方法中获取了。怎么办。

    Android下得到Home键按下的消息   在Android下,并不能通过onKeyDown这样的事件来截获Home键的消息,其原因在Android的文档中已经明确的说过了 public stati ...

  2. bzoj 1020-1029

    1020 SHOI2008 安全的航线flight 这题的代码写了很久,主要是因为几何题的东西都忘得差不多了.除去写代码的2个小时,今晚又调了一晚上,终于AC了. 这题的做法还是很有参考价值的. 最简 ...

  3. OSX: 真的吗?Mac OS X重大漏洞 改时钟获系统最高权限

    9月3日才注意到这个在8月28日刊登在英文网站9月1日在驱动之家的,关于OS X系统的sudo漏洞没有修补的新闻,今天才有时间成文上传. 这个sudo漏洞是在2013年2月27日被公布出来的,它的注册 ...

  4. Android Socket 开发技术

    根据之前的经验,应用软件的网络通信无非就是Socket和HTTP,其中Socket又可以用TCP和UDP,HTTP的话就衍生出很多方式,基础的HTTP GET和POST请求,然后就是WebServic ...

  5. linux下shutdown无法关闭tomcat进程的解决方式

    1.问题 笔者在linux下发现使用tomcat6.0.41自带的./shutdown.sh常常无法停止进程,导致各种问题的发生,令笔者相当反感! 2.解决方式一: 查找到全部的tomcat进程 $ ...

  6. SQL 教程

    SQL 教程 http://www.w3school.com.cn/sql/

  7. dump json 显示中文问题

    [root@dr-mysql01 ~]# cat a2.pl my $url="http://192.168.32.101:3000/api/getcode?env=zj&phone ...

  8. SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3457&rd=5869 解答分析:http://comm ...

  9. 改动导航栏上返回button上的字,比如把back改动为返回

    改动导航栏上返回button上的字,比如把back改动为返回 注意:这个须要在跳转之前到视图控制器中写,而不是在跳转之后到控制器中写 UIBarButtonItem *backIetm = [[UIB ...

  10. sql: sybase 和 oracle 比较

    1. sybase 和 oracle 比较 http://blog.itpub.net/14067/viewspace-1030014/ Oracle采用多线索多进程体系结构 Sybase采用单进程多 ...