poj 1860 Bellman-Ford

来源:互联网 发布:国家授时中心网络时间 编辑:程序博客网 时间:2024/05/20 09:08
点击打开链接//bellman-ford #include<iostream>#include<cstdio>#include<cstring>using namespace std;struct node{    int x,y;    double a,b;}edge[10005];                   //边集int n,m,s;                  //货币种类 兑换点数 拥有货币种类 double k;                   //拥有货币数int t = 0;double num[105];void add(int x,int y,double a,double b){    edge[t].x = x;    edge[t].y = y;    edge[t].a = a;    edge[t++].b = b;}int BF(){    for(int i=1;i<=n;i++)    {        num[i] = 0;    }    num[s] = k;                 //钱数数组    //进行松弛操作    for(int i=1; i<=n; i++)    {        for(int j=0; j<t; j++)        {            if(num[edge[j].y]<(num[edge[j].x] - edge[j].b) * edge[j].a)            {                num[edge[j].y] = (num[edge[j].x] - edge[j].b) * edge[j].a;                if(num[s]>k)                {                    return 1;                }            }        }    }    //进行完成所有松弛操作后,再进行一次松弛操作,如果发生变化,则判断有环存在   for(int j=0; j<t; j++)    {        if(num[edge[j].y]<(num[edge[j].x] - edge[j].b) * edge[j].a)        {            num[edge[j].y] = (num[edge[j].x] - edge[j].b) * edge[j].a;            return 1;        }    }    return 0;}int main(){    while(scanf("%d%d%d%lf",&n,&m,&s,&k)!=EOF)    {        double a1,b1,a2,b2;        int x1,y1;        t = 0;        for(int i=1; i<=m; i++)        {            scanf("%d%d%lf%lf%lf%lf",&x1,&y1,&a1,&b1,&a2,&b2);            add(x1,y1,a1,b1);            add(y1,x1,a2,b2);        }        int kk = BF();        if(kk == 1)        {            printf("YES\n");        }        else        {            printf("NO\n");        }    }    return 0;}

原创粉丝点击