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 求环的更多相关文章

  1. POJ 2240 Arbitrage (求负环)

    Arbitrage 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/I Description Arbitrage is the ...

  2. POJ 2240 Arbitrage (spfa判环)

    Arbitrage Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of ...

  3. POJ 2240 - Arbitrage - [bellman-ford求最短路]

    Time Limit: 1000MS Memory Limit: 65536K Description Arbitrage is the use of discrepancies in currenc ...

  4. POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)

    POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...

  5. POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)

    POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...

  6. poj 1474 Video Surveillance - 求多边形有没有核

    /* poj 1474 Video Surveillance - 求多边形有没有核 */ #include <stdio.h> #include<math.h> const d ...

  7. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

    题目传送门 /* 最短路:Floyd模板题 只要把+改为*就ok了,热闹后判断d[i][i]是否大于1 文件输入的ONLINE_JUDGE少写了个_,WA了N遍:) */ #include <c ...

  8. Codeforces Round #346 (Div. 2) E - New Reform 无相图求环

    题目链接: 题目 E. New Reform time limit per test 1 second memory limit per test 256 megabytes inputstandar ...

  9. poj 2240 Arbitrage (Floyd)

    链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...

随机推荐

  1. LA3231 Fair Share 二分_网络流

    Code: #include<cstdio> #include<vector> #include<queue> #include<cstring> #i ...

  2. Tarjan专题

    前排Orz tarjan tarjan算法在图的连通性方面有非常多的应用,dfn和low数组真是奥妙重重(并没有很搞懂反正背就完事了) 有向图强连通分量 #include<iostream> ...

  3. es6 学习7 Set 和 Map 数据结构

     Set 和 Map 数据结构 一.Set ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. const set = new Set([1, 2, 3, 4, ...

  4. UVA-10003 Cutting Sticks 动态规划 找分界点k的动规

    题目链接:https://cn.vjudge.net/problem/UVA-10003 题意 有根棍子,上面有些分割点(n<50),每次按分割点切割棍子时,费用为当前棍子的长度. 问有什么样的 ...

  5. BZOJ 2555 SubString(LCT+后缀树)

    喜闻乐见的LCT+SAM 此题要求动态插入,直接上后缀树.然后询问其实就是求一个节点的子树后缀结束节点的个数. 因为建立后缀树需要插入和删除,就直接上LCT.每次加入一个点,把它到根的路径加一 (现在 ...

  6. 学习《PythonWeb开发实战(董伟明)》中文PDF+源代码

    python可以用了进行数据分析,也可以进行Web开发,一般会使用django或者flask等进行开发. 国内介绍python web的书有写的不错的,推荐看看<PythonWeb开发实战> ...

  7. Laravel核心解读--ENV的加载和读取

    Laravel在启动时会加载项目中的.env文件.对于应用程序运行的环境来说,不同的环境有不同的配置通常是很有用的. 例如,你可能希望在本地使用测试的Mysql数据库而在上线后希望项目能够自动切换到生 ...

  8. 如何配置任意目录下Web应用程序

    1,首先创建一个Web项目,tomcat 7, JDK 1.8 2,创建Web项目并部署到tomcat服务器下运行的步骤和方法: 在Eclipse下创建一个JAVA project 在JAVA项目下创 ...

  9. 埃及分数 迭代加深搜索 IDA*

    迭代加深搜索 IDA* 首先枚举当前选择的分数个数上限maxd,进行迭代加深 之后进行估价,假设当前分数之和为a,目标分数为b,当前考虑分数为1/c,那么如果1/c×(maxd - d)< a ...

  10. 洛谷 P1193 洛谷团队训练VS传统团队训练

    P1193 洛谷团队训练VS传统团队训练 题目背景 “在中学的信息学教育领域,洛谷无疑是一个相当受欢迎的辅助网站.同时有百余所学校正在通过洛谷进行信息学竞赛(以后简称OI)的教育.洛谷之所以如此受欢迎 ...