poj 2240 Bellman-Flod 求环
http://poj.org/problem?id=2240
深刻体现了自己代码能力有问题外加改模板能力有问题。外加Debug有问题。以后做到:
1、算法原理能够轻易弄出来。
2、代码模板自己收集各种使用方法,以及已经的做过的改变的方法;
3、没有完整清晰的思路不写程序;
4、在Debug时没有基本绝对的把握,不点击“编译+执行”,不乱试
回到这道题:
我主要是想把Bellman-Ford的模板改为链式前向星的,然后就各种悲剧调试......
学到三点:1、比例的初始化,求最大,源1.0,尚不可到达0,
2、Bellman-Ford求环,能够将原来的n-1次循环(就是最多经过n-1条边时最短路径),改为n次循环,当然也能够再把判负权的代码改了,相当于第n次。 这就是我以下贴的两个版本号的代码:
3、链式前向星的Bellman-Ford模板例如以下
两种版本号都是125ms AC--事实上一样
n次循环:
#include<cstdio>
#include<cstring>
#include <string>
#include <map>
#include <iostream>
using namespace std;
//#define INF 0x0f0f0f0f
const double INF = 0;
#define MAXN 1011
#define Max(a,b) (a)>(b)?(a):(b)
struct Node{
int to;
double w;
int next;
}edge[MAXN];
int head[MAXN],s,n,m,path[MAXN];
double dist[MAXN];//注意w以及该数组的类型
using namespace std;
void init()
{
memset(head,-1,sizeof(head));
memset(path,-1,sizeof(path)); } void addEdge(int u,int v,double w,int k)
{
edge[k].to=v;
edge[k].w=w;
edge[k].next=head[u];
head[u]=k;/*起点为u的边为edge[k]*/
}
int Bellman_Ford(int v0)//v0--soure
{
for(int i=0;i<n;i++)dist[i]=0;//1.0;
dist[v0]=1.0; for(int i=0;i<n;i++)
for(int u=0;u<n;u++)
{
for(int j=head[u];j!=-1;j=edge[j].next)
{
if(dist[u]*edge[j].w>dist[edge[j].to])
{ dist[edge[j].to]= dist[u]*edge[j].w;
path[edge[j].to]=u;
}
} } if(dist[v0]>1.0)return 1;
return 0;
} int main()
{
// freopen("poj2240.txt","r",stdin);
int icase=0,flag;
double w;
string u,v;
string str;
while(scanf("%d",&n) && n)
{
init();
map<string, int>ss;
for(int i=0;i<n;i++)
{
cin>>str;
ss[str]=i;
}
scanf("%d",&m);
for(int i=0;i<m;i++)
{
cin >> u >> w >> v;
addEdge(ss[u],ss[v],w,i);
} flag=1;
for(int i=0;i<n;i++)
{ if(Bellman_Ford(i)){flag=0;printf("Case %d: Yes\n",++icase);break;} }
if(flag) printf("Case %d: No\n",++icase);
}
return 0;
}
改负权代码 能够作为链式前向星的Bellman-Ford的模板:
#include<cstdio>
#include<cstring>
#include <string>
#include <map>
#include <iostream>
using namespace std;
//#define INF 0x0f0f0f0f
const double INF = 0;
#define MAXN 1011
#define Max(a,b) (a)>(b)?(a):(b)
struct Node{
int to;
double w;
int next;
}edge[MAXN];
int head[MAXN],s,n,m,path[MAXN];
double dist[MAXN];//注意w以及该数组的类型
using namespace std;
void init()
{
memset(head,-1,sizeof(head));
memset(path,-1,sizeof(path)); } void addEdge(int u,int v,double w,int k)
{
edge[k].to=v;
edge[k].w=w;
edge[k].next=head[u];
head[u]=k;/*起点为u的边为edge[k]*/
}
int Bellman_Ford(int v0)//v0--soure
{
for(int i=0;i<n;i++)dist[i]=0;//1.0;
dist[v0]=1;
int i,j,l,u,k; for(int i=1;i<n;i++)
for(u=0;u<n;u++)
{
for(j=head[u];j!=-1;j=edge[j].next)
{
if(dist[u]*edge[j].w>dist[edge[j].to])
{
dist[edge[j].to]= dist[u]*edge[j].w;
path[edge[j].to]=u;
}
} } for(int j=0;j<n;j++)
{
for(k=head[j];k!=-1;k=edge[k].next)
{
if(edge[k].to == v0 && dist[j]*edge[k].w>1.0)//dist[edge[k].to])
return 1;
}
}
return 0;
} int main()
{
//freopen("poj2240.txt","r",stdin);
int icase=0,flag;
double w;
string u,v;
string str;
while(scanf("%d",&n) && n)
{
init();
map<string, int>ss;
for(int i=0;i<n;i++)
{
cin>>str;
ss[str]=i;
}
scanf("%d",&m);
for(int i=0;i<m;i++)
{
cin >> u >> w >> v;
addEdge(ss[u],ss[v],w,i);
}
flag=1;
for(int i=0;i<n;i++)
{
if(Bellman_Ford(i)){flag=0;printf("Case %d: Yes\n",++icase);break;} }
if(flag) printf("Case %d: No\n",++icase);
}
return 0;
}
poj 2240 Bellman-Flod 求环的更多相关文章
- POJ 2240 Arbitrage (求负环)
Arbitrage 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/I Description Arbitrage is the ...
- POJ 2240 Arbitrage (spfa判环)
Arbitrage Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of ...
- POJ 2240 - Arbitrage - [bellman-ford求最短路]
Time Limit: 1000MS Memory Limit: 65536K Description Arbitrage is the use of discrepancies in currenc ...
- POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)
POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...
- POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...
- poj 1474 Video Surveillance - 求多边形有没有核
/* poj 1474 Video Surveillance - 求多边形有没有核 */ #include <stdio.h> #include<math.h> const d ...
- 最短路(Floyd_Warshall) POJ 2240 Arbitrage
题目传送门 /* 最短路:Floyd模板题 只要把+改为*就ok了,热闹后判断d[i][i]是否大于1 文件输入的ONLINE_JUDGE少写了个_,WA了N遍:) */ #include <c ...
- Codeforces Round #346 (Div. 2) E - New Reform 无相图求环
题目链接: 题目 E. New Reform time limit per test 1 second memory limit per test 256 megabytes inputstandar ...
- poj 2240 Arbitrage (Floyd)
链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...
随机推荐
- HDU-1257 最少拦截系统 贪心/DP 最长上升子序列的长度==最长不上升子序列的个数?
题目链接:https://cn.vjudge.net/problem/HDU-1257 题意 中文题咯中文题咯 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然 ...
- Couldn't connect to Docker daemon at http+docker://localunixsocket - is it running?
解决方法: 1.进入启动文件目录 2.将用户加入到docker 组 sudo gpasswd -a ${USER} docker 3.使用root用户 sudo su 4. 切换当前用户 su ${ ...
- swift学习笔记(五)构造过程
构造过程是为了使用某个类.结构体或枚举类型的实例而进行的准备过程.在构造过程中,对每一个属性进行了初始值预设和其它必要的准备和初始化工作. 与OC相比,swift的构造函数.不须要返回值.同一时候,在 ...
- 程序员之--C语言细节13(二维数组和指针,&*a[i][0]的理解,数组1[e]和e[1]非常可能你没见过)
主要内容:二维数组和指针.&*a[i][0]的理解.数组1[e]和e[1] #include <stdio.h> #define NUM_ROWS 10 #define NUM_C ...
- 公布Qt Widgets桌面应用程序的方法
公布Qt Widgets桌面应用程序的方法 Qt是一款优秀的跨平台开发框架,它能够在桌面.移动平台以及嵌入式平台上执行.眼下Qt 5介绍程序公布的文章帖子比較少.大家又很想要知道怎样公布Qt应用程序, ...
- js如何实现简繁体互转
js如何实现简繁体互转 一.总结 一句话总结:其实无论是简体还是繁体,都是在显示端(前端),其实所有的我只用动js就好了,没必要动php. 当然,后端也可以做前端的事情,只是麻烦了点(要多通信两次,第 ...
- 英语影视台词---五、And Then There Were None
英语影视台词---五.And Then There Were None 一.总结 一句话总结:求阿加莎·克里斯蒂小说<无人生还>的英文版<And Then There Were No ...
- 语法错误: unexpected ''); ?></span></span></h2> ' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';'
语法错误: unexpected ''); ?></span></span></h2>' (T_CONSTANT_ENCAPSED_STRING), expe ...
- nyoj--42--一笔画问题(并查集)
一笔画问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下来. ...
- python-搭建django环境
python-搭建django环境 标签(空格分隔): python Django简介 Django,发音为[`dʒæŋɡəʊ],是用python语言写的开源web开发框架,并遵循MVC设计.劳伦斯出 ...