zoukankan      html  css  js  c++  java
  • Currency Exchange

    Currency Exchange
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 23346   Accepted: 8450

    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
    题意:某种货币经过转换后是否可以增值。
    判断是否含有正环。
    Bellmen_Ford方法实现:
    遍历途中所有的边(edge(u,v)),判断是否存在这样情况:
    d(v) > d (u) + w(u,v)
    则返回false,表示途中存在从源点可达的权为负的回路。
    初始化d(S)=V   而源点到其他店的距离(权值)初始化为无穷小(0),
    当s到其他某点的距离能不断变大时,说明存在最大路径

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 
     6 using namespace std;
     7 
     8 #define N 1100
     9 #define INF 0xfffffff
    10 
    11 int n, m, s, cnt = 0;
    12 double sum;
    13 
    14 double dist[N];
    15 
    16 struct node
    17 {
    18     int u, v, next;
    19     double c, r;
    20 }e[N*4];
    21 
    22 void add(int u, int v, double r, double c)
    23 {
    24     e[cnt].u = u;
    25     e[cnt].v = v;
    26     e[cnt].c = c;
    27     e[cnt].r = r;
    28     cnt++;
    29 }
    30 
    31 int slove()
    32 {
    33     for(int i = 1; i <= n; i++)
    34         dist[i] = 0;
    35 
    36     dist[s] = sum;
    37     int i;
    38     for(i = 1; i <= n; i++)
    39     {
    40         int flag = 0;
    41         for(int j = 0; j < cnt; j++)
    42         {
    43             if(dist[e[j].v] < (dist[e[j].u]-e[j].c)*e[j].r)
    44             {
    45                 dist[e[j].v] = (dist[e[j].u]-e[j].c)*e[j].r;
    46                 flag = 1;
    47             }
    48         }
    49         if(!flag)
    50             break;
    51     }
    52     if(i == n+1)
    53         return true;
    54     return false;
    55 }
    56 
    57 int main()
    58 {
    59     scanf("%d%d%d%lf", &n, &m, &s, &sum);
    60     int u, v;
    61     double cu, ru, cv, rv;
    62 
    63     while(m--)
    64     {
    65         scanf("%d%d%lf%lf%lf%lf", &u, &v, &cu, &ru, &cv, &rv);
    66         add(u, v, cu, ru);
    67         add(v, u, cv, rv);
    68     }
    69     if(slove())
    70         printf("YES
    ");
    71     else
    72         printf("NO
    ");
    73     return 0;
    74 }

    spfa:

    两种情况Yes,一种是存在正权回路,一种是求最长路后,实现了增值,也是Yes
    用spfa来判断是否存在正权回路,其实spfa是可以用来判断是否存在回路的,不管是正权还是负权,只不过它们松弛的条件不同,正权的话,我们是往dis[]权值增大的方向松弛,负权的话,我们是往dis[]权值减少的方向松弛,然后判断是否存在回路只要看有没有一点入队列的次数大于n就行了
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <queue>
     5 #define maxn 107
     6 #define inf 0x7fffffff
     7 #define eps 1e-8
     8 using namespace std;
     9 int ct[maxn];
    10 double r[maxn][maxn],c[maxn][maxn],dis[maxn],v;
    11 bool visit[maxn];
    12 int n,m,s;
    13 bool spfa()
    14 {
    15     queue<int>q;
    16     while (!q.empty()) q.pop();
    17     dis[s] = v;
    18     q.push(s);
    19     visit[s] = true; ct[s]++;//ct统计入队次数
    20     while (!q.empty())
    21     {
    22         int u = q.front(); q.pop();
    23         for (int i = 1; i <= n; ++i)
    24         {
    25             if ( (dis[u] - c[u][i])*r[u][i] - dis[i]  > eps)
    26             {
    27                 dis[i] = (dis[u] - c[u][i])*r[u][i];28                 
    29                 if (!visit[i])//判断是否在队列中,然后入队
    30                 {
    31                     visit[i] = true;
    32                     q.push(i);
    33                     if (++ct[i] > n) return true;
    34                 }
    35             }
    36         }
    37         visit[u] = false;//把根节点标为未入队,因为后面的节点可能还会更新他这是与bfs的区别
    38     }
    39     if (dis[s] - v > eps) return true;
    40     else return false;
    41 }
    42 int main()
    43 {
    44     int i,j;
    45     for (i = 0; i < maxn; ++i)
    46     {
    47         visit[i] = false;
    48         ct[i] = 0; dis[i] = 0;
    49         for (j = 0; j < maxn; ++j)
    50         {
    51             r[i][j] = c[i][j] = 0;
    52         }
    53     }
    54     scanf("%d%d%d%lf",&n,&m,&s,&v);
    55     int a,b;
    56     for (i = 0; i < m; ++i)
    57     {
    58         scanf("%d%d",&a,&b);
    59         scanf("%lf%lf%lf%lf",&r[a][b],&c[a][b],&r[b][a],&c[b][a]);//建图
    60     }
    61     if (spfa()) printf("YES
    ");
    62     else        printf("NO
    ");
    63 
    64 }
  • 相关阅读:
    .Net Core部署到CentOS
    Docker容器中开始.Net Core之路
    自我总结和学习表单提交的几种方式 (二)
    自我总结和学习表单提交的几种方式 (一)
    Asp.Net MVC下自定义错误页和展示错误页的几种方式
    .Net下发送邮件遇到问题及解决方案
    Asp.Net MVC CodeFirst模式数据库迁移步骤
    利用微软认知服务实现语音识别功能
    Asp.Net MVC路由生成URL过程
    针对于多线程概念的理解
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4794825.html
Copyright © 2011-2022 走看看