zoukankan      html  css  js  c++  java
  • POJ 1860 Currency Exchange

    题目描述

    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

    题目分析

    题面很容易读懂,至于题意,大概就是给你若干的货币种类和他们之间的部分汇率和兑换税率,给你一个特定货币的数值,问能否通过操作,使得此种类的货币增加。我们可以轻而易举的构建图的模型,换而言之,题目的意思就是让我们求这张图是否存在一个回路使得我们的权值无限增大。是的,你想到了什么?求负环!
    那么我们自然而然可以想到spfa和bellman-ford。由于考虑到有的同学没有学过最短路,所以我们这里普及一下这两个算法。

    bellman-ford

    在此之前我们可能要了解一下松弛这个东西,类似于dis[i]=min(dis[x]+w[x][i],dis[i])的操作我们把他叫做松弛,让我们来想一想,对应一张图来说,我们对遍历每一个点,对他进行一轮松弛,那么最坏的情况,我们只会更新掉第一个点所可以连接的点的dis点的值,所以这里我们考虑最坏的一种情况,要进行n-1次的松弛才可以更新完所以的dis点,当然,我们这里不考虑存在负环,如果存在负环,那么事情就会变的微妙,想一想为什么?基于这样的思想,下面我们实现一下这个算法,这是一份板子,关于spfa的话是bellman-ford的一个队列优化,有兴趣大家可以去了解一下,我这里就不做多的介绍了。

          #include<bits/stdc++.h>
          #define MEM(x, y) memset(x, y, sizeof(x))
          #define ll long long
           using namespace std;
          const int maxnum=100;
          const int maxint=99999; 
          const double eps=1e-6;
          const int inf=0x3f3f3f3f;
          const double PI=acos(-1.0);
          //bellman-ford 
          struct edge {
    	int u,v,w;
            };
          edge e[maxnum];
          int dis[maxnum],pre[maxnum];
          int nodenum,edgenum,s;
          void init () {
    	      cin>>nodenum>>edgenum>>s;
    	      for (int i=1;i<=nodenum;i++) {
    		      dis[i]=maxint;
    	      }
    	      dis[s]=0;
    	      for (int i=1;i<=edgenum;i++) {
    		      cin>>e[i].u>>e[i].v>>e[i].w;
    		      if (e[i].u==s) dis[e[i].v]=e[i].w;
    	      }
          }
          void relax (int u,int v,int w) {
    	      if (dis[v]>dis[u]+w) {
    		      dis[v]=dis[u]+w;
    		      pre[v]=u;
    	      }
          }
          bool bellman () {
    	      for (int i=1;i<=nodenum-1;i++) 
    	       for (int j=1;j<=edgenum;j++) 
    	        relax(e[j].u,e[j].v,e[j].w);
    	      bool flag=0;
    	      for (int i=1;i<=nodenum;i++) {
    		      if (dis[e[i].v]>dis[e[i].v]+e[i].w); {
    		         flag=1;
    		         break;
    		      }
    	      }
    	      return flag;
          }
          void print_path (int root) {
    	      while (root!=pre[root]) {
    		      printf ("%d ",root);
    		      root=pre[root];
    	      }
    	      if (root==pre[root]) cout<<root;
          }
          int main( ) {
            init();
            bellman();
            for (int i=1;i<=nodenum;i++) cout<<dis[i]<<" ";
            return 0;
          }
    

    本题代码实现

    上述的板子里面给出的负环的检测,我们这题的话有两个要点
    -在松弛的时候,我们是选择大的距离
    -在检测到起点的值被更新大以后我们就可以直接判定成功

    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<vector> 
    #include<algorithm>
    using namespace std;
    const int maxn= 10005;
    const int N=1e6+5;
    const int inf=0x3f3f3f3f;
    typedef long long ll;
    int n, m, s;
    double dis[maxn], cost[maxn][maxn], rate[maxn][maxn], v;
    bool vis[maxn];
    bool spfa (int start) {
    	memset (vis,0,sizeof (vis));
    	memset (dis,0,sizeof (dis));
    	dis[start] = v;
    	queue <int> q;
    	q.push(start);
    	vis[start] = 1;
    	while (!q.empty()) {
    		int x=q.front ();
    		q.pop();
    		vis[x] = 0;
    		for (int i=0;i<=n;i++) {
    			if (dis[i] < (dis[x]-cost[x][i])*rate[x][i]) dis[i]=(dis[x]-cost[x][i])*rate[x][i];
    			if (dis[start] >v) return 1;
    			if (!vis[i]) {
    			   q.push(i);
    			   vis[i] = 1;	
    			}  
    		}
    	}
    	return false;
    }
    int main() {
        while (cin>>n>>m>>s>>v) {
        	double rab, rba, cba, cab;
        	int a, b;
        	for (int i=1;i<=n;i++) 
        	 for (int j=1;j<=n;j++) {
        	 	if (i==j) rate[i][j] = 1;
        	 	else rate[i][j] = 0, cost[i][j] = 0;
    		 }
    		for (int i=1;i<=m;i++) {
    			cin>>a>>b>>rab>>cab>>rba>>cba;
    		    rate[a][b] = rab;
    		    rate[b][a] = rba;
    		    cost[a][b] = cab;
    		    cost[b][a] = cba;
    		}
    		if (spfa (s)) cout<<"Yes"<<endl; 
    		else cout<<"No"<<endl;
    		
    	}
        
    	return 0;
    }
    
  • 相关阅读:
    ngx_lua_waf完整安装说明
    Linux(CentOS)下的JDK的安装和环境配置
    Genymotion的2个问题及解决方法
    Appscan的第一个测试请求就是提交MAC地址
    oracle相关知识
    数据结构之树
    kafka的写入内存?硬盘
    算法的时间复杂度和空间复杂度
    Java线程池
    mapReduce和spark的shuffle
  • 原文地址:https://www.cnblogs.com/hhlya/p/13061752.html
Copyright © 2011-2022 走看看