题目链接

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. 1107: 单向公路(bfs+输入整理)(DFS也可以,而且更快)

    Description 某个地区有许多城镇,但并不是每个城镇都跟其他城镇有公路连接,且有公路的并不都能双向行驶.现在我们把这些城镇间的公路分布及允许的行驶方向告诉你,你需要编程解决通过公路是否可以从一 ...

  2. DWR整合之Servlet

    DWR 与 Servlet 有 2 个 Java 类你一般需要用在 DWR 中,是 webContext 和 WebContextFactory 在 DWR 1.x 它们在 uk.ltd.getahe ...

  3. UI常用控件

    UICommonlyUsedControls [UI常用控件] 不需要学习多么深入,但是要知道系统提供的有用的控件. 一.UISwitch(开关) 二.UIActivityIndicatorView( ...

  4. NAVICAT 拒绝链接的问题

    处理步骤 1.先在原有的建一个新的用户 2.成功后,再点击原有的,即可使用

  5. js 增删改查

    <html><head lang="en"> <meta charset="UTF-8"> <title>< ...

  6. webservice中采用协议Http,它是指什么意思

    webservice 协议 Web Service使用的是 SOAP (Simple Object Access Protocol)协议soap协议只是用来封装消息用的.封装后的消息你可以通过各种已有 ...

  7. [即时通讯]openfire启动失败解决办法

    当你发现你的电脑上的openfire无论你是重新安装还是重启都无法启动的情况下你可以按照我下面写的那些终端指令来启动你的openfire不用再去重新做你的系统了. 一般你发现你的openfire打开出 ...

  8. iOS_init相关信息

    第一.initWithNibName这个方法是在controller的类在IB中创建,但是通过Xcode实例化controller的时候用的. 第二.initWithCoder 是一个类在IB中创建但 ...

  9. iOS 主动抛出异常

    http://blog.csdn.net/jymn_chen/article/details/38096749 http://blog.sina.com.cn/s/blog_7270a06c0101b ...

  10. iOS开发——pch文件创建

    新换的公司,接手的项目里面连pch文件都没有,每次需要用到屏幕的宽高时,都是现写.今天既然碰到了,就把PCH这个玩意说一下. 1.Command+N,打开新建文件窗口:iOS->Other-&g ...