UVA 11090 Going in Cycle!!(二分 + spfa 判负环)

题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2031

题目大意:给你一个n个点,m条边的有向图。问你平均权值最小的有向环。输出这个平均权值。

解题思路:问你这个权值,二分答案是很容易想到的,然后对每条边 - mid,再 spfa 判负环。还有就是要先判断他是不是本来就没有环吗,强连通当然可以,不过对于这道题目,这种更好:设最大的边权是max_c,然后把每条边的边权都 - (max_c+1),用 spfa 看看有没有负环。这里的权值是浮点数,所以二分要用double,之前没怎么写过double 的二分,按照写整型一样的写,WA了,改精度,改为 1e-4 ,还是WA。用书上的写法写了一遍,A了。。 

求神牛看看我这么写为什么WA?3Q~~~

我写的二分:

double l = 0,r = max_c;
double ans;
while(r-l >= EPS)
{
    double mid = (l+r)/2;
    if(check(mid,n))
    {
        ans = mid;
        r = mid-EPS;
    }
    else l = mid+EPS;
}
printf("%.2f\n",ans);


书上的二分:

double l = 0,r = max_c;
while(r-l > EPS)
{
    double mid = l+(r-l)/2;
    if(check(mid,n))
    {
        r = mid;
    }
    else l = mid;
}
printf("%.2f\n",l);


代码如下:

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

const int MAXN = 55;
const int MAXM = MAXN*MAXN;
const double EPS = 1e-3;

struct Edge
{
    int t,next;
    double val;
} edge[MAXM];

int head[MAXN],tot;

void init()
{
    tot = 0;
    memset(head,-1,sizeof(head));
}

void add_edge(int s,int t,double val)
{
    edge[tot].t = t;
    edge[tot].val = val;
    edge[tot].next = head[s];
    head[s] = tot++;
}

bool inq[MAXN];
int cnt[MAXN];
double d[MAXN];

int spfa(int n)
{
    queue<int> q;
    memset(inq,0,sizeof(inq));
    memset(cnt,0,sizeof(cnt));
    for(int i = 1;i <= n;i++)
    {
        d[i] = 0;
        inq[i] = 1;
        q.push(i);
    }
    while(!q.empty())
    {
        int cur = q.front();
        q.pop();
        inq[cur] = 0;
        for(int i = head[cur]; i != -1;i = edge[i].next)
        {
            int next_id = edge[i].t;
            double val = edge[i].val;
            double tmp = d[cur]+val;
            if(tmp < d[next_id])
            {
                d[next_id] = tmp;
                if(!inq[next_id])
                {
                    q.push(next_id);
                    inq[next_id] = 1;
                    if(++cnt[next_id] > n) return 1;
                }
            }
        }
    }
    return 0;
}

int check(double mid,int n)
{
    for(int i = 0;i < tot;i++)
        edge[i].val -= mid;
    int ok = spfa(n);
    for(int i = 0;i < tot;i++)
        edge[i].val += mid;
    return ok;
}

int main()
{
    int cas = 0;
    int _;
    scanf("%d",&_);
    while(_--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        init();
        int max_c = 0;
        while(m--)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            add_edge(a,b,c);
            max_c = max(max_c,c);
        }
        printf("Case #%d: ",++cas);
        if(!check(max_c+1,n))
        {
            puts("No cycle found.");
            continue;
        }
        double l = 0,r = max_c;
        while(r-l > EPS)
        {
            double mid = (l+r)/2;
            if(check(mid,n))
            {
                r = mid;
            }
            else l = mid;
        }
        printf("%.2f\n",l);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值