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 ...
随机推荐
- dorado中的creationType选择类型
新建model层中 DataType类型的时候,有几个属性creationType,matchType时候需要在右侧选择对应的javaBean,这是时候要在弹出的对话框搜索. 此时,只要搜索javaB ...
- hdu 1753 大明A+B(高精度小数加法)
//深刻认识到自己的粗心,为此浪费了一天.. Problem Description 话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫"大明". 这时他已经不是 ...
- HDOJ 1422 重温世界杯 -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1422 Problem Description 世界杯结束了,意大利人连本带利的收回了法国人6年前欠他们 ...
- mysql中的sql时间格式转换
from_unixtime(unix_timestamp, format) 把时间戳转化为指定的格式 as: select from_unixtime(addTime, '%Y-%m-%d %h:%i ...
- html5 meta头部设置
<meta name="viewport" content="height=[pixel_value | device-height], width=[pixel_ ...
- Apose 套打
给web添加一个dll引用:Apose.Words 下载链接:http://yunpan.cn/cA7v6uceM6KVw 提取码 11df 在Global.asax里面的Application_S ...
- IntelliJ IDEA 14.x 创建工作空间与多个Java Web项目
以往的Eclipse.NetBeans等开发工具不同,IDEA的Project相当与Eclipse的Workspace,而Module相当于Project. 下边就给出Eclipse与IDEA的概念的 ...
- 【toplink】 位居第一的Java对象关系可持续性体系结构
TopLink,是位居第一的Java对象关系可持续性体系结构,原署WebGain公司的产品,后被Oracle收购,并重新包装为Oracle AS TopLink.TOPLink为在关系数据库表中存储 ...
- 关于LookAt
Transform.LookAt 注视 function LookAt (target : Transform, worldUp : Vector3 = Vector3.up) : void 旋转物体 ...
- Source Insight 显示中文乱码
Source Insight 3.X utf8支持插件震撼发布 继上次SI多标签插件之后,因为公司内部编码改为utf8编码,因此特意做了这个Source Insight 3.X utf8插件. 下载地 ...