标签:
Arbitrage
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5610 Accepted Submission(s): 2610
1 #include <map> 2 #include <queue> 3 #include <cmath> 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 using namespace std; 8 const int INF = 0x3f3f3f3f; 9 double ap[33][33], dis[33]; int vis[33]; 10 map<string, int> mmap; 11 int n; 12 bool Spfa(int src) 13 { 14 memset(vis, 0, sizeof(vis)); 15 for(int i = 1; i <= n; i++) 16 dis[i] = 0.0; 17 dis[src] = 1.0; vis[src] = 1; //****************// 18 queue<int> q; 19 q.push(src); 20 while(!q.empty()) 21 { 22 int temp = q.front(); 23 q.pop(); 24 vis[temp] = 0; 25 for(int j = 1; j <= n; j++) 26 { 27 if(dis[j] < dis[temp] * ap[temp][j]) 28 { 29 dis[j] = dis[temp] * ap[temp][j]; 30 if(!vis[j]) 31 q.push(j); 32 vis[j] = 1; 33 if(dis[src] > 1.0) 34 return true; 35 } 36 } 37 } 38 return false; 39 } 40 int main() 41 { 42 int Q = 1; 43 while(~scanf("%d", &n), n) 44 { 45 memset(ap, 0, sizeof(ap)); 46 mmap.clear(); string mon; 47 for(int i = 1; i <= n; i++) //无向图边权关系存储, 按照有向图来存也可。 48 { 49 cin >> mon; 50 mmap[mon] = i; 51 } 52 int m; 53 scanf("%d", &m); 54 string a, b; double oo; 55 for(int i = 1; i <= m; i++) 56 { 57 cin >> a >> oo >>b; 58 ap[mmap[a]][mmap[b]]=oo; 59 } 60 if(Spfa(1)) 61 printf("Case %d: Yes\n", Q++); 62 else 63 printf("Case %d: No\n", Q++); 64 } 65 return 0; 66 }
Fyold:
1 #include <map> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 const int INF = 0x3f3f3f3f; 7 map<string, int> mmap; 8 double ap[33][33]; 9 int n; 10 bool Floyd() 11 { 12 for(int k = 1; k <= n; k++) 13 for(int i = 1; i <= n; i++) 14 for(int j = 1; j <= n; j++) 15 if(ap[i][j] < ap[i][k] * ap[k][j]){ 16 ap[i][j] = ap[i][k] * ap[k][j]; 17 // printf("%.4lf\n", ap[i][j]); 18 } 19 for(int i = 1; i <= n; i++) //*******************// 20 if(ap[i][i] > 1) 21 return true; 22 return false; 23 } 24 int main() 25 { 26 int Q = 1; 27 while(~scanf("%d", &n), n) 28 { 29 mmap.clear(); 30 for(int i = 1; i <= n; i++) 31 for(int j = 1; j <= n; j++) 32 ap[i][j]=(i==j?0:INF); 33 memset(ap, 0, sizeof(ap)); 34 string mon; int k = 1; 35 for(int i = 0; i < n; i++){ 36 cin >> mon; 37 mmap[mon] = k++; 38 } 39 int m; 40 scanf("%d", &m); 41 for(int i = 0; i < m; i++){ 42 string a, b; double oo; 43 cin >>a >> oo >> b; 44 ap[mmap[a]][mmap[b]]=oo; 45 if(ap[mmap[a]][mmap[b]] > oo) 46 ap[mmap[a]][mmap[b]]=ap[mmap[b]][mmap[a]]=oo; 47 } 48 if(Floyd()) 49 printf("Case %d: Yes\n", Q++); 50 else 51 printf("Case %d: No\n", Q++); 52 } 53 return 0; 54 }
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4728225.html