POJ 2240_Arbitrage
题意:
给定一系列货币汇率,求能否从一种货币,经过一系列转换,最终转化回更高价值的该种货币。
分析:
即为求最长路并判断是否存在“正”权值回路,这里用的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的更多相关文章
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- 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 ...
- poj 2352 Stars 数星星 详解
题目: poj 2352 Stars 数星星 题意:已知n个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时 ...
随机推荐
- Webform 内置对象 Response对象、Request对象,QueryString
Request对象:获取请求Request["key"]来获取传递过来的值 QueryString:地址栏数据传递 ?key=value&key=value注意事项:不需要 ...
- c++通过管道pipe获取cmd输出的字符
#include <stdio.h>#include<iostream>#include<string>using namespace std; // 描述:exe ...
- FlowNet: Learning Optical Flow with Convolutional Networks
作者:嫩芽33出处:http://www.cnblogs.com/nenya33/p/7122701.html 版权:本文版权归作者和博客园共有 转载:欢迎转载,但未经作者同意,必须保留此段声明:必须 ...
- PHP文件及目录考察点
文件读取/写入操作 fopen()函数 用来打开一个文件,打开时需要指定打开模式 打开模式 模式 |作用 --- |--- 'r' |只读方式打开,将文件指针指向文件头. 'r+' |读写方式打开,将 ...
- ZooKeeper系列(三)
前面虽然配置了集群模式的Zookeeper,但是为了方面学建议在伪分布式模式的Zookeeper学习Zookeeper的shell命令. 一.Zookeeper的四字命令 Zookeeper支持某些特 ...
- linux中查看文件指定行的数据
http://jingyan.baidu.com/article/15622f24125872fdfdbea560.html
- BZOJ4873 LuoguP3749 寿司餐厅
题面太长,请诸位自行品尝—>寿司餐厅 分析: 首先题目中给了限制条件,假如选了D(i,j)(i<j),那么也就选了D(i+1,j)和D(i,j-1)两个点. 于是我们一下就明白了,哦,最大 ...
- HNOI 2010 物品调度 并查集 置换
题意: 题意有点细,暂不概括.请仔细审题. 分析: 我们先要把c生成出来. 记得颜神讲这道题,首先表明,这道题有两个问题需要处理. 第一个是要先定位,第二个是要求最小移动步数. 定位时对于每一个物品i ...
- rownum导致sql不能进行谓词推入
背景 案件(13405_RI-再保结算查询导出太慢),造成性能慢的原因是执行以下sql时,每次执行平均需要消耗2秒, 画面上,点击一次导出按钮,就会发起数以百记的调用. SQL执行代码段 select ...
- Bash的循环结构(for和while)
在bash有三中类型的循环结构表达方法:for,while,until.这里介绍常用的两种:for和while. for bash的for循环表达式和python的for循环表达式风格很像: for ...