首页 > 代码库 > POJ 1860: Currency Exchange 【SPFA】

POJ 1860: Currency Exchange 【SPFA】

套汇问题,从源点做SPFA,如果有一个点入队次数大于v次(v表示点的个数)则图中存在负权回路,能够套汇,如果不存在负权回路,则判断下源点到自身的最长路是否大于自身,使用SPFA时松弛操作需要做调整

#include<iostream>

#include<cstdio>

#include<string.h>

#include <stdlib.h>

#include <math.h>

using namespace std;

const int maxn=2000;

double dist[maxn]={0},rate[maxn][maxn]={{0}},ope[maxn][maxn]={{0}},v;

int n,m,s,cnt[maxn]={0};

bool spfa(int scr)

{

   int l=0,r=1,que[10000]={0},visit[maxn]={0},temp;

   que[++l]=scr;

   dist[scr]=v;

   cnt[scr]=1;

   while(l<=r)

    {

       temp=que[l++];

       visit[temp]=0;

       for(int i=1;i<=n;i++)

       {

           if (rate[temp][i]>0 &&dist[i]<(dist[temp]-ope[temp][i])*rate[temp][i])

           {

               dist[i]=(dist[temp]-ope[temp][i])*rate[temp][i];

                if (visit[i]==0)

               {

                    visit[i]=1;

                    que[++r]=i;

                    cnt[i]++;

                    if (cnt[i]>=n)returntrue;

                }

           }

       }

    }

   return dist[s]>v;

}

int main()

{

   int x,y;

   scanf("%d%d%d%lf",&n,&m,&s,&v);

   for(int i=1;i<=m;i++)

    {

       scanf("%d%d",&x,&y);

       scanf("%lf%lf%lf%lf",&rate[x][y],&ope[x][y],&rate[y][x],&ope[y][x]);

    }

   if(spfa(s))printf("YES\n");else printf("NO\n");

 

   return 0;

}

POJ 1860: Currency Exchange 【SPFA】