思路:

有向图判负环。

实现:

(1)spfa

 #include <iostream>
#include <map>
#include <string>
#include <cmath>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
double G[MAXN][MAXN], d[MAXN];
bool in[MAXN];
int n, m, num[MAXN];
bool spfa(int s)
{
queue<int> q;
d[s] = ;
q.push(s);
in[s] = true;
while (!q.empty())
{
int tmp = q.front(); q.pop();
in[tmp] = false;
for (int i = ; i <= n; i++)
{
if (d[tmp] + G[tmp][i] < d[i])
{
d[i] = d[tmp] + G[tmp][i];
if (!in[i]) { in[i] = true; q.push(i); }
num[i]++;
if (num[i] > n) return true;
}
}
}
return false;
}
int main()
{
map<string, int> mp;
string s, t;
double x;
int Kase = ;
while (cin >> n, n)
{
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
G[i][j] = INF;
}
}
fill(d, d + n + , INF);
fill(in, in + n + , );
fill(num, num + n + , );
mp.clear();
for (int i = ; i <= n; i++)
{
cin >> s;
mp[s] = i;
}
cin >> m;
for (int i = ; i <= m; i++)
{
cin >> s >> x >> t;
G[mp[s]][mp[t]] = -log(x);
}
for (int i = ; i <= n; i++) G[][i] = ;
cout << "Case " << Kase++ << ": ";
if (spfa()) cout << "Yes" << endl;
else cout << "No" << endl;
}
return ;
}

(2)floyd

 #include <iostream>
#include <map>
#include <string>
#include <cmath>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
double G[MAXN][MAXN];
int n, m; int main()
{
map<string, int> mp;
string s, t;
double x;
int Kase = ;
while (cin >> n, n)
{
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
if (i == j) G[i][j] = ;
else G[i][j] = INF;
}
}
mp.clear();
for (int i = ; i <= n; i++)
{
cin >> s;
mp[s] = i;
}
cin >> m;
for (int i = ; i <= m; i++)
{
cin >> s >> x >> t;
G[mp[s]][mp[t]] = -log(x);
}
for (int k = ; k <= n; k++)
{
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
if (G[i][k] + G[k][j] < G[i][j])
G[i][j] = G[i][k] + G[k][j];
}
}
}
bool flg = false;
for (int i = ; i <= n; i++)
{
if (G[i][i] < ) { flg = true; break; }
}
cout << "Case " << Kase++ << ": ";
if (flg) cout << "Yes" << endl;
else cout << "No" << endl;
}
return ;
}

poj2240 Arbitrage的更多相关文章

  1. POJ-2240 Arbitrage BellmanFord查可循环圈

    题目链接:https://cn.vjudge.net/problem/POJ-2240 题意 套利(Arbitrage)就是通过不断兑换外币,使得自己钱变多的行为 给出一些汇率 问能不能套利 思路 马 ...

  2. poj-------(2240)Arbitrage(最短路)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15640   Accepted: 6563 Descri ...

  3. POJ2240——Arbitrage(Floyd算法变形)

    Arbitrage DescriptionArbitrage is the use of discrepancies in currency exchange rates to transform o ...

  4. POJ-2240 -Arbitrage(Bellman)

    题目链接:Arbitrage 让这题坑了,精度损失的厉害.用赋值的话.直接所有变成0.00了,无奈下,我仅仅好往里输了,和POJ1860一样找正环,代码也差点儿相同,略微改改就能够了,可是这个题精度损 ...

  5. POJ2240 Arbitrage(Floyd判负环)

    跑完Floyd后,d[u][u]就表示从u点出发可以经过所有n个点回到u点的最短路,因此只要根据数组对角线的信息就能判断是否存在负环. #include<cstdio> #include& ...

  6. POJ2240 Arbitrage(最短路)

    题目链接. 题意: 根据汇率可以将一种金币换成其他的金币,求最后能否赚到比原来更多的金币. 分析: 最短路的求法,用floyd. #include <iostream> #include ...

  7. poj2240 - Arbitrage(汇率问题,floyd)

    题目大意: 给你一个汇率图, 让你判断能否根据汇率盈利 #include <iostream> #include <cstdlib> #include <cstdio&g ...

  8. 一步一步深入理解Dijkstra算法

    先简单介绍一下最短路径: 最短路径是啥?就是一个带边值的图中从某一个顶点到另外一个顶点的最短路径. 官方定义:对于内网图而言,最短路径是指两顶点之间经过的边上权值之和最小的路径. 并且我们称路径上的第 ...

  9. poj图论解题报告索引

    最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...

随机推荐

  1. IDEA新手下载及和eclipse,myeclipse的区别

    一:下载安装包,我们可以去官网下载.下载地址:http://www.jetbrains.com/idea/download/#secation=windows 二:IDEA的安装: 1.下载安装包后双 ...

  2. 我的arcgis培训照片9

    来自:http://www.cioiot.com/successview-547-1.html

  3. linux下配置LAMP开发环境,以及经常使用小细节

    本来安装没什么可说到.可是在linux其中easy会出现各种各样到问题. 我安装以后导致各种问题 比方php无法正常解析,数据库无法关闭,Apache无法开启等等........ 所以搞得我比較郁闷, ...

  4. linux设备驱动归纳总结(八):2.match.probe.remove

    linux设备驱动归纳总结(八):2.总线.设备和驱动的关系 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  5. LINQ实现

    public static IEnumerable<TSource> MyWhere<TSource>(     this IEnumerable<TSource> ...

  6. [译]IOS中AutoLayout布局与Transform的冲突问题

    http://m.blog.csdn.net/blog/a345017062/43565279 原文链接见这里: http://stackoverflow.com/questions/12943107 ...

  7. (十七)LU分解

    #encoding=utf-8 import numpy as np # 输入数据 # a用来记录x的系数 a=[[2.0,2.0,3.0],[4.0,7.0,7.0],[-2.0,4.0,5.0]] ...

  8. JTabbedPane的LookAndFeel--TabbedPaneUI

    在定制JTabbedPane的时候是需要使用到LookAndFeel的,而使用LookAndFeel定制的时候,其实主要是继承BasicTabbedPaneUI. to be continue...

  9. 【UVa 10881】Piotr's Ants

    Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: ...

  10. AIZU AOJ 2309 Vector Compression 最小树形图(朱—刘算法)

    题意简述:给定若干个相同维度的向量,寻找一种排序方法,使得所有向量的表示长度总和最低. 所谓表示长度为(Aj-r*Ai)^2,其中i<j  数据范围:向量总数和维度均小于100 思路:(1)首先 ...