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. ASP.Net MVC 生成安全验证码

    ---------html <td>验证码:</td>            <td>                <img src="/Logi ...

  2. CheckedListBox与CheckedListBox联动

    包括保存和加载 //查找业务类型 DataTable dtyewu = sb.SelectSyscode(0, true); if (dtyewu.Rows.Count > 0) { flagc ...

  3. CSS浮动特性总结

    1.假设现在CSS中没有浮动(float)属性,那么会变成一个什么样子.我们会发现,目前流行采用浮动方法实现的无论是分栏布局,还是列表排列我们都可以用其他一些CSS属性(不考虑table)代替实现,唯 ...

  4. Linux查看目录挂载点

    用命令 df 即可 # df /var/lib/ Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda3 135979984 669 ...

  5. Django初体验

    为什么使用Django 快速开发 使用python 数据库ORM系统 大量内置应用 后台管理系统 admin 用户认证系统 auth 会话系统 sessions 安全性高 表单验证 SQL注入 跨站点 ...

  6. expdp ORA-39213

    [oracle@BI2 dir_dp]$ impdp ruijie_kettle/ruijie_kettle schemas=ruijie_kettle directory=dir_dp dumpfi ...

  7. 【prism】前期准备

    在网上下了prism框架源码,目前最新版本为4.1,其中包含的内容如下: 其中包含三类文件: 1.类似于Desktop only-Prism Library.bat的批处理文件,用来打开相应的Pris ...

  8. entity framework in mysql

    To start using Entity Framework 6 and Visual Studio 2013 is necessary to install MySQL for Visual St ...

  9. Codeforces Round #328 div2

    Problem_A(592A): 题意: 在一个8*8的棋盘上有黑白两种棋子,'W'代表白色,'B'代表黑色. 玩家A执白子,先走. 白子只能向上走,黑子只能向下走.如果有障碍物则不能走, 比如白色的 ...

  10. 1091-Black Vienna

    描述 This problem is based on the game of Black Vienna. In this version there are three players and 18 ...