poj1860

来源:互联网 发布:js中的top属性 编辑:程序博客网 时间:2024/05/16 16:57

这题算bellman算法的模板了,基本上bellman的思路在我印象中就是将能变的都变了,一直变到他不能变为止。。。

题意就是给你n个城市,m个交换点,s是起点,v是原始资金,问通过几个交换点能不能使钱变多?

感觉题意读着像走私神马的。。。

#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>#define eps 1e-8using namespace std;struct node{    int num1,num2;    double vv,rate;};node node[204];double d[204];int n,m,s;double v;bool bellman(){    int i,j;    bool flag;    while (d[s]<=v+eps)    {        flag=0;        for (i=0;i<2*m;i++)        {            if (d[node[i].num2]+eps<(d[node[i].num1]-node[i].vv)*node[i].rate)            {                flag=1;                d[node[i].num2]=(d[node[i].num1]-node[i].vv)*node[i].rate;            }        }          if (!flag)            return d[s]>v;    }    return 1;    }int main(){    int s1,s2,i;    double v1,v2,rate1,rate2;    while (scanf("%d%d%d%lf",&n,&m,&s,&v)!=EOF)    {        memset(d,0,sizeof(d));        memset(node,0,sizeof(node));        d[s]=v;        for (i=0;i<2*m;i++)        {            scanf("%d%d%lf%lf",&s1,&s2,&rate1,&v1);            node[i].num1=s1;node[i].num2=s2;            node[i].vv=v1;            node[i].rate=rate1;            i++;            scanf("%lf%lf",&rate2,&v2);            node[i].num2=s1;node[i].num1=s2;            node[i].vv=v2;            node[i].rate=rate2;        }        if (bellman()) printf("YES\n");        else printf("NO\n");    }    return 0;}
这题是在鬼节刷的。。。结果很神奇地0ms过了。。。应该是oj抽了。。。怪瘆人的。。。