Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:只能用Floyd,不能用bellman么?In Reply To:只能用Floyd,不能用bellman么? Posted by:threetree at 2008-08-28 15:57:08 都可以用,Bellman-Ford也可以,类似与求最长路径: 附上我的。。。 //要注意同一种货币可能汇率不同的情况 // 如: // 1 // a // 1 // a 2.0 a // 输出应该是Yes。。。 #include <iostream> #include <cstdio> using namespace std; const int MAX = 30; char currency[MAX][100]; struct Currency { int from; int to; double rate; }cur[MAX * MAX]; double maxcur[MAX]; int n, m; void init() { int i, j; for(i = 0; i < n; i++) maxcur[i] = 0; } bool Bellman_Ford(int s) { int i, k; maxcur[s] = 1.0; if(n == 1) { for(i = 0; i < m; i++) if(maxcur[cur[0].to] < maxcur[cur[0].from] * cur[0].rate) maxcur[cur[0].to] = maxcur[cur[0].from] * cur[0].rate; } else { for(k = 1; k < n; k++) { for(i = 0; i < m; i++) { if(maxcur[cur[i].to] < maxcur[cur[i].from] * cur[i].rate) maxcur[cur[i].to] = maxcur[cur[i].from] * cur[i].rate; } } } if(maxcur[s] > 1.0) return true; return false; } int main() { int i, j, k; int icou = 0; char tempf[100], tempt[100]; double ratetemp; while(1) { scanf("%d", &n); if(n == 0) break; icou++; for(i = 0; i < n; i++) { scanf("%s", currency[i]); } scanf("%d", &m); int flag = 0; for(j = 0; j < m; j++) { scanf("%s", tempf); scanf("%lf", &ratetemp); scanf("%s", tempt); flag = 0; cur[j].rate = ratetemp; //printf("%f--%f\n", ratetemp, cur[j].rate); for(i = 0; i < n; i++) { if(!strcmp(currency[i], tempf)) { cur[j].from = i; flag++; if(flag == 2) break; } if(!strcmp(currency[i], tempt)) { cur[j].to = i; flag++; if(flag == 2) break; } } } /*for(i = 0; i < m; i++) { printf("%d,%d : %lf\n", cur[i].from, cur[i].to, cur[i].rate); }*/ printf("Case %d: ", icou); bool is = false; for(i = 0; i < n; i++) { init(); if(Bellman_Ford(i)) { printf("Yes\n"); is = true; break; } } if(!is) printf("No\n"); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator