d[i]代表从起点出发可以获得最多的钱数,松弛是d[v]=r*d[u],求最长路,看有没有正环

然后这题输入有毒,千万别用cin 因为是大输入,组数比较多,然后找字符串用strcmp就好,千万不要用map

这题刚开始我T了(用的map),还以为组数很多卡spfa呢,然后我上网看了看都是floyd的,然后我用floyd写了一发,891ms过了

然后我感觉spfa的复杂度也不是很大,就是看有没有正环,所以我觉得可能是map+cin的锅,然后改了一发,用的spfa,47ms过

真是,算了,实质是本蒟蒻经验不足(其实也不是没做过卡输入的)

这是map+cin+floyd

#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<iostream>
#include<string>
#include<cmath>
#include<map>
using namespace std;
typedef long long LL;
const int N=;
const int INF=0x3f3f3f3f;
map<string,int>mp;
double d[N][N];
int n,m;
bool fun(){
for(int k=;k<=n;++k)
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
d[i][j]=max(d[i][j],d[i][k]*d[k][j]);
for(int i=;i<=n;++i)
if(d[i][i]>)return true;
return false;
}
int main(){
int cas=;
while(~scanf("%d",&n),n){
mp.clear();
for(int i=;i<=n;++i){
string t;
cin>>t;
mp[t]=i;
}
memset(d,,sizeof(d));
for(int i=;i<=n;++i)
d[i][i]=;
scanf("%d",&m);
while(m--){
string x,y;
double r;
cin>>x>>r>>y;
d[mp[x]][mp[y]]=r;
}
printf("Case %d: ",++cas);
if(fun())printf("Yes\n");
else printf("No\n");
}
}

这是strcmp+spfa(推荐看这个)

#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<iostream>
#include<string>
#include<cmath>
#include<map>
using namespace std;
typedef long long LL;
const int N=;
const int INF=0x3f3f3f3f;
struct Edge{
int v,next;
double r;
}edge[N*N];
int n,m,head[N],tot,cnt[N];
void add(int u,int v,double r){
edge[tot].v=v;
edge[tot].r=r;
edge[tot].next=head[u];
head[u]=tot++;
}
bool inq[N];
double d[N];
queue<int>q;
bool spfa(int s){
for(int i=;i<=n;++i)
d[i]=cnt[i]=inq[i]=;
d[s]=,++cnt[s],inq[s]=true;
while(!q.empty())q.pop();
q.push(s);
while(!q.empty()){
int u=q.front();
q.pop();
inq[u]=false;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
if(d[u]*edge[i].r>d[v]){
d[v]=d[u]*edge[i].r;
if(!inq[v]){
inq[v]=true;
if(++cnt[v]>n)return true;
q.push(v);
}
} }
if(d[s]>)return true;
}
return false;
}
char a[N][];
int find(char *s){
for(int i=;i<=n;++i)
if(!strcmp(s,a[i]))return i;
}
int main(){
int cas=;
while(~scanf("%d",&n),n){
memset(head,-,sizeof(head)),tot=;
for(int i=;i<=n;++i)
scanf("%s",a[i]);
scanf("%d",&m);
while(m--){
char s[];
double r;
scanf("%s%lf",s,&r);
int u=find(s);
scanf("%s",s);
int v=find(s);
add(u,v,r);
}
printf("Case %d: ",++cas);
if(spfa())printf("Yes\n");
else printf("No\n");
}
}

POJ 2240 Arbitrage spfa 判正环的更多相关文章

  1. POJ 2240 Arbitrage(判正环)

    http://poj.org/problem?id=2240 题意:货币兑换,判断最否是否能获利. 思路:又是货币兑换题,Belloman-ford和floyd算法都可以的. #include< ...

  2. POJ 2240 Arbitrage (spfa判环)

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

  3. POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  4. POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】

    题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total ...

  5. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

  6. Currency Exchange POJ - 1860 (spfa判断正环)

    Several currency exchange points are working in our city. Let us suppose that each point specializes ...

  7. poj1860(spfa判正环)

    题目连接:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 money=(nowmoney-手续费)*rat ...

  8. poj 3621 二分+spfa判负环

    http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...

  9. loj 1221(spfa判正环)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25957 思路:由于路线为一个环,将路径上的权值改为c-p*d,那么 ...

随机推荐

  1. 在Mac OS X中使用VIM开发STM32(3)

    本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重!       在上一篇文章中,我们安装了ctags插件,ctags能对我们的源代码文件中的元素建立索引表, ...

  2. dynamic 动态获取object数据

    1.替代XXX.GetType().GetProperty("YYY").GetValue(XXX) static object GetPerson() { return new ...

  3. 转 修改oracle用户密码永不过期

      1.查看用户的proifle是哪个,一般是default: sql>SELECT username,PROFILE FROM dba_users; 2.查看指定概要文件(如default)的 ...

  4. 干货:Web应用上线之前程序员应该了解的技术细节

    [伯乐在线注]:<Web 应用上线前,程序员应考虑哪些技术细节呢?>这是 StackExchange 上面的一个经典问题贴. 最赞回复有 2200+ 顶,虽然大多数人可能都听过其中大部分内 ...

  5. Challenge Checkio(python)—初尝python练习网站

    最近在找点python语言练习的网站,发现这个网站不错 http://www.checkio.org/ 页面设计的也比较漂亮,比较适合学习python的语法知识.不过注册这个网站 开始就得解决一个py ...

  6. classes system in sencha touch

    //http://www.sencha.com/learn/sencha-class-system var Person = new Ext.Class({ name: 'Mr. Unknown', ...

  7. office2016 部分截图

    哈啊哈哈啊哈 看看吧~~~~~~~~~~~~~~~~~

  8. MXNet在64位Win7下的编译安装

    注:本文原创,作者:Noah Zhang  (http://www.cnblogs.com/noahzn/) 我笔记本配置比较低,想装个轻量级的MXNet试试,装完之后报错,不是有效的应用程序,找不到 ...

  9. centos 卸载vsftpd方法

    centos 卸载vsftpd方法 在服务器上安装了vsftpd,配置出错需要卸载vsftpd.卸载vsftpd的命令如下: 1 [root@localhost ~]# rpm -aq vsftpd2 ...

  10. java连接数据库时出现如下错误:

    java.lang.ClassNotFoundException: com.mysql.jdbc.driver at org.apache.catalina.loader.WebappClassLoa ...