(floyd 1.1)hdu 1217 Arbitrage(使用floyd来求最长路——判断是否存在一种货币,经过一个兑换回路以后>=1单元)
2015-03-27 12:14
495 查看
题目:
Total Submission(s): 5052 Accepted Submission(s): 2318
[align=left]Problem Description[/align]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.
[align=left]Input[/align]The input file 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.
[align=left]Output[/align]For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes" respectively "Case case: No".
[align=left]Sample Input[/align]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
[align=left]Sample Output[/align]Case 1: Yes
Case 2: No
[align=left]Source[/align]University of Ulm Local Contest 1996
[align=left]Recommend[/align]Eddy | We have carefully selected several similar problems for you: 1142 1385 2112 1598 2923
题目分析:
使用floyd来求最长路。这道题需要注意以下的一些地方。
1)文字索引需要转化成数字索引。因为数组里面是数字索引。
string huobi_name;
int i;
for(i = 0 ; i < n ; ++i){//***将文字索引转换成数字索引.因为数组中用的是数字索引
cin >> huobi_name;
money[huobi_name] = huobi_type++;
}
2)求最长路时的floyd的初始化方法的写法。
/**
* 这道题中floyd的初始化.
* 以前的写法如上提所示.
* 这里讲一下区别。以前是求最短路径,是一个由大到小的紧缩过程。
* 而这道题抽象以下其实是求最长路径,是一个由小到大的放大的过程
*/
void initial() {
int i, j;
for (i = 1; i <= n; ++i) {
for (j = 1; j <= n; ++j) {
e[i][j] = 0;//所有东西一开始都初始化位0
}
}
}
3)求最长路时floyd函数的放缩的写法。其实就是把 > 换成 < 即可。
/**
*floyd算法
*/
void floyd() {
int i, j, k;
for (k = 1; k <= n; ++k) {//遍历所有的中间点
for (i = 1; i <= n; ++i) {//遍历所有的起点
for (j = 1; j <= n; ++j) {//遍历所有的终点
//*****floyd求最长路的写法
if (e[i][j] < e[i][k] * e[k][j]) {//如果当前i-->j的距离 小于 i-->k--->j的距离之和
e[i][j] = e[i][k] * e[k][j];//更新从i--->j的最短路径
}
}
}
}
}
这道题的代码如下:
/*
* hdu1217.cpp
*
* Created on: 2015年3月27日
* Author: Administrator
*/
#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
const int maxn = 31;
double e[maxn][maxn];
int n;
const int inf = 99999999;
/*
* void initial() {
int i, j;
for (i = 1; i <= n; ++i) {
for (j = 1; j <= n; ++j) {
if (i == j) {
e[i][j] = 0;
} else {
e[i][j] = inf;
}
}
}
}
*
*/
/**
* 这道题中floyd的初始化.
* 以前的写法如上提所示.
* 这里讲一下区别。以前是求最短路径,是一个由大到小的紧缩过程。
* 而这道题抽象以下其实是求最长路径,是一个由小到大的放大的过程
*/
void initial() {
int i, j;
for (i = 1; i <= n; ++i) {
for (j = 1; j <= n; ++j) {
e[i][j] = 0;//所有东西一开始都初始化位0
}
}
}
/**
*floyd算法
*/
void floyd() {
int i, j, k;
for (k = 1; k <= n; ++k) {//遍历所有的中间点
for (i = 1; i <= n; ++i) {//遍历所有的起点
for (j = 1; j <= n; ++j) {//遍历所有的终点
//*****floyd求最长路的写法
if (e[i][j] < e[i][k] * e[k][j]) {//如果当前i-->j的距离 小于 i-->k--->j的距离之和
e[i][j] = e[i][k] * e[k][j];//更新从i--->j的最短路径
}
}
}
}
}
int main(){
int cnt = 1;
while(scanf("%d",&n)!=EOF,n){
map<string,int> money;
int huobi_type = 1;
string huobi_name;
int i;
for(i = 0 ; i < n ; ++i){//***将文字索引转换成数字索引.因为数组中用的是数字索引
cin >> huobi_name;
money[huobi_name] = huobi_type++;
}
initial();
int m;
scanf("%d",&m);
while(m--){
string from;
string to;
double change;
cin >> from >> change >> to;
e[money[from]][money[to]] = change;
}
floyd();
bool flag = false;
for(i = 1 ; i <= n ; ++i){//遍历所有的货币
if(e[i][i] > 1){//看是否存在一种货币经过一个兑换回路以后 >= 1个单元
//如果存在
flag = true;//则将flag标记为true。
break;//跳出循环
}
}
//输出最后的结果
if(flag == true){
printf("Case %d: Yes\n",cnt++);
}else{
printf("Case %d: No\n",cnt++);
}
}
return 0;
}
Arbitrage
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5052 Accepted Submission(s): 2318
[align=left]Problem Description[/align]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.
[align=left]Input[/align]The input file 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.
[align=left]Output[/align]For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes" respectively "Case case: No".
[align=left]Sample Input[/align]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
[align=left]Sample Output[/align]Case 1: Yes
Case 2: No
[align=left]Source[/align]University of Ulm Local Contest 1996
[align=left]Recommend[/align]Eddy | We have carefully selected several similar problems for you: 1142 1385 2112 1598 2923
题目分析:
使用floyd来求最长路。这道题需要注意以下的一些地方。
1)文字索引需要转化成数字索引。因为数组里面是数字索引。
string huobi_name;
int i;
for(i = 0 ; i < n ; ++i){//***将文字索引转换成数字索引.因为数组中用的是数字索引
cin >> huobi_name;
money[huobi_name] = huobi_type++;
}
2)求最长路时的floyd的初始化方法的写法。
/**
* 这道题中floyd的初始化.
* 以前的写法如上提所示.
* 这里讲一下区别。以前是求最短路径,是一个由大到小的紧缩过程。
* 而这道题抽象以下其实是求最长路径,是一个由小到大的放大的过程
*/
void initial() {
int i, j;
for (i = 1; i <= n; ++i) {
for (j = 1; j <= n; ++j) {
e[i][j] = 0;//所有东西一开始都初始化位0
}
}
}
3)求最长路时floyd函数的放缩的写法。其实就是把 > 换成 < 即可。
/**
*floyd算法
*/
void floyd() {
int i, j, k;
for (k = 1; k <= n; ++k) {//遍历所有的中间点
for (i = 1; i <= n; ++i) {//遍历所有的起点
for (j = 1; j <= n; ++j) {//遍历所有的终点
//*****floyd求最长路的写法
if (e[i][j] < e[i][k] * e[k][j]) {//如果当前i-->j的距离 小于 i-->k--->j的距离之和
e[i][j] = e[i][k] * e[k][j];//更新从i--->j的最短路径
}
}
}
}
}
这道题的代码如下:
/*
* hdu1217.cpp
*
* Created on: 2015年3月27日
* Author: Administrator
*/
#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
const int maxn = 31;
double e[maxn][maxn];
int n;
const int inf = 99999999;
/*
* void initial() {
int i, j;
for (i = 1; i <= n; ++i) {
for (j = 1; j <= n; ++j) {
if (i == j) {
e[i][j] = 0;
} else {
e[i][j] = inf;
}
}
}
}
*
*/
/**
* 这道题中floyd的初始化.
* 以前的写法如上提所示.
* 这里讲一下区别。以前是求最短路径,是一个由大到小的紧缩过程。
* 而这道题抽象以下其实是求最长路径,是一个由小到大的放大的过程
*/
void initial() {
int i, j;
for (i = 1; i <= n; ++i) {
for (j = 1; j <= n; ++j) {
e[i][j] = 0;//所有东西一开始都初始化位0
}
}
}
/**
*floyd算法
*/
void floyd() {
int i, j, k;
for (k = 1; k <= n; ++k) {//遍历所有的中间点
for (i = 1; i <= n; ++i) {//遍历所有的起点
for (j = 1; j <= n; ++j) {//遍历所有的终点
//*****floyd求最长路的写法
if (e[i][j] < e[i][k] * e[k][j]) {//如果当前i-->j的距离 小于 i-->k--->j的距离之和
e[i][j] = e[i][k] * e[k][j];//更新从i--->j的最短路径
}
}
}
}
}
int main(){
int cnt = 1;
while(scanf("%d",&n)!=EOF,n){
map<string,int> money;
int huobi_type = 1;
string huobi_name;
int i;
for(i = 0 ; i < n ; ++i){//***将文字索引转换成数字索引.因为数组中用的是数字索引
cin >> huobi_name;
money[huobi_name] = huobi_type++;
}
initial();
int m;
scanf("%d",&m);
while(m--){
string from;
string to;
double change;
cin >> from >> change >> to;
e[money[from]][money[to]] = change;
}
floyd();
bool flag = false;
for(i = 1 ; i <= n ; ++i){//遍历所有的货币
if(e[i][i] > 1){//看是否存在一种货币经过一个兑换回路以后 >= 1个单元
//如果存在
flag = true;//则将flag标记为true。
break;//跳出循环
}
}
//输出最后的结果
if(flag == true){
printf("Case %d: Yes\n",cnt++);
}else{
printf("Case %d: No\n",cnt++);
}
}
return 0;
}
相关文章推荐
- 对于一个整数矩阵,存在一种运算,对矩阵中任意元素加一时,需要其相邻(上下左右)某一个元素也加一, 现给出一正数矩阵,判断其是否能够由一个全零矩阵经过上述运算得到。
- 对于一个整数矩阵,存在一种运算,对矩阵中任意元素加一时,需要其相邻(上下左右)某一个元素也加一, 现给出一正数矩阵,判断其是否能够由一个全零矩阵经过上述运算得到。
- 对于一个整数矩阵,存在一种运算,对矩阵中任意元素加一时,需要其相邻(上下左右)某一个元素也加一, 现给出一正数矩阵,判断其是否能够由一个全零矩阵经过上述运算得到。
- 对于一个整数矩阵,存在一种运算,对矩阵中任意元素加一时,需要其相邻(上下左右)某一个元素也加一,现给出一正数矩阵,判断其是否能够由一个全零矩阵经过上述运算得到
- 对于一个整数矩阵,存在一种运算,对矩阵中任意元素加一时,需要其相邻(上下左右)某一个元素也加一, 现给出一正数矩阵,判断其是否能够由一个全零矩阵经过上述运算得到。
- [ACM] hdu 1217 Arbitrage (bellman_ford最短路,判断是否有正权回路或Floyed)
- 对于一个整数矩阵,存在一种运算,对矩阵中任意元素加一时,需要其相邻(上下左右),某一个元素也加一,现给出一正数矩阵,判断其是否能够由一个全零矩阵经过上述运算得到。
- 对于一个整数矩阵,存在一种运算,对矩阵中任意元素加一时,需要其相邻(上下左右),某一个元素也加一,现给出一正数矩阵,判断其是否能够由一个全零矩阵经过上述运算得到。
- 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bccced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中
- 判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进
- 给出一个2D板和一个单词,找出这个单词是否存在于网格中。 该单词可以由顺序相邻单元的字母构成,其中“相邻”单元是水平或垂直相邻的单元。 相同的字母单元可能不会被多次使用。
- 当获取相似数据时,使用不同方法调用不同sp,但是使用同一个方法去用IIDataReader或者SqlDataReader读取数据时需要判断column name是否存在。
- [ACM] hdu 1217 Arbitrage (bellman_ford最短路,推断是否有正权回路或Floyed)
- POJ 1860(二次用floyd, 判断每一个点 ,是否存在正权回路)
- 如何判断一个图中是否存在回路
- 微软面试(or电面)试题——如何判断一个单链表是否存在回路
- 交互式让用户输入一个文件路径,判断文件是否存在,若存在判断文件类型,若文件是普通文件,不能使用wc。
- 算法导论 习题 22.4-3 判断一个无向图是否存在回路
- 如何使用jQuery判断一个元素是否存在
- hdu 2647(拓扑排序,判断有向图是否存在回路及自环,并统计每个顶点的前驱点数)