三道题都是考察最短路算法的判环。其中1860和2240判断正环,3259判断负环。

难度都不大,可以使用Bellman-ford算法,或者SPFA算法。也有用弗洛伊德算法的,笔者还不会SF-_-……

直接贴代码。

1860 Currency Exchange:

#include <cstdio>
#include <cstring> int N,M,S;
double V;
const int maxn=;
int first[maxn],vv[maxn*maxn],nxt[maxn*maxn];
double ww[maxn*maxn],cc[maxn*maxn];
double d[maxn];
int count[maxn];
int stack[maxn];
bool vis[maxn]; bool SPFA()
{
for(int i=;i<=N;i++)
vis[i]=count[i]=d[i]=;
int top=;
stack[++top]=S;
d[S]=V;
vis[S]=true;
count[S]++; while(top)
{
int a=stack[top--];
vis[a]=false; for(int e=first[a];e;e=nxt[e])
{
if(d[vv[e]]<(d[a]-cc[e])*ww[e])
{
d[vv[e]]=(d[a]-cc[e])*ww[e];
if(!vis[vv[e]])
{
stack[++top]=vv[e];
count[vv[e]]++;
if(count[vv[e]]>=N)
return false;
vis[vv[e]]=true;
}
}
}
}
return true;
} int main()
{
// freopen("in.txt","r",stdin);
int e=;
scanf("%d%d%d%lf",&N,&M,&S,&V);
for(int i=;i<=M;i++)
{
int u,v;
double w1,c1,w2,c2;
scanf("%d%d%lf%lf%lf%lf",&u,&v,&w1,&c1,&w2,&c2);
nxt[e]=first[u],vv[e]=v,ww[e]=w1,cc[e]=c1,first[u]=e++;
nxt[e]=first[v],vv[e]=u,ww[e]=w2,cc[e]=c2,first[v]=e++;
}
printf(SPFA()?"NO\n":"YES\n");
}

2240 Arbitrage:  起点不确定,需要枚举。map方便一点,hash……应该快一点

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#include <map>
#include <string> const int maxn=;
map<string,int> mp;
int first[maxn],vv[maxn*maxn],nxt[maxn*maxn];
double ww[maxn*maxn];
int stack[maxn];
double d[maxn];
int count[maxn];
bool vis[maxn];
int n; int SPFA(int sta)
{
int top=;
stack[++top]=sta;
memset(vis,,sizeof(vis));
memset(d,,sizeof(d));
memset(count,,sizeof(count));
count[sta]++;
d[sta]=; while(top)
{
int a=stack[top--];
vis[a]=false; for(int e=first[a];e;e=nxt[e]) if(d[vv[e]]<d[a]*ww[e])
{
d[vv[e]]=d[a]*ww[e];
if(!vis[vv[e]])
{
stack[++top]=vv[e];
count[vv[e]]++;
if(count[vv[e]]>=n)
return false;
vis[vv[e]]=true;
}
}
}
return true;
} int main()
{
// freopen("in.txt","r",stdin);
int cas=;
string str,str2;
while(scanf("%d",&n) && n)
{
mp.clear();
for(int i=;i<=n;i++)
{
cin>>str;
mp[str]=i;
} memset(first,,sizeof(first)); int e=;
int m;
scanf("%d",&m);
while(m--)
{
cin>>str>>ww[e]>>str2;
int u=mp[str];
int v=mp[str2];
nxt[e]=first[u],vv[e]=v,first[u]=e++;
} bool flag=false;
for(int i=;i<=n;i++) if(!SPFA(i))
flag=true;
printf("Case %d: ",cas++);
printf(flag?"Yes\n":"No\n");
}
}

3259 Wormholes:

#include <cstdio>
#include <cstring> int first[],vv[],ww[],nxt[];
int d[]; bool relax(int u,int v,int w)
{
if(d[v]<=d[u]+w) return false;
d[v]=d[u]+w;
return true;
} bool bellman_ford(int n)
{
memset(d,,sizeof(d)); bool flag;
for(int i=;i<=n;i++)
{
flag=true;
for(int u=;u<=n;u++)
for(int e=first[u];e;e=nxt[e])
if(relax(u,vv[e],ww[e]))
flag=false;
if(flag)
return true;
else if(i==n)
return false;
}
return true;
} int main()
{
// freopen("in.txt","r",stdin);
int T;
scanf("%d",&T); while(T--)
{
int N,M,W;
scanf("%d%d%d",&N,&M,&W);
int e=;
int u,v,w; memset(first,,sizeof(first));
for(int i=;i<M;i++)
{
scanf("%d%d%d",&u,&v,&w);
nxt[e]=first[u],vv[e]=v,ww[e]=w,first[u]=e++;
nxt[e]=first[v],vv[e]=u,ww[e]=w,first[v]=e++;
}
for(int i=;i<W;i++)
{
scanf("%d%d%d",&u,&v,&w);
nxt[e]=first[u],vv[e]=v,ww[e]=-w,first[u]=e++;
} if(bellman_ford(N))
printf("NO\n");
else
printf("YES\n");
}
}

POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告的更多相关文章

  1. 最短路(Bellman_Ford) POJ 1860 Currency Exchange

    题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...

  2. POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)

    POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...

  3. POJ 1860 Currency Exchange 最短路+负环

    原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Tota ...

  4. POJ 1860 Currency Exchange (最短路)

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

  5. POJ 1860 Currency Exchange【bellman_ford判断是否有正环——基础入门】

    链接: http://poj.org/problem?id=1860 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

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

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

  7. poj - 1860 Currency Exchange Bellman-Ford 判断正环

    Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...

  8. POJ 1860 Currency Exchange (Bellman-Ford)

    题目链接:POJ 1860 Description Several currency exchange points are working in our city. Let us suppose t ...

  9. 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19881   Accepted: 711 ...

随机推荐

  1. VIM 拼写/spell check

    VIM 拼写检查/spell check 一.Hunspell科普 Hunspell 作为一个拼写检查的工具,已经用在了许多开源的以及商业软件中.包括Google Chrome, Libreoffic ...

  2. makefile--#的不正确使用

    /usr/vacpp/bin/makeC++SharedLib -o /cicm/src/dao/testcase/rel/FUNCTEST.ibmcpp -brtl -bnortllib -p100 ...

  3. iOS 构造方法

    构造方法:用于在对象创建出来的时候为对象的成员变量或者属性赋值 1.构造方法就是初始化对象的方法,它属于对象方法.构造方法分为系统自带和自定义构造方法. 如何创建自定义构造方法: 属于对象方法那么以  ...

  4. day19 数据库的初步认识

    一:数据库的概念 数据库:一个用于储存数据并可以对之进行管理和使用的软件系统. sql:struct(结构)  query(查询)  language(语言) 结构化查询语言: 其实是一种国际化语言标 ...

  5. RX学习笔记:FreeCodeCamp的JavaScript基本算法挑战

    FreeCodeCamp的JavaScript基本算法挑战 https://www.freecodecamp.com 2016-07-03 JavaScript还不是非常熟悉,用已经会的知识来解这些题 ...

  6. SharePoint2013TimerJob计时器发送邮件

    http://www.3fwork.com/b500/000307MYM008190/

  7. oracle expdp 无法导出SYS下特定TABLE

    创建测试表: D:\app\product\\db_1>sqlplus "/as sysdba" SQL :: Copyright (c) , , Oracle. All r ...

  8. Open CASCADE 基础类(Foundation Classes)

    1 介绍(Introduction) 1 如何使用Open CASCADE技术(OCCT)基础类. This manual explains how to use Open CASCADE Techn ...

  9. codeforce #339(div2)C Peter and Snow Blower

    Peter and Snow Blower 题意:有n(3 <= n <= 100 000)个点的一个多边形,这个多边形绕一个顶点转动,问扫过的面积为多少? 思路:开始就认为是一个凸包的问 ...

  10. linux管道的容量和内部组织方式

    1.管道容量  count=65536,即64KB #include<stdio.h> #include<sys/types.h> #include<unistd.h&g ...