zoukankan      html  css  js  c++  java
  • poj1860 Bellman_Ford算法

    Currency Exchange
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 21922   Accepted: 7910

    Description

    Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency. 
    For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 - 0.39) * 29.75 = 2963.3975RUR. 
    You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B - numbers of currencies it exchanges, and real RAB, CAB, RBA and CBA - exchange rates and commissions when exchanging A to B and B to A respectively. 
    Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations. 

    Input

    The first line of the input contains four numbers: N - the number of currencies, M - the number of exchange points, S - the number of currency Nick has and V - the quantity of currency units he has. The following M lines contain 6 numbers each - the description of the corresponding exchange point - in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=103
    For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10-2<=rate<=102, 0<=commission<=102
    Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 104

    Output

    If Nick can increase his wealth, output YES, in other case output NO to the output file.

    Sample Input

    3 2 1 20.0
    1 2 1.00 1.00 1.00 1.00
    2 3 1.10 1.00 1.10 1.00
    

    Sample Output

    YES
     1 #include<stdio.h>
     2 #include<string.h>
     3 
     4 struct Node{
     5     int u,v;    // 起点,终点
     6     double r,c;
     7 };
     8 
     9 Node edge[221]; bool flg;
    10 double dis[110];
    11 
    12 int nodenum, edgenum, source,t;    // 结点数,边数,源点,起始值,总边数
    13 double V;
    14 
    15 void relax(int u,int v,double r,double c)   //*用double 
    16 {
    17     if(dis[v]<(dis[u]-c)*r);
    18         dis[v]=(dis[u]-c)*r;
    19 }
    20 
    21 bool Bellman_Ford()
    22 {
    23     int i,j,k;
    24     for(i=1;i<=nodenum;i++)
    25         dis[i]=0;
    26     dis[source]=V;
    27 
    28     for(i=1;i<=nodenum-1;i++)
    29     {
    30         for(j=1;j<=t;j++)                   //*注意是总边数
    31         {
    32             relax(edge[j].u,edge[j].v,edge[j].r,edge[j].c);
    33         }
    34         /*flg=false;   //优化做法
    35         for(j=1;j<=t;j++)
    36         {
    37             if(dis[edge[j].v] < (dis[edge[j].u]-edge[j].c)*edge[j].r)//求最大
    38             {
    39                 //flg=true;
    40                 dis[edge[j].v]=(dis[edge[j].u]-edge[j].c)*edge[j].r;
    41             }
    42         }
    43         if(!flg) return false;*/
    44     }
    45     //judge
    46 
    47     for(i=1;i<=t;i++)
    48     {
    49         if(dis[edge[i].v]<(dis[edge[i].u]-edge[i].c)*edge[i].r)
    50             return true;
    51     }
    52     return false;
    53 }
    54 
    55 
    56 int main()
    57 {
    58     int i,j,k;
    59     while(scanf("%d %d %d %lf",&nodenum,&edgenum,&source,&V)!=EOF)
    60     {
    61         t=1;
    62         for(i=1;i<=edgenum;i++)
    63         {
    64             scanf("%d %d %lf %lf",&edge[t].u,&edge[t].v,&edge[t].r,&edge[t].c);
    65             t++;
    66             edge[t].u=edge[t-1].v,edge[t].v=edge[t-1].u;
    67             scanf("%lf %lf",&edge[t].r,&edge[t].c);
    68             t++;
    69         }
    70         t--;                    //总边数值
    71         if(Bellman_Ford()==true)
    72             printf("YES
    ");
    73         else
    74             printf("NO
    ");
    75     }
    76     return 0;
    77 }
    View Code
  • 相关阅读:
    PHP添加Redis模块及连接
    Redis高级应用
    Redis常用命令
    Redis的数据类型及操作
    Redis下载及安装部署
    NoSQL介绍
    8种Nosql数据库系统对比
    JQ插件
    libcurl一般用法
    密钥对加密原理
  • 原文地址:https://www.cnblogs.com/cyd308/p/4539453.html
Copyright © 2011-2022 走看看