HDU1217(floyd算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217

 

package D0801;
/*
 * 题目大意:就是套汇率,如果最后套到的钱大于原来的钱就yes否则no
 * 思路:floyd
 * 
 * */
import java.util.*;

public class HDU1217 {

	static List<String> list;
	static int s, n;
	static double[][] map;
	static double[][] dist;
	static int[][] pre;

	public static boolean floyd() {
		/*for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				dist[i][j] = map[i][j];
				pre[i][j] = i;
			}
		}*/
		for (int k = 0; k < n; k++) {
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					/*if (dist[i][k] != Double.MAX_VALUE
							&& dist[k][j] != Double.MAX_VALUE
							&& dist[i][j]<dist[i][k]*dist[k][j])*/
					if(dist[i][j]<dist[i][k]*dist[k][j])
						dist[i][j]=dist[i][k]*dist[k][j];
					if(i==j&&dist[i][j]>1)return true;
				}
			}
		}
		/*for (int i = 0; i < n; i++) {
			if (dist[i][i] > 1)
				return true;
		}*/
		return false;
	}

	public static void main(String[] args) {
		int m;
		Scanner sc = new Scanner(System.in);
		int cases = 0;
		String[] s;
		while (sc.hasNext()) {
			n = sc.nextInt();
			if (n == 0)
				break;
			s = new String[n];
			for (int i = 0; i < n; i++) {
				s[i] = sc.next();
			}
			// 初始化
			// System.out.println(n);
			/*map = new double[n][n];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					map[i][j] = 0;
				}
			}*/
			dist = new double[n][n];
			pre = new int[n][n];
			list = new ArrayList<String>();
			m = sc.nextInt();
			while (m-- > 0) {
				String s1 = sc.next();
				double rate = sc.nextDouble();
				String s2 = sc.next();
				int i, j;
				//处理字符串得到索引值
				for (i = 0; i < n; i++) {
					if (s1.equals(s[i]))
						break;
				}
				for (j = 0; j < n; j++) {
					if (s2.equals(s[j]))
						break;
				}
				//map[i][j]=rate;
				dist[i][j]=rate;

			}
			String result = null;
			if (floyd())
				result = "Yes";
			else
				result = "No";
			System.out.println("Case " + (++cases) + ": " + result);
		}
	}
}


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怎么演

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值