2240

 
  
1 /*
2 判断是否存在正环
3 居然还有同种货币的兑换,此点很。。。
4 */
5
6 // include file
7 #include < cstdio >
8 #include < cstdlib >
9 #include < cstring >
10 #include < cmath >
11 #include < cctype >
12 #include < ctime >
13
14 #include < iostream >
15 #include < sstream >
16 #include < fstream >
17 #include < iomanip >
18 #include < bitset >
19 #include < strstream >
20
21 #include < algorithm >
22 #include < string >
23 #include < vector >
24 #include < queue >
25 #include < set >
26 #include < list >
27 #include < functional >
28
29 using namespace std;
30
31 // typedef
32 typedef long long LL;
33 typedef unsigned long long ULL;
34
35 //
36 #define read freopen("in.txt","r",stdin)
37 #define write freopen("out.txt","w",stdout)
38 #define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
39 #define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
40 #define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
41 #define FORp(a,b,c) for(int p=(a);p<(b);p+=c)
42
43 #define FF(i,a) for(int i=0;i<(a);i+++)
44 #define FFD(i,a) for(int i=(a)-1;i>=0;i--)
45 #define Z(a) (a<<1)
46 #define Y(a) (a>>1)
47
48 const double eps = 1e - 6 ;
49 const double INFf = 1e10;
50 const int INFi = 1000000000 ;
51 const double Pi = acos( - 1.0 );
52
53 template < class T > inline T sqr(T a){ return a * a;}
54 template < class T > inline T TMAX(T x,T y)
55 {
56 if (x > y) return x;
57 return y;
58 }
59 template < class T > inline T TMIN(T x,T y)
60 {
61 if (x < y) return x;
62 return y;
63 }
64 template < class T > inline void SWAP(T & x,T & y)
65 {
66 T t = x;
67 x = y;
68 y = t;
69 }
70 template < class T > inline T MMAX(T x,T y,T z)
71 {
72 return TMAX(TMAX(x,y),z);
73 }
74
75
76 // code begin
77 #define MAXN 35
78 int N,M;
79 bool F[MAXN][MAXN];
80 double G[MAXN][MAXN];
81 double dst[MAXN];
82 char name[MAXN][ 30 ];
83
84 int getdx( char * tar)
85 {
86 FORi( 1 ,N + 1 , 1 )
87 {
88 if ( strcmp(tar,name[i]) == 0 )
89 return i;
90 }
91 return - 1 ;
92 }
93
94 bool bellman_ford()
95 {
96
97 memset(dst, 0 , sizeof (dst));
98 dst[ 1 ] = 1.0 ;
99
100 FORi( 2 ,N + 1 , 1 )
101 {
102 FORj( 1 ,N + 1 , 1 )
103 {
104 FORk( 1 ,N + 1 , 1 )
105 {
106 if ( F[j][k] )
107 {
108 if ( dst[j] * G[j][k] > dst[k] )
109 {
110 dst[k] = dst[j] * G[j][k];
111 }
112 }
113 }
114 }
115 }
116
117
118 //
119 FORj( 1 ,N + 1 , 1 )
120 {
121 FORk( 1 ,N + 1 , 1 )
122 {
123 if ( F[j][k] )
124 {
125 if (dst[j] * G[j][k] > dst[k]) return true ;
126 }
127 }
128 }
129
130 return false ;
131 }
132
133 int main()
134 {
135 read;
136 write;
137 char c1[ 30 ],c2[ 30 ];
138 double r;
139 int cas = 1 ;
140 while (scanf( " %d " , & N) !=- 1 )
141 {
142 if (N == 0 ) break ;
143 FORi( 1 ,N + 1 , 1 )
144 {
145 scanf( " %s " ,name[i]);
146 }
147 memset(F, 0 , sizeof ( bool ) * MAXN * MAXN);
148 scanf( " %d " , & M);
149 while (M -- )
150 {
151 scanf( " %s %lf %s " ,c1, & r,c2);
152 int a = getdx(c1);
153 int b = getdx(c2);
154 F[a][b] = 1 ;
155 G[a][b] = r;
156 }
157 printf( " Case %d: " ,cas ++ );
158 if (bellman_ford()) printf( " Yes\n " );
159 else printf( " No\n " );
160 }
161 return 0 ;
162 }

转载于:https://www.cnblogs.com/ac2012/archive/2011/03/03/1969936.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值