poj 1860 Currency Exchange

本文探讨了通过货币兑换实现资金增值的策略与条件,详细解释了货币兑换过程、盈利判断及算法实现。

题意:就是货币兑换;问你经过几次兑换时候比原来的钱数是否多;

 N -货币种类, M - 兑换的方式, S - Nick拥有的币种 ,V - Nick该种币种的数值数;

注意:货币的兑换可以循环使用多次;

View Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
class Node
{
public:
      int num;
      double rate,com,sum;    
      Node *next;
};
int flag = 0,S;double V;
Node node[124];
int visit[124];
Node *New_Node( int num ,double rate,double com )
{
    Node *e = new Node;
    e->num = num; e->rate = rate;e->com = com,e->sum = 0;
    e->next = NULL;
    return e;
}
void DFS( int n,Node *p )//深搜 
{
    Node *q;
    double t = (node[n].sum - p->com)*p->rate;
    double  sum = node[p->num].sum;
    if( (t > V && p -> num == S)||( t > node[p->num].sum && visit[p->num] )) 
    {//如果到了开始的位置并且大于开始值或者循环使钱数增多;表示可以盈利 
       flag = 1; return ;    
    }
      if( t > node[p->num].sum )
      {
        visit[p->num] = 1;
        node[p->num].sum = t;
        q = node[p->num].next;
        while( q && !flag )
        {
            DFS( p -> num ,q );    
            q = q -> next;
        } 
        node[p->num].sum = sum;  //回朔 
        visit[p->num] = 0;        
    }
    return ;
}
void Empty_Node( )//初始化 
{
    for( int i = 0 ; i < 124 ; i ++ )
    {
        node[i].next = NULL;
        node[i].sum = 0;node[i].num = 0;
        node[i].rate = 0 ; node[i].com = 0;
        visit[i] = 0;

    }    
}
int main(  )
{
    int N,M,start,end;
    double rate1,com1,rate2,com2;
    while( scanf( "%d %d %d %lf",&N,&M,&S,&V )==4 )
    {        
        flag = 0;
        Node *p,*q;
        Empty_Node( );
          for( int i = 0 ; i < M ; i ++ )//建立邻接表 
          {
            scanf( "%d %d %lf %lf %lf %lf",&start ,&end,&rate1,&com1,&rate2,&com2 );
            p = node[start].next;
            q = New_Node( end , rate1, com1 );
            node[start].next = q ;    q -> next = p;
            p = node[end].next;
            q = New_Node( start , rate2, com2 ); 
            node[end].next = q; q -> next = p;
        }
        node[S].sum = V;
        p = node[S].next;
        visit[S] = 1;
        while( p&&!flag )
        {
            DFS( S,p );
            p = p -> next;    
        }
        if( flag ) printf( "YES\n" );
        else printf( "NO\n" );
    }
    //system( "pause" );
    return 0;
}

 

转载于:https://www.cnblogs.com/bo-tao/archive/2012/06/30/2571095.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值