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个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时 ...
随机推荐
- 关于C# DropDownList 动态加载数据笔记
今天在处理一个导游注册的页面,其中需要填写地址以及该地址下所有旅行社,地址区级以上都是用下拉列表实现,具体地址街道等手动填写.在填写区县之后,该区县下的所有旅行社也需要动态加载. 后台代码 DataT ...
- java获取公网ip以及物理地址和代理商
package ipconfig; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStr ...
- Java数组的交集、并集
// 求两个数组的交集 public static int[] SameOfTwoArrays(int[] arr1, int[] arr2) { // 新建一个空数组,用于存储交集,空数组长度应该为 ...
- 了解java内存回收机制-博客导读
此文作为读优质博客前的导读文 1.如何判断对象是否该回收 该对象是否被引用,是否处于不可达状态 2.对象的引用机制 强引用.软引用.弱引用.虚引用 3.垃圾回收机制如何回收.算法. 串行回收.并行回收 ...
- PAT甲级考前整理(2019年3月备考)之三,持续更新中.....
PAT甲级考前整理一:https://www.cnblogs.com/jlyg/p/7525244.html,主要讲了131题的易错题及坑点 PAT甲级考前整理二:https://www.cnblog ...
- 'NSUnknownKeyException' … setValue:forUndefinedKey:]: …not key value coding compliant
解决一个问题: 当我添加一个IBout, 报了如下错误 NSUnknownKeyException' … setValue:forUndefinedKey:]: …not key value codi ...
- ORM-PetaPoco
PetaPoco有以下特色:--------------------------20170715姜彦 微小,没有依赖项……单个的C#文件可以方便的添加到任何项目中. 工作于严格的没有装饰的Poco类, ...
- chpasswd - 成批更新用户的口令
总览 chpasswd [-e] 描述 chpasswd 从系统的标准输入读入用户的名称和口令,并利用这些信息来更新系统上已存在的用户的口令.在没有用 -e 这个开关选项的情况下,口令将按明文的形式接 ...
- autorun - 自动装载/卸载CDROMs并在装载后执行/path/to/cdrom/autorun
总览 autorun [-lmqv?V] [-a EXEC] [-c CDPLAYER] [-e STRING] [-i MILLISEC] [-n STRING] [-t STRING] [--au ...
- WPF小记 -- 使用Path自己画图标,点击命中(焦点)丢失问题
在Template中,Path外面的Grid需添加Background属性值.否则点击范围会受限制,例如:Click,在RadioButton的Height和With范围内点击,命中率<1. & ...