POJ1860-Currency Exchange-判正环

两种货币的交换可以当成两条边,建图后跑Bellman_ford算法就好了。

Bellman_ford算法可以用来处理负边权,所以可以判断是否存在负环。反过来就可以判断是否存在正环。

 1 /*--------------------------------------------------------------------------------------*/
 2 //        Helica‘s header
 3 //        Second Edition
 4 //        2015.11.7
 5 //
 6 #include <algorithm>
 7 #include <iostream>
 8 #include <cstring>
 9 #include <ctype.h>
10 #include <cstdlib>
11 #include <cstdio>
12 #include <vector>
13 #include <string>
14 #include <queue>
15 #include <stack>
16 #include <cmath>
17 #include <set>
18 #include <map>
19
20 //debug function for a N*M array
21 #define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)22 {for(int j=0;j<(M);j++){23 printf("%d",G[i][j]);}printf("\n");}
24 //debug function for int,float,double,etc.
25 #define debug_var(X) cout<<#X"="<<X<<endl;
26 /*--------------------------------------------------------------------------------------*/
27 using namespace std;
28
29 const int maxn = 100+10;
30 int N,M,T,S;
31 int tol;
32 double V;
33
34 struct Edge
35 {
36     int u,v;
37     double c,r;
38 }E[10*maxn];
39
40 double dist[maxn];
41
42 bool bellman_ford(int start,int n)
43 {
44     memset(dist,0,sizeof dist);
45     dist[start] = V;
46
47     for(int i=1;i<n;i++)
48     {
49         bool flag = false;
50         for(int j=0;j<tol;j++)if(dist[E[j].v] < (dist[E[j].u]-E[j].c)*E[j].r )
51         {
52             dist[E[j].v] = (dist[E[j].u]-E[j].c)*E[j].r;
53            flag = true;
54         }
55         if(!flag) return false;
56     }
57
58     for(int j=0;j<tol;j++)
59     {
60         if(dist[E[j].v] < (dist[E[j].u]-E[j].c)*E[j].r)
61             return true;
62     }
63     return false;
64 }
65
66 int main()
67 {
68     while(~scanf("%d%d%d%lf",&N,&M,&S,&V))
69     {
70         int a,b;
71         double r1,r2,c1,c2;
72         tol = 0;
73         for(int i=0;i<M;i++)
74         {
75             scanf("%d%d%lf%lf%lf%lf",&a,&b,&r1,&c1,&r2,&c2);
76             E[tol].u = a;E[tol].v = b;
77             E[tol].c = c1;E[tol++].r = r1;
78             E[tol].u = b;E[tol].v = a;
79             E[tol].c = c2;E[tol++].r = r2;
80         }
81         if(bellman_ford(S,N))
82             printf("YES\n");
83         else
84             printf("NO\n");
85
86     }
87 }
时间: 2024-12-28 02:34:31

POJ1860-Currency Exchange-判正环的相关文章

POJ-1860 Currency Exchange( Bellman_Ford, 正环 )

题目链接:http://poj.org/problem?id=1860 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can b

poj1860(spfa判正环)

题目连接:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 money=(nowmoney-手续费)*rate,现在问你有v钱,从s开始出发交换钱能不能赚钱. 分析:如何存在正环,能无限增加钱,肯定可以赚了,因此用spfa判一下即可 #include <cstdio> #include <cstring> #include <string> #include <cmath&

POJ-1860 Currency Exchange---Bellman-Ford判断正环

题目链接: https://vjudge.net/problem/POJ-1860 题目大意: 我们的城市有几个货币兑换点.让我们假设每一个点都只能兑换专门的两种货币.可以有几个点,专门从事相同货币兑换.每个点都有自己的汇率,外汇汇率的A到B是B的数量你1A.同时各交换点有一些佣金,你要为你的交换操作的总和.在来源货币中总是收取佣金. 例如,如果你想换100美元到俄罗斯卢布兑换点,那里的汇率是29.75,而佣金是0.39,你会得到(100 - 0.39)×29.75=2963.3975卢布. 你

POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1860 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two par

POJ-1860 Currency Exchange 【spfa判负环】

Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the

POJ1860 Currency Exchange【BellmanFord算法】【求正权回路】

Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20994 Accepted: 7522 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and perfor

poj1860——Currency Exchange(Eellman-Ford+权值为正的环路)

Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the

POJ 1860 货币兑换 SPFA判正环

Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20280   Accepted: 7270 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and pe

POJ1860:Currency Exchange(BF)

http://poj.org/problem?id=1860 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be sev

POJ1860——Currency Exchange(BellmanFord算法求最短路)

Currency Exchange DescriptionSeveral currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points sp