Going in Cycle!!+二分答案+spfa判负环+裸题+uva

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

 解决方案:我去,w了八天,原来图可能是不联通的。

code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#define MMAX 55
using namespace std;
long long head[MMAX],inqueue[MMAX];
double d[MMAX];
bool invis[MMAX];
int n,m;
typedef struct edge
{

    long long from,to;
    double v;
    int next;
} edge;
edge E[2*MMAX*MMAX];
const long long inf=1000000000;
bool spfa(double mid)
{
    memset(invis,false,sizeof(invis));
    memset(inqueue,0,sizeof(inqueue));
    queue<int >Q;
    for(int i=1; i<=n; i++) invis[i]=true,d[i]=inf,Q.push(i);
    d[1]=0.0;
    while(!Q.empty())
    {
        int temp=Q.front();
        Q.pop();
        invis[temp]=false;
        inqueue[temp]++;
        if(inqueue[temp]>n) return true;
        for(int v=head[temp]; v!=-1; v=E[v].next)
        {
            if(d[E[v].to]>d[temp]+(-mid+E[v].v))
            {
                d[E[v].to]=d[temp]+(-mid+E[v].v);
                if(!invis[E[v].to])
                {
                    invis[E[v].to]=true;
                    Q.push(E[v].to);
                }
            }
        }
    }

    return false;

}
int main()
{
    int N,s=0;
    scanf("%d",&N);
    while(N--)
    {
        scanf("%d%d",&n,&m);
        memset(head,-1,sizeof(head));
        int k=0;
        int M=0;
        int L=inf;
        for(int i=0; i<m; i++)
        {
            int a,b;
            int c;
            scanf("%d%d%d",&a,&b,&c);
            E[k].from=a;
            E[k].to=b;
            E[k].v=c;
            E[k].next=head[a];
            head[a]=k++;
            M=max(M,c);
            L=min(L,c);
        }

        double low=L,high=M,mid;
        if(!spfa(low+high))
        {
            printf("Case #%d: No cycle found.\n",++s);
            continue;
        }
        while(abs(high-low)>0.0001)
        {
            mid=(low+high)/2.0;
            if(spfa(mid))
            {
                high=mid;
            }
            else low=mid;
        }
        printf("Case #%d: %.2lf\n",++s,low);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值