zoukankan      html  css  js  c++  java
  • Currency Exchange(判断是否有正环)

    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 16456   Accepted: 5732

    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 // spfa
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<queue>
     5 using namespace std;
     6 
     7 const int INF = 0x3f3f3f3f;
     8 queue <int> que;
     9 double rate[101][101];
    10 double commission[101][101];
    11 double dis[101];
    12 int n,m,s;
    13 double v;
    14 int inque[101];
    15 int map[101][101];
    16 
    17 bool spfa()
    18 {
    19     while(!que.empty())
    20         que.pop();
    21     memset(inque,0,sizeof(inque));
    22     for(int i = 1; i <= n; i++)
    23         dis[i] = 0;
    24     dis[s] = v;
    25     que.push(s);
    26     inque[s] = 1;
    27     while(!que.empty())
    28     {
    29         int u = que.front();
    30         que.pop();
    31         inque[u] = 0;
    32         for(int i = 1; i <= n; i++)
    33         {
    34             if(map[u][i] != INF && dis[i] < (dis[u]-commission[u][i])*rate[u][i])
    35             {
    36                 dis[i] = (dis[u]-commission[u][i])*rate[u][i];
    37                 if(inque[i] == 0)
    38                 {
    39                     inque[i] = 1;
    40                     que.push(i);
    41                 }
    42             }
    43         }
    44         if(dis[s] > v) return true;//若松弛后dis[s] > v 说明有正环
    45     }
    46     return false;
    47 }
    48 int main()
    49 {
    50     int a,b;
    51     while(~scanf("%d %d %d %lf",&n,&m,&s,&v))
    52     {
    53         memset(map,INF,sizeof(map));
    54         for(int i = 1; i <= m; i++)
    55         {
    56             scanf("%d %d",&a, &b);
    57             scanf("%lf %lf %lf %lf",&rate[a][b],&commission[a][b],&rate[b][a],&commission[b][a]);
    58             map[a][b] = 0;
    59             map[b][a] = 0;
    60         }
    61         if(spfa())
    62             printf("YES
    ");
    63         else printf("NO
    ");
    64     }
    65     return 0;
    66 }
    View Code
     1 //Bellman_ford
     2 
     3 #include<stdio.h>
     4 #include<string.h>
     5 struct node
     6 {
     7     int u,v;
     8     double rate,comm;
     9 }map[210];
    10 int vis[101];
    11 int n,m,s,cnt;
    12 double v;
    13 double dis[101];
    14 
    15 bool Bellman_ford()
    16 {
    17     memset(dis,0,sizeof(dis));//dis[]初始化为0;
    18     dis[s] = v;
    19     int i,j,flag;
    20     for(i = 1;i <= n; i++)
    21     {
    22         flag = 0;
    23         for(j = 0; j < cnt; j++)//松弛任意两点
    24         {
    25             if(dis[map[j].v] < (dis[map[j].u]-map[j].comm)*map[j].rate)
    26             {
    27                 dis[map[j].v] = (dis[map[j].u]-map[j].comm)*map[j].rate;
    28                 flag = 1;
    29             }
    30         }
    31         if(flag == 0)
    32             break;
    33     }
    34     if(i >= n+1)//若松弛n次还可以松弛说明存在正环;
    35         return true;
    36     else return false;
    37 }
    38 int main()
    39 {
    40     int a,b;
    41     double c,d,e,f;
    42     while(~scanf("%d %d %d %lf",&n,&m,&s,&v))
    43     {
    44         cnt = 0;
    45         while(m--)
    46         {
    47             scanf("%d %d %lf %lf %lf %lf",&a,&b,&c,&d,&e,&f);
    48             map[cnt++] = ((struct node){a,b,c,d});
    49             map[cnt++] = ((struct node){b,a,e,f});
    50         }
    51         if(Bellman_ford())
    52             printf("YES
    ");
    53         else printf("NO
    ");
    54     }
    55     return 0;
    56 }
    View Code
  • 相关阅读:
    sqlserver OpenRowSet 对应的三种数据库驱动
    讨论贴:在sp_executesql 中生成的临时表的可见性
    避免创建表的情况下,执行存储过程插入临时表
    讨论贴:Sqlserver varbinary 是二进制数据,却是十六进制的表现形式
    对于unallocated space的翻译 我想说几句话
    增删改查 的一些不常用的小技巧
    转 TextBox的EnableViewState属性问题
    总结 output 用法
    VSIX 插件右键菜单(2)
    MVC使用记录
  • 原文地址:https://www.cnblogs.com/LK1994/p/3243285.html
Copyright © 2011-2022 走看看