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;

}

 

你可能感兴趣的:(Exchange)