uva11090 - Going in Cycle!!

该博客详细探讨了如何使用Bellman-Ford算法解决UVA11090问题,重点在于处理含有负权边的图中循环的检测。通过实例解析,阐述了算法的步骤和在图论中的应用,帮助读者理解负环检测的重要性。
摘要由CSDN通过智能技术生成

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, N. N test cases follow. Each one starts with two numbers n and m. m 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


给定一个N个点M条边的有向加权图,求图中的平均权值最小的环

如果存在一个环,w1,w2,w3....wn
二分一个平均值
若平均值小于mid,则mid>(w1+w2+w3+...+wn)/n;  
即 (w1-mid)+(w2-mid)+(w3-mid)+...+(wn-mid)<0
则转化成了求图中是否有负权回路

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>

using namespace std;


const int inf=0x3f3f3f3f;

int n,m,tot;
int now[100],cnt[100];
int pre[10010],son[10010];
double val[10010];
double d[100];
bool vis[100];

void add(int u,int v,double x)
{
    tot++;
    pre[tot]=now[u];
    now[u]=tot;
    son[tot]=v;
    val[tot]=x;
}

bool spfa(double mid)
{
    memset(vis,0,sizeof(vis));
    memset(cnt,0,sizeof(cnt));

    queue < int > Q;
    for (int i=1; i<=n; i++)
    {
        d[i]=0;
        vis[i]=1;
        Q.push(i);
    }
    while (!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        int p=now[u];
        vis[u]=0;
        while (p>0)
        {
            int v=son[p];
            if (d[u]+val[p]-mid<d[v])
            {
                d[v]=d[u]+val[p]-mid;
                if (!vis[v])
                {
                    Q.push(v);
                    vis[v]=1;
                    if (++cnt[v]>n) return true;
                }
            }
            p=pre[p];
        }
    }
    return false;
}


int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while (T--)
    {
        memset(now,0,sizeof(now));
        tot=0;
        double l=0, r=0;
        scanf("%d%d",&n,&m);
        for (int i=1; i<=m; i++)
        {
            int u,v;
            double x;
            scanf("%d%d%lf",&u,&v,&x);
            add(u,v,x);
            if (x>r)  r=x;
        }
        printf("Case #%d: ",cas++);
        if (!spfa(r+1))
        {
            printf("No cycle found.\n");
            continue;
        }
        while (r-l>1e-3)
        {
            double mid=(l+r)/2.;
           // printf("%.7lf\n",mid);
            if ( spfa(mid) )  r=mid;
            else
                l=mid;
        }
        printf("%.2lf\n",l);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值