三道题都是考察最短路算法的判环。其中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. Codevs 1684 垃圾陷阱

    1684 垃圾陷阱 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 卡门--农夫约翰极其珍视的一条Holsteins奶牛--已经落了 ...

  2. [翻译][MVC 5 + EF 6] 11:实现继承

    原文:Implementing Inheritance with the Entity Framework 6 in an ASP.NET MVC 5 Application 1.选择继承映射到数据库 ...

  3. 网址、URL

    手册网:http://www.shouce.ren/ in_array().array_search().array_key_exists() http://www.shangxueba.com/ji ...

  4. centos6.5 安装python2.7.5

    1. 下载python2.7.5,保存到 /data/http://www.python.org/ftp/python/ 2. 解压文件tar xvf Python-2.7.5.tar.bz2 3. ...

  5. ios中XPath的语法

    在XML的读写中,我们知道有Xpath的语法 1.定位节点:(/)代表绝对的路径,代表起始地位置.(//)表示文件中所有符合模式的元素都会被选出来,即使是处于树中不同的层级也会被选出来 2.KissX ...

  6. API获得ip,JS获得IP地理信息

      <script type="text/javascript" src="http://zone.xmp.kankan.xunlei.com/find_area_ ...

  7. MySQL基础学习之索引

    创建新表新索引 CREATE TABLE 表名(数据名 类型,INDEX  索引名称(属性)) 创建存在表的索引 CREATE INDEX 索引名称  ON 表名(属性) 修改索引 ALTER TAB ...

  8. 捕获ClientDataSet.ApplyUpdates和SocketConnection异常

    核心提示:如何捕获ClientDataSet.ApplyUpdates的错误,不用ReconcileError... var cdsEmp:TClientDataSet; //保存 procedure ...

  9. tableView -- tips

    1. 如果发现TableView的第一个sectionHeader不显示, 那么可以断定, 你没有用代理方法来设置 sectionHeader的高度! #pragma mark - delegate ...

  10. 【转载】Java 升级到jdk7后DbVisualizer 6 启动空指针的处理方案

    将JDK从6升级到了7(或从其他电脑移植DBV文件夹后),每当启动DbVisualizer 6的时候都会报空指针异常 在官网上找到了相关的方案,如下: In the DbVisualizer inst ...