标签:des style blog http io color ar os sp
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16175 | Accepted: 6803 |
Description
Input
Output
Sample Input
3 USDollar BritishPound FrenchFranc 3 USDollar 0.5 BritishPound BritishPound 10.0 FrenchFranc FrenchFranc 0.21 USDollar 3 USDollar BritishPound FrenchFranc 6 USDollar 0.5 BritishPound USDollar 4.9 FrenchFranc BritishPound 10.0 FrenchFranc BritishPound 1.99 USDollar FrenchFranc 0.09 BritishPound FrenchFranc 0.19 USDollar 0
Sample Output
Case 1: Yes Case 2: No
Source
将字符串用map处理一下。
然后用spfa判一下环就可以了~
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <vector> #include <queue> #include <map> #include <set> #include <stack> #include <algorithm> using namespace std; typedef long long LL; const int N = 110; const int inf = 1e7+7; const double PI = acos(-1.0); const double eps = 1e-6 ; int n , m ; struct node { int v ; double w ; node(){} node( int a , double b ) { v = a , w = b ; } }; vector<node>g[N]; map<string,int>mp; string str1 , str2 ; bool vis[N] , inq[N] ; double dis[N]; int in_cnt[N]; bool spfa( int s ) { queue<int>que; for( int i = 0 ; i <= n ; ++i ) dis[i] = 0.0 , in_cnt[i] = 0 , inq[i] = false; dis[s] = 1.0 ; inq[s] = true ; que.push(s); while( !que.empty() ) { int u = que.front() ; que.pop(); inq[u] = false , vis[u] = true ; for( int i = 0 ; i < g[u].size() ; ++i ) { int v = g[u][i].v ; double w = g[u][i].w ; if( dis[u] * w > dis[v] ) { dis[v] = dis[u] * w ; if( !inq[v] ) { in_cnt[v] ++ ; if( in_cnt[v] >= n ) return false; inq[v] = true; que.push(v); } } } } return true; } void run() { double w ; mp.clear(); for( int i = 0 ; i <= n ; ++i )g[i].clear(); for( int i = 1 ; i <= n ; ++i ){ cin >> str1 , mp[str1] = i ; } cin >> m ; while( m-- ) { cin >> str1 >> w >> str2 ; int u = mp[str1] , v = mp[str2] ; g[u].push_back( node(v,w) ); } memset(vis,false,sizeof vis); bool tag = false ; for( int i = 1 ; i <= n ; ++i ) if( !vis[i] ) { if( !spfa(i) ) { tag = true ; break ; } } if( tag ) cout << "Yes" <<endl; else cout << "No" << endl; } int main() { #ifdef LOCAL freopen("in.txt","r",stdin); #endif // LOCAL ios::sync_with_stdio(false); int cas = 1 ; while( cin >> n && n ){ cout << "Case "<<cas++ <<": ";run(); } }
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/hlmark/p/4099219.html