zoukankan      html  css  js  c++  java
  • UVA 436

    UVA 436 - Arbitrage (II)

    题目链接

    题意:给定一些国家货币的汇率。问是否能通过不断换货币使钱得到增长

    思路:floyd,完事后推断一下有没有连到自己能大于1的情况

    代码:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <string>
    #include <map>
    using namespace std;
    
    const int N = 35;
    
    int n;
    double g[N][N];
    map<string, int> hash;
    
    int main() {
    	int cas = 0;
    	while (~scanf("%d", &n) && n) {
    		string a, b;
    		hash.clear();
    		for (int i = 1; i <= n; i++) {
    			cin >> a;
    			hash[a] = i;
    		}
    		int m;
    		double f;
    		for (int i = 1; i <= n; i++) {
    			for (int j = 1; j <= n; j++) {
    				if (i == j) g[i][j] = 1.0;
    				else g[i][j] = 0;
    			}
    		}
    		scanf("%d", &m);
    		while (m--) {
    			cin >> a >> f >> b;
    			int u = hash[a], v = hash[b];
    			g[u][v] = f;
    		}
    		for (int k = 1; k <= n; k++) {
    			for (int i = 1; i <= n; i++) {
    				for (int j = 1; j <= n; j++)
    					g[i][j] = max(g[i][j], g[i][k] * g[k][j]);
    			}
    		}
    		printf("Case %d: ", ++cas);
    		int flag = 1;
    		for (int i = 1; i <= n; i++) {
    			if (g[i][i] > 1.0) {
    				printf("Yes
    ");
    				flag = 0;
    				break;
    			}
    		}
    		if (flag) printf("No
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    Thrift在微服务中的使用
    MySQL 必知必会
    IDEA 内使用 git
    分布式锁
    LeetCode 图
    LeetCode 位运算
    LeetCode 数组
    LeetCode 字符串
    LeetCode 哈希表
    LeetCode 栈和队列
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/7080169.html
Copyright © 2011-2022 走看看