一个关于套利的题,就是判断是否有正环,我这里是用的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. Linux集群之keepalive+Nginx

    集群从功能实现上分高可用和负载均衡: 高可用集群,即“HA"集群,也常称作“双机热备”. 当提供服务的机器宕机,备胎将接替继续提供服务: 实现高可用的开源软件有:heartbeat.keep ...

  2. VMware Workstation 11 搭建windows server 2012 之sql server 2012集群常见问题整理

    1.windows server 2012内置支持iSCSI发起程序无需额外安装,iSCSI Software Target 可作为“文件和存储服务”角色下的内置功能使用 2.拷贝虚拟机的文件加入域时 ...

  3. 自己动手制作的淘宝闲鱼APP宝贝数据采集工具软件

    之前做过淘宝PC端宝贝和店铺数据的采集,后来需要做APP端的数据采集,因为没有学过Android,以前也都是做PC端的软件,有没有其他方法呢? 突然想到了用手机模拟器,可以在电脑端控制运行手机APP端 ...

  4. is,as,类库

    is和as运算符: 所有类型的基类 object类型 - 基类:所有类型的基类,就类似是整个生物圈的生物类,是个大的概念 object o1 = new Random(); //object可以承载R ...

  5. Struts2 xxAction-validation.xml使用

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC &quo ...

  6. mysql 拒绝登录解决

    一大早打开Navicat Lite for MySQL客户端,提示1045 access denied for user ’root’@’localhost’ using password yes,太 ...

  7. requests_html 使用

    安装 pip install requests-html #2种方式爬取 博客园from requests_html import HTMLSession session=HTMLSession() ...

  8. WPF 去掉Drag a column header here to group by that column

    <dxg:GridControl.View> <dxg:TableView x:Name="view" ShowGroupPanel="False&qu ...

  9. [持续交付实践] 开篇:持续集成&持续交付综述

    前言 随着微服务架构与容器虚拟化技术的发展,持续集成与持续交付的概念又重新回到了大家的视野,越来越多的公司开始使用持续集成的系统来解决频繁发布带来的质量问题:使用持续交付的工具来实现代码在不同环境上的 ...

  10. ROS--导航、路径规划和SLAM

    一.用move_base导航走正方形 1. roscore 2.执行 roslaunch rbx1_bringup fake_turtlebot.launch 然后 roslaunch rbx1_na ...