POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告
三道题都是考察最短路算法的判环。其中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 解题报告的更多相关文章
- 最短路(Bellman_Ford) POJ 1860 Currency Exchange
题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...
- POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...
- POJ 1860 Currency Exchange 最短路+负环
原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- POJ 1860 Currency Exchange (最短路)
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- POJ 1860 Currency Exchange【bellman_ford判断是否有正环——基础入门】
链接: http://poj.org/problem?id=1860 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- poj - 1860 Currency Exchange Bellman-Ford 判断正环
Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...
- POJ 1860 Currency Exchange (Bellman-Ford)
题目链接:POJ 1860 Description Several currency exchange points are working in our city. Let us suppose t ...
- 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 19881 Accepted: 711 ...
随机推荐
- 超强的ACM题目类型总结
转:初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. ...
- 当linux遇上多网卡时
我虚拟机有三个网卡,有两个在用,分别是不同的网段 eth4: 192.168.100.6/24 eth6: 192.168.137.131/24 但是默认系统把我的内部网络eth6设置为所有ip地址需 ...
- C#简单的tcpserver
实现一个简单的TCPserver,用tcplistener实现. 当收到客户端特定信息"101"时,发送给客户端"202“指令. using System; using ...
- linux批量修改文件名的shell脚本
linux中批量修改文件名的shell脚本代码,主要是使用了rename,结合shell,喜欢的朋友可以参考下 使用 rename 命令 ======================== NAME ...
- ECshop网点程序优化-后台添加类目自动选择上次父类目并计算Sort Order
如果在ECshop后台批量添加过大量类目的人都能体会到是多么的不方便(这点还是要说一下ECshop的产品经理,细节上还是要多注意),每次添加都需要在几百个类目里面找到要添加的父类目也是一个麻烦事,比如 ...
- 用twisted 做一个日志收集系统
混沌初开 起初我是不会上logging模块的,直接导致了即时有了日志,我也存到了数据库中,而且量也不大,是否能遇到异常只能靠运气了 开天辟地 不得不说,没有任何输出的线上环境真的挺难调试的,当然,聪明 ...
- Codeforces Round #345 (Div. 1) B. Image Preview
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed ...
- Android开发(一):环境搭建
引言 本系列将记录我在步入Android开发过程中的一些流水账及经验,如有疏漏,还望不吝赐教. 目录 1.JDK安装及配置 2.Eclipse.Android SDK ADT安装及配置 正文 1.JD ...
- 条件编译用法(#ifndef #define #endif#else)
1. 文件中的#ifndef 头件的中的#ifndef,这是一个很关键的东西.比如你有两个C文件,这两个C文件都include了同一个头文件.而编译时,这两个C文件要一同编译成一个可运行文件,于是问题 ...
- leetcode-173:Binary Search Tree Iterator(Java)
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...