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 求环

时间: 2024-08-28 05:25:44

poj 2240 Bellman-Flod 求环的相关文章

[ An Ac a Day ^_^ ][kuangbin带你飞]专题四 最短路练习 POJ 2240 Arbitrage spfa求负环

题意就是问倒腾外币能不能升值 不用spfa 用其他的最短路算法也可以 松弛条件换成dist[v]<dist[u]*e[u][i].value 当然 貌似只有spfa有这个坑…… 有A  (value>1.0) A 这种情况……我的天 用Dij Floyd都只用判断如果松弛到了自己 那么一定有环 直接跳出就行 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<

POJ 2240 Arbitrage (spfa判环)

Arbitrage Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 Frenc

POJ 2240 Arbitrage

Bellman 求最大环. 询问货币是否纯在套汇. 假如给你 1 元,通过兑换之后 超过 1 元就是存在套汇了. 用 map 映射比较方便. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<queue> #include<map> #include<iostream>

POJ 2240 -- Arbitrage(Bellman-Ford)

POJ 2240 -- Arbitrage(Bellman-Ford) 题意: 已知n种货币,以及m种货币汇率及方式,问能否通过货币转换,使得财富增加. Bellman-ford 算法: 一个具有n个顶点的图如果不存在环,则从顶点x,到顶点y,最多经过n-1条边(要考虑连通性,每个顶点最多经过 1 次),因此 x 到 y 的最短路 最多经过 n - 1 次松弛操作(就是更新长度)就应该出现,如果第 n 次松弛还可以得到最优,那么这个图就肯定是存在环了(直接用Dijkstra 就无法得到最优的,环

poj 2240

Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14832   Accepted: 6255 Description Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currenc

POJ 2299 Ultra-QuickSort(归并排序求逆序对数)

题目地址:POJ 2299 今天下午的多校看来没有白做...实在做不出题闲着无聊看小白鼠学会了个归并排序.哈哈. 归并排序简单地说其实就是先分成一个二叉树直至单个,然后依次从最底层不断进行合并,逆序对数就是在合并的过程中,加入后面的那段中到了比他大的时候,那后面的那些就都是比他大的,都是逆序对数,所以直接加上即可.网上资料很多,就不细说了..用了分治的思想. 自己根据理解写的代码,考虑的太不全面了..又调了好长时间... 代码如下: #include <algorithm> #include

POJ 2540 半平面交求可行区域面积

Hotter Colder Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2343   Accepted: 981 Description The children's game Hotter Colder is played as follows. Player A leaves the room while player B hides an object somewhere in the room. Player

poj 2104主席树求区间第k小

POJ - 2104 题意:求区间第k小 思路:无修改主席树 AC代码: #include "iostream" #include "iomanip" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set&

POJ 1177 Picture(扫描线求周长)

与求面积并的差不多,但是这个与扫描的方向相同的情况不太好处理,如果扫描线离散化两次扫两遍其实也可以解决这个问题,但是这样无论在时间还是空间上稍微就有点浪费了啊.这里因为我是离散x坐标的所以对于平行于y轴的方向上的统计比较难统计.处理的方法是:标记区间左边的断点,和右边的断点,求出这个区间一共有多少个断点.就可以统计出平行于y轴的长度了.这里合并的时候需要判断右边的左区间和左边的右区间是否相同,如果相同的话,说明他们连接到了一起,要减去多加的. Picture Time Limit: 2000MS