floyd

Posted mekakucityactor

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了floyd相关的知识,希望对你有一定的参考价值。

floyd可用于解决某些可传递性问题(如1<2,2<3,则1<3 )

POJ2240:http://poj.org/problem?id=2240

Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 28138   Accepted: 11789

Description

Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5 percent. 

Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not. 

Input

The input will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible. 
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.

Output

For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes" respectively "Case case: No".

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
题意:给出一堆货币汇率,判断是否可以通过一些交换赚取差价。
题解:货币的交换过程属于乘法运算,具有传递性,可以通过floyd得到所有交换情景,判断dist[i][i]与1的大小关系即可。
POJ3660:http://poj.org/problem?id=3660
Cow Contest

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16083   Accepted: 8997

Description


N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors.

The contest is conducted in several head-to-head rounds, each between two cows. If cow A has a greater skill level than cow B (1 ≤ A ≤ N; 1 ≤ B ≤ NA ≠ B), then cow A will always beat cow B.

Farmer John is trying to rank the cows by skill level. Given a list the results of M (1 ≤ M ≤ 4,500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory.


Input


* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and B


Output


* Line 1: A single integer representing the number of cows whose ranks can be determined
 


Sample Input


5 5
4 3
4 2
3 2
1 2
2 5

Sample Output


2
题意:给出一些数据的大小关系,判断由多少个数据可以被确定具体排名
题解:易知如果知道一个数据与另外n-1个数的关系,就可以确定它的排名,由于大小关系具有可传递性,所以可以使用floyd得到所有数之间的关系统计能与另外n-1个数确定的个数即可

以上是关于floyd的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Floyd-Warshall 算法中输出最短路径?

图论模型--dijstra算法和floyd算法

Floyd算法(最短路)

Floyd算法的原理和实现代码

floyd算法

java实现floyd统计天津地铁的网站距离