Arbitrage POJ - 2240
题目链接:https://vjudge.net/problem/POJ-2240
思路:判正环,Bellman-ford和SPFA,floyd都可以,有正环就可以套利。
这里用SPFA,就是个板子题吧,把松弛改成乘法操作就好了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std; typedef long long LL;
#define inf (1LL << 25)
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--) const int N = ;
map<string,int > si; //编号,方便建图
int head[N];
bool vis[N];
int tot[N];
double value[N];
int cnt;
int n; struct Edge{
int to;
double w;
int next;
}e[]; void add(int u,int v,double w){
e[cnt].to = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt++;
} bool SPFA(){ rep(i,,n) value[i] = ;
value[] = ; //随意选个点就行
rep(i,,n) vis[i] = false;
rep(i,,n) tot[i] = ;
vis[] = true;
queue<int> que;
que.push(); while(!que.empty()){
int u = que.front();
que.pop();
vis[u] = false; for(int o = head[u]; ~o; o = e[o].next){
int v = e[o].to;
double w = e[o].w; if(value[v] < value[u] * w){
value[v] = value[u] * w;
if(!vis[v]){
vis[v] = true;
que.push(v);
tot[v]++;
if(tot[v] > n - ) return true; //有正环
}
}
}
} return false; } int main(){ ios::sync_with_stdio(false);
cin.tie(); int tot = ;
while(cin >> n && n){ rep(i,,n) head[i] = -;
cnt = ;
si.clear();
string in;
rep(i,,n){
cin >> in;
si[in] = i;
} int m;
string u,v;
double w; cin >> m;
rep(i,,m){
cin >> u >> w >> v;
add(si[u],si[v],w);
}
cout << "Case " << ++tot << ": ";
if(SPFA()) cout << "Yes" << endl;
else cout << "No" << endl;
} getchar(); getchar();
return ;
}
Arbitrage POJ - 2240的更多相关文章
- Arbitrage - poj 2240 (Bellman-ford)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17374 Accepted: 7312 Description Ar ...
- 最短路(Floyd_Warshall) POJ 2240 Arbitrage
题目传送门 /* 最短路:Floyd模板题 只要把+改为*就ok了,热闹后判断d[i][i]是否大于1 文件输入的ONLINE_JUDGE少写了个_,WA了N遍:) */ #include <c ...
- POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)
POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...
- poj 2240 Arbitrage 题解
Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21300 Accepted: 9079 Descri ...
- poj 2240 Arbitrage (Floyd)
链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...
- POJ 2240 Arbitrage【Bellman_ford坑】
链接: http://poj.org/problem?id=2240 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 2240 && ZOJ 1082 Arbitrage 最短路,c++ stl pass g++ tle 难度:0
http://poj.org/problem?id=2240 用log化乘法为加法找正圈 c++ 110ms,g++tle #include <string> #include <m ...
- POJ 2240 Arbitrage(floyd)
http://poj.org/problem?id=2240 题意 : 好吧,又是一个换钱的题:套利是利用货币汇率的差异进行的货币转换,例如用1美元购买0.5英镑,1英镑可以购买10法郎,一法郎可以购 ...
- poj 2240 Arbitrage (最短路 bellman_ford)
题目:http://poj.org/problem?id=2240 题意:给定n个货币名称,给m个货币之间的汇率,求会不会增加 和1860差不多,求有没有正环 刚开始没对,不知道为什么用 double ...
随机推荐
- Html学习之四(页面布局)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- C++ class 内的 [] 重载示例。
#include <iostream> // overloading "operator [] " inside class ///////////////////// ...
- C++ 标准库 std::find 查找
参见:https://en.cppreference.com/w/cpp/algorithm/find 查找指定字符/数字等. #include <iostream> #include & ...
- Vue介绍(一)
官网:https://cn.vuejs.org/ Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.V ...
- 【day09】PHP
一.函数 1. 作用域(Scope) (1)局部变量:变量在声明的代码段中有效 a.动态变量 b.静态变量:static ,用在函数中,当调用函数后内存不释放,能存储变量的最后的值. (2)全局变量: ...
- 使用system V实现读者写者问题
#include <stdio.h> #include <sys/sem.h> #include <sys/ipc.h> #include <string.h ...
- Python基础-day02-3
循环 目标 程序的三大流程 while 循环基本使用 break 和 continue while 循环嵌套 01. 程序的三大流程 在程序开发中,一共有三种流程方式: 顺序 -- 从上向下,顺序执行 ...
- LeetCode 739:每日温度 Daily Temperatures
题目: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperature ...
- 读取指定页面中的超链接-Python 3.7
#!/usr/bin/env python#coding: utf-8from bs4 import BeautifulSoupimport urllibimport urllib.requestim ...
- docker命令之link
1.新建两台容器,第二台(busybox_2)link到第一台(busybox_1) [root@localhost ~]# docker run -d -it --name busybox_1 bu ...