Arbitrage--POJ 2240

1、题目类型:图论、最短路径、Floyd算法。

2、解题思路:Floyd算法的简单应用;STL中map的简单应用。

3、注意事项:避免map报警告,文件开始调用#pragma warning(disable:4786)。

4、实现方法:

  
    
#pragma warning(disable:4786)
#include
< iostream >
#include
< map >
#include
< string >
using namespace std;

double Map[ 30 ][ 30 ];
int n;
map
< string , int > node;

void Floyed()
{
int i,j,k;
for (k = 0 ;k < n;k ++ )
for (i = 0 ;i < n;i ++ )
for (j = 0 ;j < n;j ++ )
{
if ( Map[i][j] < Map[i][k] * Map[k][j] )
Map[i][j]
= Map[i][k] * Map[k][j];
}
}

int main()
{
int i,j = 0 ,m;
double k;
bool flag;
string tmp,tmp1;
while (cin >> n && n)
{
j
++ ;
flag
= 0 ;
memset(Map,
0 , sizeof (Map));
for (i = 0 ;i < n;i ++ )
{
Map[i][i]
= 1.0 ;
cin
>> tmp;
node[tmp]
= i;
}
cin
>> m;
for (i = 0 ;i < m;i ++ )
{
cin
>> tmp >> k >> tmp1;
Map[node[tmp]][node[tmp1]]
= k;
}
Floyed();
for (i = 0 ;i < n;i ++ )
{
if (Map[i][i] > 1.0 )
{
flag
= 1 ;
break ;
}
}
cout
<< " Case " << j << " : " ;
if (flag)
cout
<< " Yes " << endl;
else
cout
<< " No " << endl;
}
return 0 ;
}

 

你可能感兴趣的:(poj)