HDU1217:Arbitrage(SPFA)
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1217
题目大意
在每种钱币间进行各种交换,最后换回自己如果能赚,那么就Yes,否则No
注意应为有负权所以dijsktra在这里行不通了可以用国产的spfa算法,可比bfs。
我的AC代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
using namespace std;
map<string,int> mat;
int n,m;
char str[100],s1[100],s2[100];
double trip[30][30],dis[30];
int main(void)
{
int spfa(int src);
int i,j,ans=1;
double w;
while(scanf("%d",&n)==1&&n)
{
mat.clear();
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
trip[i][j]=1;
else
trip[i][j]=0;
}
}
for(i=1;i<=n;i++)
{
scanf("%s",str);
mat[str]=i;
}
scanf("%d",&m);
while(m--)
{
scanf("%s%lf%s",s1,&w,s2);
trip[mat[s1]][mat[s2]]=w;
}
int flag=0;
for(i=1;i<=n;i++)
{
if(spfa(i))
{
flag=1;
break;
}
}
printf("Case %d: %s\n",ans++,flag?"Yes":"No");
}
return 0;
}
int spfa(int src)
{
queue<int> q;
int vis[30],i;
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));//全为0,表示碰到有路就搜。
dis[src]=1;
vis[src]=1;
q.push(src);
while(!q.empty())
{
int now=q.front();
q.pop();
vis[now]=0;
for(i=1;i<=n;i++)
{
if(dis[now]*trip[now][i]>dis[i])//有路就搜全部遍历。
{
dis[i]=dis[now]*trip[now][i];
if(dis[src]>1)
return 1;
if(!vis[i])
{
vis[i]=1;
q.push(i);
}
}
}
}
return 0;
}
HDU1217:Arbitrage(SPFA)的更多相关文章
- POJ 2240 Arbitrage (spfa判环)
Arbitrage Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of ...
- POJ 2240 Arbitrage spfa 判正环
d[i]代表从起点出发可以获得最多的钱数,松弛是d[v]=r*d[u],求最长路,看有没有正环 然后这题输入有毒,千万别用cin 因为是大输入,组数比较多,然后找字符串用strcmp就好,千万不要用m ...
- hdu 1217 Arbitrage (spfa算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:通过货币的转换,来判断是否获利,如果获利则输出Yes,否则输出No. 这里介绍一个ST ...
- hdu1217 Arbitrage
Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform on ...
- DFS判断正环
hdu1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Nyoj Arbitrage(Floyd or spfa or Bellman-Ford)
描述Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a curren ...
- (简单) POJ 2240 Arbitrage,SPFA。
Description Arbitrage is the use of discrepancies in currency exchange rates to transform one unit o ...
- POJ 2240 Arbitrage(SPFA+邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...
- Arbitrage HDU1217
汇率转换问题: 怎么样才能套利 可以用Floyd算法: #include<bits/stdc++.h> using namespace std; ][]; int main() { int ...
随机推荐
- 中国天气网 JSON接口的城市编码解析及结果
最近在弄一个Android应用,其中一个功能是天气情况展示,准备使用google的天气API服务(http://www.google.com/ig/api?hl=zh-cn&weather=, ...
- Android-----View绘制流程以及invalidate()等相关方法分析 .
引自:http://blog.csdn.net/qinjuning/article/details/7110211 前言: 本文是我读<Android内核剖析>第13章----View工作 ...
- CocoaPods安装和使用及问题----看过写的最好的
目录 CocoaPods是什么? 如何下载和安装CocoaPods? 如何使用CocoaPods? 场景1:利用CocoaPods,在项目中导入AFNetworking类库 场景2:如何正确编译运行一 ...
- oracle查询每个表的占用空间
Select Segment_Name,Sum(bytes)/1024/1024 From User_Extents Group By Segment_Name order by Sum(bytes) ...
- chrome 开发人员工具
JavaScript Beautifier JavaScript 文件在上线前一般都会压缩下,压缩的 JavaScript 几乎没有可读性,几乎无法设定断点.在 Scripts 面板下面有个 Pret ...
- 基于心跳的socket长连接
http://coach.iteye.com/blog/2024444 基于心跳的socket长连接 博客分类: http socket 案例: 心跳: socket模拟网页的报文连接某个网站,创建t ...
- CodeForces 590B Chip 'n Dale Rescue Rangers
这题可以o(1)推出公式,也可以二分答案+验证. #include<iostream> #include<cstring> #include<cmath> #inc ...
- 计算机学院大学生程序设计竞赛(2015’12) 1006 01 Matrix
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> ...
- 【BZOJ 1572】 1572: [Usaco2009 Open]工作安排Job(贪心+优先队列)
1572: [Usaco2009 Open]工作安排Job Description Farmer John 有太多的工作要做啊!!!!!!!!为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单 ...
- SVG页面loading动态图
https://github.com/SamHerbert/SVG-Loaders demo http://samherbert.net/svg-loaders/