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 ...
随机推荐
- OSI概述问答
1. 网络中体系结构的七层.四层.五层是怎么回事? OSI(Open System Interconnection)开放系统互连参考模型的七层协议体系结构:概念清楚,理论比较完整,但既复杂又不用 ...
- K8s初探
1. K8s概述 2. K8s的工作原理 什么是K8s 用法: 核心概念 集群 Kubernetes Master Node Pod Lable Replication Con ...
- select选中值,传this
<select onChange = "a(this)"></select> function a(obj){ $(obj).find("opti ...
- 自己编写并发布一个Vue组件
自己编写并发布一个Vue组件 1. 几种开源协议的介绍 https://blog.csdn.net/techbirds_bao/article/details/8785413 2.开始编写组件 新建p ...
- JavaScript内存机制
内存模型 JS内存空间分为栈(stack).堆(heap).池(一般也会归类为栈中). 其中栈存放变量,堆存放复杂对象,池存放常量. 基础数据类型与栈内存 JS中的基础数据类型,这些值都有固定的大小, ...
- 找tensorboard
一开始因为用户不对,提示tensorboard:未找到命令 切换正确账户寻找tensorboard 然后打开Xstart,输入firefox,把链接输入进去 即可
- Kneser猜想与相关推广
本文本来是想放在Borsuk-Ulam定理的应用这篇文章当中.但是这个文章实在是太长,导致有喧宾夺主之嫌,从而独立出为一篇文章,仅供参考.$\newcommand{\di}{\mathrm{dist} ...
- POJ——T2553 The Bottom of a Graph
http://poj.org/problem?id=2553 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10987 ...
- Qt之图形(渐变填充)
简述 QGradient可以和QBrush组合使用,来指定渐变填充. Qt目前支持三种类型的渐变填充: QLinearGradient:显示从起点到终点的渐变. QRadialGradient:以圆心 ...
- [数位dp] bzoj 3209 花神的数论题
题意:中文题. 思路:和普通数位dp一样,这里转换成二进制,然后记录有几个一. 统计的时候乘起来就好了. 代码: #include"cstdlib" #include"c ...