zoukankan      html  css  js  c++  java
  • 18.11.16 POJ 1860 Currency Exchange(有向图最短路)

    描述

    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.
    输入

    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.
    输出

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

    样例输入

    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
    

    样例输出

    YES

    来源

    Northeastern Europe 2001, Northern Subregion

     1 #include <iostream>
     2 #include <string.h>
     3 #include <algorithm>
     4 #include <stack>
     5 #include <string>
     6 #include <math.h>
     7 #include <queue>
     8 #include <stdio.h>
     9 #include <string.h>
    10 #include <vector>
    11 #include <fstream>
    12 #include <set>
    13 #define  inf 999999;
    14 
    15 using namespace std;
    16 int n, m, s;
    17 double v;
    18 double dist[101];
    19 struct edge{
    20     int from, to;
    21     double rate,comm;
    22     edge() {}
    23     edge(int a,int b,double c,double d) {
    24         from = a, to = b;
    25         rate = c, comm = d;
    26     }
    27 };
    28 vector<edge>all;
    29 
    30 void bellmanford(){
    31     for (int i = 1; i < n; i++)
    32     {
    33         bool flag = false;
    34         for (int i = 0; i < all.size(); i++) {
    35             int s = all[i].from, e = all[i].to;
    36             double newval = (dist[s] - all[i].comm)*all[i].rate;
    37             if (dist[e] < newval)
    38             {
    39                 flag = true;
    40                 dist[e] = newval;
    41             }
    42         }
    43         if (!flag)
    44             break;
    45     }
    46     for (int i = 0; i < all.size(); i++) {
    47         int s = all[i].from, e = all[i].to;
    48         double newval = (dist[s] - all[i].comm)*all[i].rate;
    49         if (dist[e] < newval)
    50         {
    51             printf("YES
    ");
    52             return;
    53         }
    54     }
    55     printf("NO
    ");
    56 }
    57 
    58 void init() {
    59     scanf("%d%d%d%lf", &n, &m, &s, &v);
    60     for (int i = 1; i <= m; i++) {
    61         int x, y;
    62         double r1, r2, c1, c2;
    63         scanf("%d%d", &x, &y);
    64         scanf("%lf%lf%lf%lf", &r1, &c1, &r2, &c2);
    65         all.push_back(edge(x, y, r1,c1));
    66         all.push_back(edge(y, x, r2,c2));
    67     }
    68     dist[s] = v;
    69     bellmanford();
    70 }
    71 
    72 int main()
    73 {
    74     init();
    75     return 0;
    76 }
    View Code

    wa点:

    现在拿的币种不一定是1号!

     1 int n, m, s;
     2 double v;
     3 struct Edge {
     4     int s, e;
     5     double rse, cse,res,ces;
     6 }edge[maxn];
     7 double money[maxn];
     8 
     9 void init() {
    10     scanf("%d%d%d%lf", &n, &m, &s, &v);
    11     for (int i = 1; i <= m; i++)
    12         scanf("%d%d%lf%lf%lf%lf", &edge[i].s, &edge[i].e,
    13             &edge[i].rse, &edge[i].cse, &edge[i].res, &edge[i].ces);
    14     money[s] = v;
    15     for (int k = 1; k <= 2*m; k++)
    16     {
    17         for (int i = 1; i <= m; i++) {
    18             int x = edge[i].s, y = edge[i].e;
    19             double se = (money[x] - edge[i].cse)*edge[i].rse;
    20             double es = (money[y] - edge[i].ces)*edge[i].res;
    21             if (money[x] && se > money[y])
    22                 money[y] = se;
    23             if (money[y] && es > money[x])
    24                 money[x] = es;
    25         }
    26     }
    27     for (int i = 1; i <= m; i++) {
    28         int x = edge[i].s, y = edge[i].e;
    29         double se = (money[x] - edge[i].cse)*edge[i].rse;
    30         double es = (money[y] - edge[i].ces)*edge[i].res;
    31         if (money[x] && se > money[y]) {
    32             printf("YES
    ");
    33             return;
    34         }
    35         if (money[y] && es > money[x]) {
    36             printf("YES
    ");
    37             return;
    38         }
    39     }
    40     printf("NO
    ");
    41 }
    View Code

    更新!

    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    Zend框架2入门(二) (转)
    Zend框架2入门(一) (转)
    PHP Strict standards:Declaration of … should be compatible with that of…(转)
    ::符号
    mysql查询今天,昨天,近7天,近30天,本月,上一月数据的方法(转)
    php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法(转)
    PHP5.4新特性(转)
    PHP5.4的变化关注---What has changed in PHP 5.4.x(转)
    关于PHP的curl开启问题 (转)
    安装apache重启的时候,报错端口被占用,错误1
  • 原文地址:https://www.cnblogs.com/yalphait/p/9970929.html
Copyright © 2011-2022 走看看