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 ...
随机推荐
- 关闭和释放JDBC
今天在项目中看到一段代码, Connection conn = null; ResultSet rs = null; PreparedStatement pss = null; try { ...
- Android之EditText控件
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content&q ...
- 数据库建表的时候报 “1215 Cannot add foreign key constraint”
很大原因是因为: 引用表中的字段类型和被引用的主键的类型不统一. 比如说学生表中有一个班级ID字段引用班级表. 班级表的ID是int类型,学生表中的班级ID是Varchar类型. 肯定会提示上述121 ...
- HDU 4557 非诚勿扰 队列、(记一次失败的SBT尝试)
非诚勿扰 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) [Problem De ...
- 关于easyUI的datagrid的编辑功能时的问题
编辑时,如果form中包含了id输入域,会发送一个{id,id}这样的字符串到服务端,因为javascript的function edit(){}逻辑中,已经拿到Id提交了.所以,编辑和添加功能共用的 ...
- 安卓图表引擎AChartEngine(六) - 框架源码结构图
包结构: org.achartengine: org.achartengine.model: org.achartengine.renderer: org.achartengine.tools: 安卓 ...
- for和getElementByTagName配合
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HDU 3874 Necklace
莫队算法. #include<cstdio> #include<cstring> #include<cmath> #include<queue> #in ...
- linux下文件和目录的颜色表示
蓝色表示目录: 绿色表示可执行文件: 红色表示压缩文件: 浅蓝色表示链接文件: 灰色表示其它文件: 红色闪烁表示链接的文件有问题了: 黄色是设备文件,包括block, char, fifo. (摘自: ...
- SQL Server--导入和导出向导
作用:创建一个只需要最少的转换就可以快速导入或导出数据的包 操作步骤: 1 打开打入和导出向导方式 有四种方式 1)在SSMS(SQL Server Management Studio)中,右击目标数 ...