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 ...
随机推荐
- Snuke's Subway Trip
すぬけ君の地下鉄旅行 / Snuke's Subway Trip Time limit : 3sec / Memory limit : 256MB Score : 600 points Problem ...
- Hadoop webHDFS设置和使用说明
1.配置 namenode的hdfs-site.xml是必须将dfs.webhdfs.enabled属性设置为true,否则就不能使用webhdfs的LISTSTATUS.LISTFILESTATUS ...
- 初始化Direct3D
1.Direct3D概述 Direct3D是一套底层图形API,借助该API,我们能够利用硬件加速功能来绘制3D场景. 设备制造商将其产品支持的全部功能都实现到HAL中. 1.1 Direct3D提供 ...
- smarty(原理概述)
转自:http://www.cnblogs.com/RightDear/archive/2012/11/06/2756218.html smarty(模板引擎,模板技术) 使用smarty主要是为了实 ...
- Go Runtime hashmap实现
努力学习go中,看到skoo博客内容很不错, 所以转载学习下 前两天有小伙伴问道是否看过 Go 语言 map 的实现,当时还真没看过,于是就花了一点时间看了一遍 runtime 源码中的 hashma ...
- [转]ASP.NET Core 1 Deploy to IIS
本文转自: http://webmodelling.com/webbits/aspnet/aspnet-deploy-iis.aspx 15 Sep 2016. This tutorial will ...
- STM32内置参照电压的使用(转)
源:STM32内置参照电压的使用 每个STM32芯片都有一个内部的参照电压,相当于一个标准电压测量点,在芯片内部连接到ADC1的通道17. 根据数据手册中的数据,这个参照电压的典型值是1.20V,最小 ...
- Ext分页之php中,真分页显示
这是我经过很多天调试的真分页显示Ext组件 显示页面ext.php <html> <head> <meta http-equiv="Content-Type&q ...
- INC函数
1.inc(integer) inc(a)等于a:=a+1; 例: i := 100; inc(i); 结果:i等于101 2.inc(integer,integer) inc(a,b)等于a:=a+ ...
- Memcached源码分析之从SET命令开始说起
作者:Calix 如果直接把memcached的源码从main函数开始说,恐怕会有点头大,所以这里以一句经典的“SET”命令简单地开个头,算是回忆一下memcached的作用,后面的结构篇中关于命令解 ...