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个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时 ...
随机推荐
- 用jquery的.val() 给具有style="display:none;" 属性的标签写值的问题。
今天写项目, 碰到奇怪现象, 用jquery的val()函数怎么都无法给标签赋值,而我确定是否赋值是通过浏览器控制台来看的.其实这种方式不准确,因为具有 style="display:non ...
- 纯CSS写的对勾样式
& .cicle{ position: relative; float: right; margin-right: -1rem; ...
- 461在全志r16平台tinav3.0系统下使用地磁计QMC5883L
461在全志r16平台tinav3.0系统下使用地磁计QMC5883L 2018/9/7 14:08 版本:V1.0 开发板:SC3817R SDK:tina v3.0 (基本确认全志tina v3. ...
- java设计模式之代理模式 ,以及和java 回调机制的区别
java 代理模式就是: 将自己要做的事交给别人去做(这个别人就是代理者,自己就是被代理者),为什么自己能做的要交给别人去做了?假如一个小学生小明,现在要写作业,但是又想玩游戏,他更想玩游戏,并且不想 ...
- Elasticsearch の 查询类型
查询类型SearchType Es中一共有四种查询类型:QUERY_AND_FETCH.QUERY_THEN_FETCH.DFS_QUERY_AND_FETCH.DFS_QUERY_THEN_FETC ...
- vue项目打包步骤及运行打包项目
(1)项目打包 终端运行命令 npm run build 打包成功的标志与项目的改变,如下图: 点击index.html,通过浏览器运行,出现以下报错,如图: 那么应该如何修改呢? 具体步骤如下 ...
- vue-router 基本使用(vue工程化)
(1)概念: 路由,其实就是指向的意思,当我点击页面上的home按钮时,页面中就要显示home的内容,如果点击页面上的about 按钮,页面中就要显示about 的内容.Home按钮 => h ...
- spring springmvc 获取所有url
@Autowired private RequestMappingHandlerMapping handlerMapping; @Test public void getAllApi() { Map& ...
- fastclick.js插件使用
引入插件步骤 ①在HTML页面中添加 <script type='application/javascript' src='/path/to/fastclick.js'></scr ...
- HTML 之 DOM文件对象模型
文件对象模型 (DOM: Document Object Model) DOM 是 W3C定义的一种访问文档的标准. "The W3C Document Object Model (DOM) ...