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. easyui只选择年份月份的插件---SimpleCanleder

    <td>日期</td> <td> <input type="text" id="search_begindate" s ...

  2. 10 - 应用程序间通信、本地通知、加速计、URL传输中文

    一.应用间通信 URL 调用系统服务: tel:11111 sms:xxx@163.com http:// URL深入 类型://主机:端口/地址?参数 label框等于文字大小快捷键:command ...

  3. 07-IOSCore - CoreData补充、音频视频

    xml被plist取代了  数据库被coredata取代了 一.Core Data 高级补充 1. Core Data 本质是什么?操作数据库的数据 ORM Object Relationship M ...

  4. python使用libssh2连接linux

    1.安装(1)使用下面命令获得最新版本的ssh4py安装包    git clone git://github.com/wallunit/ssh4py (2)解压ssh4py后使用下面命令进行安装: ...

  5. Android圆环形颜色选择器:HoloColorPicker

    HoloColorPicker实现圆环形颜色选择器,可以改变颜色饱和度来选择颜色.选择颜色时,可以用手指沿着圆环滑动一个滑块,从而选择颜色. 添加以下XML至你的布局中: ? 1 2 3 4 < ...

  6. Google C++ style guide——命名约定

    1.通过命名规则 函数命名.变量命名.文件命名应具有描写叙述性. 类型和变量应该是名词,函数名能够用"命令性"动词. 2.文件命名 文件名称所有小写,能够包括下划线(_)或者断线( ...

  7. Windows Services的1053错误的解决办法之一:修改注册表允许的响应时间

    Error: 'The service did not respond in a timely fashion' (ServicesPipeTimeout) when attempting when ...

  8. android studio 怎么运行java

    方法/步骤 1.新建一个project,或者如果已经有project的话,那就直接新建一个module.注意选择Java library,然后下一步 2.输入module的一些信息.点击finish ...

  9. Oracle数据库top10物理段

    select owner, name, type, mega, tbs   from (select owner,                case                  when ...

  10. ASP.NET中IsPostBack详解(转载)

    1.IsPostBack介绍Page.IsPostBack是一个标志:当前请求是否第一次打开. 调用方法为:Page.IsPostBack或者IsPostBack或者this.IsPostBack或者 ...