题意:

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

分析:

即为求最长路并判断是否存在“正”权值回路,这里用的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. A8通用权限框架

  2. AJPFX:关于面向对象及java的一些机制的思考

    1.变量的作用域和生命周期什么关系? 2.java除了在类体和方法体(包括参数)中可以声明变量外,其他位置一定不可以声明变量吗?比如高级别的应用里是不是有例外呢? 3.java源文件中代码的组织方式一 ...

  3. Android学习笔记(九) SeekBar和RatingBar

    一.SeekBar的主要属性 -max -progress -secondaryProgress 二.onSeekBarChangeListener -onProgressChanged(SeekBa ...

  4. Windows bat 设置代理

    转自tt-0411 @echo off cls color 0A Echo The program is running... Echo Setting the ip and dns... netsh ...

  5. SpringBoot传参转换枚举

    有时候,我们传参的时候,希望使用枚举类来当作参数 public enum VipEnum { HUANG(1, "黄钻"), HONG(2, "红钻"); pr ...

  6. H5 canvas 之乱画

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 所有的工作目录 都要svn_开头,并且要进行svn同步,你能保证你不删除,你保证不了非你!

    所有的工作目录 都要svn_开头,并且要进行svn同步,你能保证你不删除,你保证不了非你! 血的代价啊~

  8. HDOJ 1846 Brave Game - 博弈入门

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1846 经典基础博弈,首先面对(m+1)的人一定会输,依次往后推即可: #include<iost ...

  9. 并不简单的Integer

    Integer是一个看着挺简单的,其实还是有点不一样,Integer是一个int的包装类,它是可以起到缓存作用的,在java基础里说过它的范围是(-128-127)在这个返回是有缓存的,不会创建新的I ...

  10. 性能测试培训day1

    测试本质: 1构造测试数据和期望结果 2执行 3验证 自动化测试: 写完代码,单元测试测代码逻辑,单元测试搞清楚代码逻辑就行了(白盒测试)先静态,运行前用工具扫描BUG例如(a==11写成a=11), ...