POJ 2240 Arbitrage 解题报告
Question Link
单向图,就是bellman-ford求正环,但是应该预设起点的钱的值为1,或者任意正数。起点暴力,枚举。好吧(其实我是真的傻了)。
傻傻的代码
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std;
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define mset(var,val) memset(var,val,sizeof(var))
#define LL long long
#define eps 0.000001
#define inf 0x7f7f7f7f
#define llinf 1e18
#define exp 0.000001
#define pai 3.141592654
#define random(x) rand()%(x)
#define lowbit(x) x&(-x)
inline int read()
{int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}while ( a>='0' && a<='9' ){ x=(x<<3)+(x<<1)+a-'0'; a=getchar();}return x*y;
}
#define M 1090
#define N 390
map<string,int >mapp;
int n,m,etot,Case;
struct node {int u,v; double w;
}e[M];
inline void addead(int u,int v,double w){++etot;e[etot].u = u; e[etot].v = v; e[etot].w = w;
}
void init(){etot = 0; mapp.clear();
}
double dis[N];
bool bellman(){for (int ai = 1; ai <= n; ai++){mset(dis,0);dis[ ai ] = 1; for (int ci = 1; ci <= n-1; ci++){for (int di = 1; di <= etot; di++){if ( dis[ e[di].v ] < dis[ e[di].u ] * e[ di ].w ){dis[ e[di].v ] = dis[ e[di].u ] * e[ di ].w;}} }for (int di = 1; di <= etot; di++){if ( dis[ e[di].v ] < dis[ e[di].u ]*e[di].w )return true; }}return false;
}
int main()
{// freopen("1.txt","r",stdin);srand((int)time(0));Case = 0; while ( scanf("%d",&n) != EOF && n ){init();rep(i,1,n){string a; cin>>a; mapp[ a ] = i; }scanf("%d",&m);rep(i,1,m){string a,b; double ww; cin>>a>>ww>>b; addead(mapp[a],mapp[b],ww);}if ( bellman( ) ){printf("Case %d: Yes\n",++Case);}else printf("Case %d: No\n",++Case);}return 0;
}
但是似乎不影响复杂度和结果。因为会return出去。
但还是体现我的对Bellman_Ford 检测负环回路的不理解。
改正的代码
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std;
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define mset(var,val) memset(var,val,sizeof(var))
#define LL long long
#define eps 0.000001
#define inf 0x7f7f7f7f
#define llinf 1e18
#define exp 0.000001
#define pai 3.141592654
#define random(x) rand()%(x)
#define lowbit(x) x&(-x)
inline int read()
{int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}while ( a>='0' && a<='9' ){ x=(x<<3)+(x<<1)+a-'0'; a=getchar();}return x*y;
}
#define M 1090
#define N 390
map<string,int >mapp;
int n,m,etot,Case;
struct node {int u,v; double w;
}e[M];
inline void addead(int u,int v,double w){++etot;e[etot].u = u; e[etot].v = v; e[etot].w = w;
}
void init(){etot = 0; mapp.clear();
}
double dis[N];
bool bellman(){mset(dis,0);dis[ 1 ] = 1; for (int ci = 1; ci <= n-1; ci++){for (int di = 1; di <= etot; di++){if ( dis[ e[di].v ] < dis[ e[di].u ] * e[ di ].w ){dis[ e[di].v ] = dis[ e[di].u ] * e[ di ].w;}} }for (int di = 1; di <= etot; di++){if ( dis[ e[di].v ] < dis[ e[di].u ]*e[di].w )return true; }return false;
}
int main()
{// freopen("1.txt","r",stdin);srand((int)time(0));Case = 0; while ( scanf("%d",&n) != EOF && n ){init();rep(i,1,n){string a; cin>>a; mapp[ a ] = i; }scanf("%d",&m);rep(i,1,m){string a,b; double ww; cin>>a>>ww>>b; addead(mapp[a],mapp[b],ww);}if ( bellman( ) ){printf("Case %d: Yes\n",++Case);}else printf("Case %d: No\n",++Case);}return 0;
}
Ford 解法
虽然没有bellman算法优
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std;
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define mset(var,val) memset(var,val,sizeof(var))
#define LL long long
#define eps 0.000001
#define inf 0x7f7f7f7f
#define llinf 1e18
#define exp 0.000001
#define pai 3.141592654
#define random(x) rand()%(x)
#define lowbit(x) x&(-x)
inline int read()
{int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}while ( a>='0' && a<='9' ){ x=(x<<3)+(x<<1)+a-'0'; a=getchar();}return x*y;
}
#define M 1090
#define N 390
map<string,int >mapp;
int n,m,etot,Case;
double e[N][N];
void init(){etot = 0; mset(e,0);rep(i,1,n) e[i][i] = 1; mapp.clear();
}
//double dis[N];
bool ford(){for (int k = 1; k <= n; k++){for (int i = 1; i <= n; i++){for (int j = 1; j <= n;j++){if ( e[i][j] < e[i][k]*e[k][j] ){e[i][j] = e[i][k]*e[k][j];}}}}for (int i = 1; i <= n; i++)if ( e[i][i] > 1 )return true; return false;
}
int main()
{// freopen("1.txt","r",stdin);srand((int)time(0));Case = 0; while ( scanf("%d",&n) != EOF && n ){init();rep(i,1,n){string a; cin>>a; mapp[ a ] = i; }scanf("%d",&m);rep(i,1,m){string a,b; double ww; cin>>a>>ww>>b; e[mapp[a]][mapp[b]] = ww; }if ( ford() ){printf("Case %d: Yes\n",++Case);}else printf("Case %d: No\n",++Case);}return 0;
}
最优解法 SPFA
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std;
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define mset(var,val) memset(var,val,sizeof(var))
#define LL long long
#define eps 0.000001
#define inf 0x7f7f7f7f
#define llinf 1e18
#define exp 0.000001
#define pai 3.141592654
#define random(x) rand()%(x)
#define lowbit(x) x&(-x)
inline int read()
{int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}while ( a>='0' && a<='9' ){ x=(x<<3)+(x<<1)+a-'0'; a=getchar();}return x*y;
}
#define M 1090
#define N 390
map<string,int >mapp;
int n,m,etot,Case;
struct node {int u,v,nx; double w;
}e[M];int hea[N];
inline void addead(int u,int v,double w){++etot;e[etot].u = u; e[etot].v = v; e[etot].w = w; e[etot].nx = hea[u]; hea[u] = etot;
}
void init(){etot = 0; mapp.clear();mset(hea,0);
}
double dis[N];
bool SPFA(){mset(dis,0);int num[N]; mset(num,0);bool inq[N]; mset(inq,0); queue< int > q; dis[ 1 ] = 1;inq[ 1 ] = 1 ;q.push(1);while ( !q.empty() ){int u = q.front(); q.pop() ; inq[ u ] = 0; for (int i = hea[u]; i ; i = e[i].nx){if ( dis[ e[i].v ] < dis[ e[i].u ]*e[i].w ){dis[ e[i].v ] = dis[ e[i].u ]*e[i].w;if ( !inq[ e[i].v ] ){q.push( e[i].v);inq[ e[i].v ] = 1; num[ e[i].v ]++;if ( num[ e[i].v ] > n )return true; }}}}return false;
}
int main()
{// freopen("1.txt","r",stdin);srand((int)time(0));Case = 0; while ( scanf("%d",&n) != EOF && n ){init();rep(i,1,n){string a; cin>>a; mapp[ a ] = i; }scanf("%d",&m);rep(i,1,m){string a,b; double ww; cin>>a>>ww>>b; addead(mapp[a],mapp[b],ww);}if ( SPFA( ) ){printf("Case %d: Yes\n",++Case);}else printf("Case %d: No\n",++Case);}return 0;
}
POJ 2240 Arbitrage 解题报告相关推荐
- POJ 2745 显示器 解题报告
POJ 2745 显示器 解题报告 编号:2745 考查点:模拟 思路:抽象出来,计算器显示是7个笔画,然后建立数组表示各笔画被覆盖情况,不过这个是我看了书之后实现的,方法果真经典. 提交情况:比 ...
- POJ 2240 Arbitrage(SPFA判正环)
POJ 2240 Arbitrage 题目大意 套利是指利用货币汇率的差异,将一种货币的一个单位转换为同一货币的多个单位.例如,假设1美元买0.5英镑,1英镑买10.0法国法郎,1法国法郎买0.21美 ...
- poj 2240 Arbitrage (Floyd)
链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...
- poj 2240 Arbitrage (floyd 变形)
http://poj.org/problem?id=2240 floyd 的变形 题意 有n个货币,他们的交换情况m个 例如: 3 USDollar BritishPound FrenchFranc ...
- POJ 2240 Arbitrage(判正环)
http://poj.org/problem?id=2240 题意: 货币兑换,判断最否是否能获利. 思路: 又是货币兑换题,Belloman-ford和floyd算法都可以的. 1 #include ...
- poj 2240 Arbitrage(bellman-ford spfa 判断正环)
http://poj.org/problem?id=2240 基本和poj 1860相同 只是把单点变成了任意点 做完1860再做这题就完全把思路套上就过了 做完才发现网上的题解都用的是floyd 不 ...
- poj 1469 COURSES 解题报告
题目链接:http://poj.org/problem?id=1469 题目意思:有 N 个人,P个课程,每一个课程有一些学生参加(0个.1个或多个参加).问 能否使得 P 个课程 恰好与 P 个学生 ...
- poj 1459-Power Network解题报告
链接:http://poj.org/problem?id=1459 网络流的压入重标记算法模板,完全按照算法导论的模式打出来的,没有什么优化之类的,只是为了给自己打模板,要省赛了,加油啊. View ...
- POJ 2240 Arbitrage(最短路 套汇)
题意 给你n种币种之间的汇率关系 推断是否能形成套汇现象 即某币种多次换为其他币种再换回来结果比原来多 基础的最短路 仅仅是加号换为了乘号 #include<cstdio> #in ...
最新文章
- PCL中outofcore模块---基于核外八叉树的大规模点云的显示
- R语言dplyr包为dataframe添加数据列实战( Add Columns):基于mutate()函数添加一个或者多个数据列(尾部添加、头部添加、条件生成、某个具体数据列的前后)
- Go语言入门篇-使用Beego构建完整web应用
- 有多个python版本_python 多版本共存
- REVERSE-PRACTICE-BUUCTF-21
- appSettings 配置mysql_app.config数据库配置字符串的三种取法
- pytorch5:pytorch常用激活函数图像绘制
- [置顶] J2EE (八) 策略模式+装饰模式+反射(java)
- 移动端常见bug汇总001
- 爬虫实例十二 沪深证券股票全站数据爬取
- ArcGIS 10 SDE for ORACLE ---迁移 (1)
- Java后台开发学习进阶路线
- 【CAD技巧】120个常见CAD问题解决办法
- 纯php实现中秋博饼游戏(1):绘制骰子图案
- IDEA 社区版 常用插件列表
- 【Unity】一些不错的unity插件
- 判断当前时间是否在股票开盘时间,不考虑周六周日和节假日
- 一张图看懂华为2019年关键业务进展
- 如何解决vmfution 虚拟机键盘鼠标延迟问题
- 微信小程序电商实战-首页(上)