URAL 1162 Currency Exchange (Bellman-Ford贝尔曼-福特算法)

#include <stdio.h>

int numOfCurrencies, numOfExchPoints, NickCurrencyNum;
double NickCurrencyAmount;
typedef struct Exchange{
	int from;
	int to;
	double rate;
	double commisssions;
}Exchange;
Exchange ExchangeArray[100000];
int numOfExchanges;
//wealth[currency]表示当把财富换成类型为currency的现金时的数量
double wealth[200];

int exchangeToIncrease(){
	wealth[NickCurrencyNum] = NickCurrencyAmount;
	int relax;
	for (relax = 1; relax < numOfCurrencies; relax++){
		int needContinue = 0;
		int ExchangeIndex;
		for (ExchangeIndex = 1; ExchangeIndex <= numOfExchanges; ExchangeIndex++){
			Exchange currentExchange = ExchangeArray[ExchangeIndex];
			//Bellman-Ford算法还能这样用!!!
			double wealthExchanged = (wealth[currentExchange.from] - currentExchange.commisssions) * currentExchange.rate;
			if ( wealthExchanged > wealth[currentExchange.to]){
				needContinue = 1;
				wealth[currentExchange.to] = wealthExchanged;
			}
		}
		if (!needContinue)
			break;
	}

	int ExchangeIndex;
	for (ExchangeIndex = 1; ExchangeIndex <= numOfExchanges; ExchangeIndex++){
		Exchange currentExchange = ExchangeArray[ExchangeIndex];
			if ( (wealth[currentExchange.from] - currentExchange.commisssions) * currentExchange.rate > wealth[currentExchange.to] )
				return 1;
	}
	return 0;
}

void addExchange(int from, int to, double rate, double comissions){
	numOfExchanges++;
	int ExchangeNum = numOfExchanges;
	ExchangeArray[ExchangeNum].from = from;
	ExchangeArray[ExchangeNum].to = to;
	ExchangeArray[ExchangeNum].rate = rate;
	ExchangeArray[ExchangeNum].commisssions = comissions;
}

int main(){
	
	scanf("%d%d%d%lf", &numOfCurrencies, &numOfExchPoints, &NickCurrencyNum, &NickCurrencyAmount);
	int exchangePoint;
	for (exchangePoint = 1; exchangePoint <= numOfExchPoints; exchangePoint++){
		int a, b;
		double rab, cab, rba, cba;
		scanf("%d%d%lf%lf%lf%lf", &a, &b, &rab, &cab, &rba, &cba);
		addExchange(a, b, rab, cab);
		addExchange(b, a, rba, cba);
	}

	printf("%s\n", exchangeToIncrease() == 1 ? "YES" : "NO");

	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值