一个关于套利的题,就是判断是否有正环,我这里是用的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. kafka partition(分区)与 group(转)

    原文  https://www.cnblogs.com/liuwei6/p/6900686.html 一. 1.原理图 2.原理描述 一个topic 可以配置几个partition,produce发送 ...

  2. Python循环语句之break与continue的用法

    摘自原文章: http://www.jb51.net/article/73383.htm Python break 语句Python break语句,就像在C语言中,打破了最小封闭for或while循 ...

  3. 杂谈1.py

    Python命名规则: 1. 组成:数字/字母/下划线 只能以字母,下划线开头 不能包含空格 避免Python关键字和函数名 简短且具有描述性 描述数据形态及支持操作 Python动态类型 变量无类型 ...

  4. day38数据库MySQL基础

    数据库相关基础1 数据库介绍 1.数据库相关概念  数据库服务器(本质就是一个台计算机,该计算机之上安装有数据库管理软件的服务端)  数据库管理管理系统RDBMS(本质就是一个C/S架构的套接字软件) ...

  5. 2.pandas数据清洗

    pandas是用于数据清洗的库,安装配置pandas需要配置许多依赖的库,而且安装十分麻烦. 解决方法:可以用Anaconda为开发环境,Anaconda内置了许多有关数据清洗和算法的库. 1.安装p ...

  6. mac os High Sierra 升级错误

    升级mac OS High Sierra错误 已经成功从10.10升级到10.12.8 mac OS  Sierra了.就是升级到10.13报错. you may not install to thi ...

  7. SSM商城项目(四)

    1. 学习计划 1.图片服务器 2.图片服务器安装 3.图片服务器的使用 4.图片上传功能 5.富文本编辑器的使用方法 6.商品添加功能实现 2. 图片服务器 1.存储空间可扩展. 2.提供一个统一的 ...

  8. Windows Server 2008中使用计划任务定时执行BAT bat进行PHP脚本的执行

    Windows Server 2008中使用计划任务定时执行BAT bat进行PHP脚本的执行 2016年01月03日 17:36:00 持之以恒 阅读数:5520 标签: windows定时任务.b ...

  9. zend studio mac

    zend studio mac是一款PHP语言集成开发环境(IDE),专为开发人员研发,它包含了所有组件的开发为完整的PHP应用程序生命周期提供条件.zend studio是很多开发人员.程序员等专业 ...

  10. ppt复制文本框文字到word的方法

    打开ppt按Alt+F11,插入--模块,  选中“工具”--“引用”--MicroSoft Word .. 复制代码: Sub Main() On Error Resume Next Dim tem ...