题目链接

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)的更多相关文章

  1. POJ 2240 Arbitrage (spfa判环)

    Arbitrage Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of ...

  2. POJ 2240 Arbitrage spfa 判正环

    d[i]代表从起点出发可以获得最多的钱数,松弛是d[v]=r*d[u],求最长路,看有没有正环 然后这题输入有毒,千万别用cin 因为是大输入,组数比较多,然后找字符串用strcmp就好,千万不要用m ...

  3. hdu 1217 Arbitrage (spfa算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:通过货币的转换,来判断是否获利,如果获利则输出Yes,否则输出No. 这里介绍一个ST ...

  4. hdu1217 Arbitrage

    Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform on ...

  5. DFS判断正环

    hdu1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  6. 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 ...

  7. (简单) POJ 2240 Arbitrage,SPFA。

    Description Arbitrage is the use of discrepancies in currency exchange rates to transform one unit o ...

  8. POJ 2240 Arbitrage(SPFA+邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...

  9. Arbitrage HDU1217

    汇率转换问题: 怎么样才能套利 可以用Floyd算法: #include<bits/stdc++.h> using namespace std; ][]; int main() { int ...

随机推荐

  1. Android------自定义ListView详解

    引自:http://www.cnblogs.com/phonegap/archive/2011/04/28/2535732.html 在这里我就对自定义ListView进行讲解下吧,直入真题吧,首先我 ...

  2. GridView绑定DataKeyNames以及如何取这些值

    DataKeyNames='FID'   //前台绑定一个值GridView1.DataKeys[e.Row.RowIndex].Value.ToString;-------------------- ...

  3. cin 与 scanf 的不同

    cin输入更方便: 首先,cin 是个C++类型对象,它的类型是basic_istream,scanf 是个不定参数的函数,其次,cin 所属的类重载了 >> 运算符,使输入更简单了,比如 ...

  4. PAT (Advanced Level) 1068. Find More Coins (30)

    01背包路径输出. 保证字典序最小:从大到小做背包. #include<cstdio> #include<cstring> #include<cmath> #inc ...

  5. linux下OpenOffice与SwfTools环境安装

    一.安装所需要的库与组件 yum install gcc* automake zlib-devel libjpeg-devel giflib-devel freetype-devel 二.安装open ...

  6. JavaFX 2.0+ WebView /WebEngine render web page to an image

    http://stackoverflow.com/questions/7796558/javafx-2-0-webview-webengine-render-web-page-to-an-image ...

  7. 关于go的不爽

    这里想记录下,自己学习.使用go语言,对于go语言不爽的地方. 1. 函数返回类型接在参数类型后面,不容易一眼看清楚函数的返回类型 如下,是不是有种很花的感觉. func NewReader(s st ...

  8. CodeForces 609C Load Balancing

    先算出目标状态,然后拿当前状态与目标状态对比,即可算出答案 #include<cstdio> #include<cmath> #include<cstring> # ...

  9. STL中map的一个知识点

    问题背景 在做USACO Section 1.1 Greedy Gift Givers的时候,我最初的想法是直接用一个map来进行数据处理.但是后来产生一个让我感到疑问的地方,后来我经过测试,发现了这 ...

  10. bootstrap建立响应式网站——tab选项卡

    1.bootstrap给我们提供了标签页 细细看了一下bootstrap的标签页源码,对tab选项卡有了更深的理解.其实说来也简单,以前自己写js和css时没有意识到整体的划分.就是分为两部分:一部分 ...