Currency Exchange

主要找正环:

 1 #include<cstdio>

 2  #include<cstring>

 3  #include<cstdlib>

 4  #include<algorithm>

 5  #include<iostream>

 6  #include<queue>

 7  #define MAXN 52000

 8  using namespace std;

 9  const int INF=-1<<28;

10  bool vis[MAXN];

11  int head[MAXN];

12  int next[MAXN];

13  int s;

14  double dis[MAXN];

15  struct node

16  {

17      int u,v;

18      double r,c;

19  }p[MAXN];

20  int e,n,m;

21  int cnt[MAXN];

22  double v;

23  void addnode(int u,int v,double r,double c)

24  {

25      p[e].u=u;

26      p[e].v=v;

27      p[e].r=r;

28      p[e].c=c;

29      next[e]=head[u];

30      head[u]=e++;

31  }

32  bool relax(int u,int v,double r,double c)

33  {

34      if(dis[v]<(dis[u]-c)*r)

35      {

36          dis[v]=(dis[u]-c)*r;

37          return true;

38      }

39      return false;

40  }

41  bool spfa(int rc)

42  {

43      memset(vis,false,sizeof(vis));

44      memset(cnt,0,sizeof(cnt));

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

46          dis[i]=0;

47      dis[rc]=v;

48      vis[rc]=true;

49      queue<int>q;

50      q.push(rc);

51      ++cnt[rc];

52      while(!q.empty()){

53          int pre=q.front();

54          q.pop();

55          vis[pre]=false;

56          for(int i=head[pre];i+1;i=next[i])

57          {

58              if(relax(pre,p[i].v,p[i].r,p[i].c)&&!vis[p[i].v]){

59                  if((++cnt[p[i].v])>n) return false;

60                  q.push(p[i].v);

61                  vis[p[i].v]=true;

62              }

63          }

64      }

65      return true;

66  }

67  int main()

68  {

69      memset(head, -1, sizeof(head));

70      memset(next, -1, sizeof(next));

71      e=0;

72      int a,b;

73      double r1,c1,r2,c2;

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

75      for(int i=0;i<m;i++){

76      scanf("%d%d%lf%lf%lf%lf",&a,&b,&r1,&c1,&r2,&c2);

77      addnode(a,b,r1,c1);

78      addnode(b,a,r2,c2);

79      }

80      if(!spfa(s)) printf("YES\n");

81      else printf("NO\n");

82      return 0;

83  }

84  

85  

86  
View Code

 

你可能感兴趣的:(Exchange)