UVA11090 Going in Cycle!! SPFA

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

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


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<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>

using namespace std;

#define eps 1e-8
#define INF 0x3f3f3f3f
#define MAXN 100

struct node
{
	int to,next;
	double dis;
}edge[MAXN*MAXN];

bool in[MAXN];
int head[MAXN],en;
int n,m,cnt[MAXN];
double dis[MAXN];

void add(int u,int v,double dis)
{
    edge[en].to=v;
    edge[en].dis=dis;
    edge[en].next=head[u];
    head[u]=en++;
}

bool spfa()
{
	queue<int> q;
	for(int i=1;i<=n;i++)
	{
		dis[i]=0;
		cnt[i]=0;
		in[i]=true;
		q.push(i);
	}
	while(!q.empty())
	{
		int u=q.front();
		in[u]=false;
		q.pop();
		for(int i=head[u];i!=-1;i=edge[i].next)
		{
			int v=edge[i].to;
			if(dis[u]+edge[i].dis<dis[v])
			{
				dis[v]=dis[u]+edge[i].dis;
				if(!in[v])
				{
					q.push(v);
					in[v]=true;
					if(++cnt[v]>=n)
                        return false;
				}
			}
		}
	}
	return true;
}

bool jud(double x)
{
    bool fg=0;
    for(int i=1;i<=n;i++)
        for(int j=head[i];j!=-1;j=edge[j].next)
            edge[j].dis-=x;
    if(!spfa()) fg=1;
    for(int i=1;i<=n;i++)
        for(int j=head[i];j!=-1;j=edge[j].next)
            edge[j].dis+=x;
    return fg;
}

int main()
{
    int cs;
    scanf("%d",&cs);
    for(int t=1;t<=cs;t++)
    {
       int u,v;
       double x;
       double l=INF,r=0,mid;
       scanf("%d%d",&n,&m);
       memset(head,-1,sizeof(head));en=0;
       for(int i=0;i<m;i++)
       {
           scanf("%d%d%lf",&u,&v,&x);
           add(u,v,x);
           l=min(l,x);
           r=max(r,x);
       }
       printf("Case #%d: ",t);
       if(!jud(r+1))
       {
            printf("No cycle found.\n");
       }
       else
       {
            while(r-l>eps)
            {
                mid=l+(r-l)/2;
                if(jud(mid))
                    r=mid;
                else
                    l=mid;
            }
            printf("%.2lf\n",r);
       }


    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值