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. 栈(顺序存储)C++模板实现

    #include <iostream> using namespace std; template <typename T> class stack{ private: int ...

  2. java basic

    //java 声明常量 //final 数据类型 常量名=值; //as: final float PI=3.14f;/ PI=3.14002F //默认浮点为 double //break:跳出多重 ...

  3. Winform TreeView控件技巧

    在开发的时候经常使用treeview控件来显示组织结构啊,目录结构啊,通常会结合属性checkedboxs,来做选中,取消的操作下面是一个选中,取消的小例子,选中节点的时候,如果节点存在子节点,可以选 ...

  4. 11个有用的Linux命令

    Linux命令行吸引了大多数Linux爱好者.一个正常的Linux用户一般掌握大约50-60个命令来处理每日的任务.今天为你解释下面几个命令:sudo.python.mtr.Ctrl+x+e.nl.s ...

  5. Educational Codeforces Round 7 F - The Sum of the k-th Powers 拉格朗日插值

    The Sum of the k-th Powers There are well-known formulas: , , . Also mathematicians found similar fo ...

  6. 【c3p0】目前使用它的开源项目有Hibernate,Spring等

    C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展.目前使用它的开源项目有Hibernate,Spring等. c3p0与dbcp区别 JNDI ...

  7. BZOJ 1227 虔诚的墓主人

    Description 小W 是一片新造公墓的管理人.公墓可以看成一块N×M 的矩形,矩形的每个格点,要么种着一棵常青树,要么是一块还没有归属的墓地.当地的居民都是非常虔诚的基督徒,他们愿意提前为自己 ...

  8. OpenGL ES 3.0 基础知识

    首先要了解OpenGL的图形管线有哪些内容,再分别去了解其中的相关的关系: 管线分别包括了顶点缓冲区/数组对象,定点着色器,纹理,片段着色器,变换反馈,图元装配,光栅化,逐片段操作,帧缓冲区.其中顶点 ...

  9. UICountingLabel实现数字变化的动画效果-b

    在大多数金融类 app 上或者其他 app 需要数字展示的地方, 经常会有如下的动画效果: 动画效果 怎么做呢? 一.下载UICountingLabel 下载地址: https://github.co ...

  10. java-development.sh

    vi /etc/profile.d/java-development.sh export JAVA_HOME=/usr/local/java/jdk1..0_55 export JRE_HOME=$J ...