一个关于套利的题,就是判断是否有正环,我这里是用的SPFA,只要判断出来一种货币初始为1,最后变得大于1就代表是正环,要注意一下最后对vector的清空,当时从1开始清空,导致wa了两次,找了半天,尽量不要出现小的错误还是很致命的

#include <iostream>
#include<string.h>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
#define MAX 99999999;
double dis[+];
double vis[+];
int time[+];
int n;
char name[][];
double fir;
typedef struct
{
int x;
double rate;
//double cost;
}point;
int judge_name( char* str)
{
for(int i=;i<n;i++)
{
if(strcmp(str,name[i])==)
return i;
}
}
vector<point> p[];
int Spfa(int start)
{
queue<int> Q;
memset(time,,sizeof(time));
memset(vis, , sizeof(vis));
memset(dis, , sizeof(dis));
dis[start] = 1.0;
vis[start] = true;
time[start]++;
Q.push(start);
while (!Q.empty()){
int temp = Q.front();
Q.pop();
vis[temp] = false;
for(int i=; i<p[temp].size(); i++)
{
int v=p[temp][i].x;
double w=p[temp][i].rate; if (dis[v] <dis[temp]*w)
{
dis[v] = dis[temp]*w;
if(dis[start]>1.0)
{
// cout<<start<<endl;
// cout<<dis[start]<<endl;;
return true;
}
if (!vis[v])
{
Q.push(v);
vis[v] = true;
}
}
}
}
return false;
}
int main()
{
int m,s;
int total=;
while(cin>>n,n)
{
for(int i=;i<n;i++)
cin>>name[i];
int m;
cin>>m;
char str1[],str2[];
double f;
point node;
for(int i=;i<m;i++)
{
cin>>str1>>f>>str2;
node.x=judge_name(str2);
node.rate=f;
p[judge_name(str1)].push_back(node);
}
int flag=;
for(int i=;i<n;i++)
{
if(Spfa(i))
{
flag=true;
break;
}
}
if(flag)
printf("Case %d: Yes\n",++total);
else
printf("Case %d: No\n",++total);
for(int i=;i<n;i++)
p[i].clear();
}
return ;
}

poj2240的更多相关文章

  1. POJ-2240 Arbitrage---判断正环+枚举

    题目链接: https://vjudge.net/problem/POJ-2240 题目大意: 已知n种货币,以及m种货币汇率及方式,问能否通过货币转换,使得财富增加. 思路: 由于这里问的是财富有没 ...

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

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

  3. 用SPFA 解决POJ2240

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30790   Accepted: 12761 Descr ...

  4. POJ-2240(floyd算法简单应用)

    Arbitrage poj-2240 #include<iostream> #include<cstdio> #include<cstring> #include& ...

  5. poj2240 floyd

    //Accepted 732 KB 782 ms //floyd应用 #include <cstdio> #include <cstring> #include <ios ...

  6. poj2240最短路 floyd

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17360   Accepted: 7308 Descri ...

  7. POJ2240 Arbitrage(Floyd判负环)

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

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

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

  9. poj2240 最短路判环

    题意:与poj1680一样,有不同的换钱渠道,可以完成特定两种货币的交换,并且有汇率,只不过此题是单向边,然后问是否能使财富增加 与poj1680一样,建图之后直接spfa判增值的环即可 #inclu ...

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

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

随机推荐

  1. maven打包时报错:-source 1.5 中不支持 diamond 运算符

    报错现象: 解决方法: 在pom文件中加入下面依赖 <build> <plugins> <plugin> <groupId>org.apache.mav ...

  2. 学习笔记之Machine Learning Crash Course | Google Developers

    Machine Learning Crash Course  |  Google Developers https://developers.google.com/machine-learning/c ...

  3. 知识点:图说 Mysql 权限管理

    图: #授权表 user #该表放行的权限,针对:所有数据,所有库下所有表,以及表下的所有字段 db #该表放行的权限,针对:某一数据库,该数据库下的所有表,以及表下的所有字段 tables_priv ...

  4. 查询linux计算机的出口ip

    执行以下命令即可: [root@tkafka ~]# curl http://members.3322.org/dyndns/getip 123.103.9.7 碰到的场景: 微信公众号需要配置ip白 ...

  5. 第19课 类型萃取(3)_类型选择的traits

    1. std::conditional (1)原型:template <bool Cond, class T, class F> struct conditional; //根据条件获取T ...

  6. H5兼容ie做法

    IE浏览器支持新的标签:可以在使用新标签之前用javascript创建一个虚假的标签,这样就可以识别了.如<article>标签,在使用前,创建一个虚假的<article>标签 ...

  7. 这可能是目前最新的 Vue 相关开源项目库汇总(转)

    访问地址:https://juejin.im/entry/58bf745fa22b9d0058895a58 原文链接:https://github.com/opendigg/awesome-githu ...

  8. Python:笔记1_字符串处理【转载】

    [转载自:https://www.cnblogs.com/houht/p/3308634.html] 1. 判断字符串str是否为空Approach 1:如果字符串长度为0,说明字符串为空,code如 ...

  9. 一个nginx 回源限速的bug处理过程记录

    一个生产环境,nginx占用cpu很高. top - :: up day, :, users, load average: 13.26, 13.20, 13.20 Tasks: total, runn ...

  10. C#面向对象架构总结

    优秀的程序从命名开始,命名规则1.类名方法名需要见名知意.2.类的字段统统首字母小写并且封装仅供本类使用,并提供属性供外界使用,属性名开头大写,如果名字与类名冲突,那么在前面加下划线. 面向对象的7大 ...