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. rsyslog 存储到 mysql

    输出Host1/2的系统日志, 记录到mysql服务器数据库中, 并发布loganalyzer 结构关系如下图: 思路: 通过远程连接mysql, 使得rsyslog的log记录能够写入到mysql中 ...

  2. 【codeforces 255D】Mr. Bender and Square

    [题目链接]:http://codeforces.com/problemset/problem/255/D [题意] 给你一个n*n的方框; 给你一个方块;(以下说的方块都是单位方块) 每一秒钟,可以 ...

  3. 基于json数据格式实现的简单数据库——jsonDB

    已在github上建立项目:https://github.com/ThinkerCodeChina/jsonDB /** +-------------------------------------- ...

  4. Qt 5.3 下OpenCV 2.4.11 开发(5)最高效的像素引用

    OpenCV 提供一个函数 getTickCount() ,能够用来測量一段代码的执行时间.另一个函数 getTickFrequency() 用来返回每秒内的时钟周期.代码操作例如以下: double ...

  5. [React] Refactor a Stateful List Component to a Functional Component with React PowerPlug

    In this lesson we'll look at React PowerPlug's <List /> component by refactoring a normal clas ...

  6. poj 2154 Color(polya计数 + 欧拉函数优化)

    http://poj.org/problem?id=2154 大致题意:由n个珠子,n种颜色,组成一个项链.要求不同的项链数目.旋转后一样的属于同一种.结果模p. n个珠子应该有n种旋转置换.每种置换 ...

  7. zoj 3820 Building Fire Stations (二分+树的直径)

    Building Fire Stations Time Limit: 5 Seconds      Memory Limit: 131072 KB      Special Judge Marjar ...

  8. Android JNI和NDK学习(09)--JNI实例二 传递类对象

    1 应用层代码 NdkParam.java是JNI函数的调用类,它的代码如下:   package com.skywang.ndk; import android.app.Activity; impo ...

  9. Thinkphp5.0怎么加载css和js

    Thinkphp5.0怎么加载css和js 问题 http://localhost/手册上的那个{load href="/static/css/style.css" /} 读取不到 ...

  10. 常见的DNS攻击——偷(劫持)、骗(缓存投毒)、打(DDos)

    常见的DNS攻击包括: 1) 域名劫持 通过采用黑客手段控制了域名管理密码和域名管理邮箱,然后将该域名的NS纪录指向到黑客可以控制的DNS服务器,然后通过在该DNS服务器上添加相应域名纪录,从而使网民 ...