poj.org部分题目(五)

文章目录

1860

#include<cstdio>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cmath>
#define mem(x,y) memset(x,y,sizeof(x))
#define max(a,b) a>b?a:b
using namespace std;
const int N=110;
const int M=220;
const double Max=0x3f3f3f3f;
 
struct node
{
   
    int beg;
    int end;
    double r,c;
};
node pp[M];
double dis[N];
int n,m,e,fn;
double fu;
 
void addedg(int beg,int end,double r,double c)
{
   
    pp[e].beg=beg;
    pp[e].end=end;
    pp[e].r=r;
    pp[e].c=c;
    ++e;
}
 
bool relax(int p)
{
   
    double t=(dis[pp[p].beg]-pp[p].c)*pp[p].r;
    if(t>dis[pp[p].end])
    {
   
        dis[pp[p].end]=t;
        return true;
    }
    return false;
}
 
bool bellman_ford()
{
   
    bool flag;
    for(int i=0;i<n;i++)
        dis[i]=0.0;
    dis[fn]=fu;
    for(int i=0;i<n-1;++i)
    {
   
        flag=false;
        for(int j=0;j<e;++j)
        {
   
            if(relax(j))
                flag=true;
        }
        if(dis[fn]>fu)
            return true;
        if(!flag)
            return false;
    }
    for(int i=0;i<e;i++)
    {
   
        if(relax(i))
            return true;
    }
    return false;
}
 
int main()
{
   
    int beg,end;
    double r1,r2,c1,c2;
    scanf("%d%d%d%lf",&n,&m,&fn,&fu);
    --fn;
    e=0;
    for(int i=0;i<m;i++)
    {
   
        scanf("%d%d%lf%lf%lf%lf",&beg,&end,&r1,&c1,&r2,&c2);
        --beg;
        --end;
        addedg(beg,end,r1,c1);
        addedg(end,beg,r2,c2);
    }
    bool ans=bellman_ford();
    if(ans)
        printf("YES\n");
    else
        printf("NO\n");
        system("pause");
    return 0;
}

在这里插入图片描述

相关推荐

  1. 届信大超越杯团体赛部分题解

    2024-01-08 14:14:02       14 阅读
  2. C语言经典面试题目(十

    2024-01-08 14:14:02       17 阅读
  3. C++经典面试题目(十

    2024-01-08 14:14:02       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-08 14:14:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-08 14:14:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-08 14:14:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-08 14:14:02       18 阅读

热门阅读

  1. 05.构造程序逻辑

    2024-01-08 14:14:02       39 阅读
  2. Spring整合drools

    2024-01-08 14:14:02       33 阅读
  3. 详解Nacos和Eureka的区别

    2024-01-08 14:14:02       37 阅读
  4. Nacos vs Eureka的区别:微服务注册中心的选择

    2024-01-08 14:14:02       36 阅读
  5. [设计模式 Go实现] 结构型~外观模式

    2024-01-08 14:14:02       30 阅读
  6. 2024年超详细的Python3学习路径规划

    2024-01-08 14:14:02       35 阅读