zoj1544

题目大意:

你知道我们的城市有N种不同的货币,交换点可以用6个数字描述:整数A和B,它交换的货币数量,和实数RAB, CAB, RBA and CBA-从A交换到B和从B交换到A的交换率和佣金
Nick有一些S货币,而且他想知道一些交换操作之后,他的钱能不能涨
输入:N,货币的数量,M,交换点的数量,S,Nick货币的编号,V,他的钱数
接下来就是A,B,RAB,CAB, RBA,CBA

解题思路:

最短路问题

代码如下:

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define eps 1e-5
#define SIZE 110
using namespace std;

int N,S,M;
double V;
typedef struct node
{
  int x,y;
  double rate,comm;
}node;
node arr[SIZE*SIZE];
int cou;

int Bellman()
{
  double dis[SIZE];
  for(int i=1;i<=N;i++)
    dis[i]=-1.0;
  dis[S]=V;
  while(1)
  {
    int flag=0;
    for(int k=0;k<cou;k++)
    {
      int x=arr[k].x;
      if(dis[x]<eps)  continue;
      int y=arr[k].y;
      double comm=arr[k].comm;
      double rate=arr[k].rate;
      if((dis[x]-comm)*rate-dis[y]>eps)
      {
        dis[y]=(dis[x]-comm)*rate;
        flag=1;
      }
    }
    if(dis[S]>V)  return 1;
    if(!flag)  
      break;
  }
  return 0;
}

int main()
{
  int x,y;
  while(scanf("%d%d%d%lf",&N,&M,&S,&V)!=EOF)
  {
    cou=0;
    for(int i=0;i<M;i++)
    {
      scanf("%d%d",&x,&y);
      arr[cou].x=x;
      arr[cou].y=y;
      scanf("%lf%lf",&arr[cou].rate,&arr[cou].comm);
      cou++;
      arr[cou].x=y;
      arr[cou].y=x;
      scanf("%lf%lf",&arr[cou].rate,&arr[cou].comm);
      cou++;
    }
    if(Bellman())
      printf("YES\n");
    else 
      printf("NO\n");
  }
  return 0;
}

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值