题意:

给定一系列货币汇率,求能否从一种货币,经过一系列转换,最终转化回更高价值的该种货币。

分析:

即为求最长路并判断是否存在“正”权值回路,这里用的bellmanford算法。

代码:

#include<iostream>
#include<cstring>
#include<map>
using namespace std;
const int maxn = 50, maxm =1005;
int n, m, flag = 0, cnt = 1;
map<string, int>mp;
struct edge{
int from, to;
double w;
}e[maxm];
double d[maxn];
int find_loop(int v)
{
memset(d,0,sizeof(d));
d[v] = 1;
for(int i = 0; i < n; i ++){
for(int j = 0; j < m; j++){
if(d[e[j].to] < d[e[j].from] * e[j].w){
d[e[j].to] = d[e[j].from] * e[j].w;
if(i==n-1) return 1;
}
}
}
return 0;
}
int main (void)
{
while(cin>>n&&n){
flag = 0;
string ci, cj;
for(int i = 0; i < n; i ++) {cin>>ci;mp[ci] = i;}
cin>>m;
for(int i = 0; i < m ; i ++){
cin>>ci>>e[i].w >>cj;
e[i].from = mp[ci];
e[i].to = mp[cj];
}
for(int i = 0; i < n; i++){
flag = find_loop(i);
if(flag) break;
}
if(flag) cout<<"Case "<<cnt++<<": Yes"<<endl;
else cout<<"Case "<<cnt++<<": No"<<endl;
}
return 0;
}

POJ 2240_Arbitrage的更多相关文章

  1. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  2. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  3. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  4. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  5. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  6. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  7. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  8. POJ 2752 Seek the Name, Seek the Fame [kmp]

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17898   Ac ...

  9. poj 2352 Stars 数星星 详解

    题目: poj 2352 Stars 数星星 题意:已知n个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时 ...

随机推荐

  1. (2)《Head First HTML与CSS》学习笔记---img与基于标准的HTML5

    1.浏览器处理图像的过程: 1.服务器获取文件,显示出文本结构,以及预留默认的大小给<img>(如果该<img>有width-1值和height-1值,则根据这个值提前设好页面 ...

  2. struts2 <allowed-methods > 标签配置

    1.在struts2   2.5版本中添加了对方法访问的权限,如果没有被添加到<allow-method> 方法的标签,将会报一下错误 5:05:18.078 [http-apr-8020 ...

  3. 【PostgreSQL-9.6.3】进程及体系结构

    本文主要讲述了PG的几个主要进程,以及PG的核心架构.进程和体系结构详见下图: 从上面的体系结构图可以看出来,PG使用经典的C/S架构,进程架构.在服务器端有主进程.服务进程.子进程.共享内存以及文件 ...

  4. EcliplseJPA2.1和glassfish3.1兼容问题

    之前一个项目,持久层用eclipseJpa2.1实现,web服务器用的是glassfish3.1. 部署完成后测试的时候出现bug,反反复复折腾了n次,最终确认是版本兼容的问题. 或者用glassfi ...

  5. JDK1.8中的Stream详解

    Stream简介 Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念.它也不同于 StAX 对 XML ...

  6. bootparam - 介绍Linux核心的启动参数

    描叙 Linux 核心在启动的时候可以接受指定的"命令行参数"或"启动参数".在通常情况下,由于核心有可能无法识别某些硬件,或可能将某些硬件识别为不正确的配置, ...

  7. JS日期,金钱处理

    一丶获取两个时间的天数 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...

  8. python __future__ 使用

    在开头加上from __future__ import print_function这句之后,即使在python2.X,使用print就得像python3.X那样加括号使用.python2.X中pri ...

  9. IDEA、Eclipse快捷键对比

    IDEA.Eclipse快捷键对比 序号 功能 IDEA Eclipse 1 很多功能:导入包,处理异常,强转cast Alt+Enter   2 导入包,自动修正??? Ctrl+Enter   3 ...

  10. How To: IDENTIFY THE ASM DEVICE FROM ASMLIB

    使用oracleasm querydisk可以查询到device的major和minor,从而对应. for i in `oracleasm listdisks` do oracleasm query ...