Bellman_Ford算法   求图中是否存在负权值的回路   若图中不存在   则最短路最多经过n-1个结点   若经过超过n-1个节点 则存在负权值的回路  此图永远无法找到最短路  每条边最多经过n-1次松弛~~

#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int INF = 100000000;
const int maxn = 1005;
vector<int> G[maxn];
int weight[maxn][maxn];
queue<int> q;
bool inq[maxn];
int d[maxn],vis[maxn];
int n,m;
bool Bellman_Ford()
{
    for(int i = 0 ; i < n; i++) d[i] = INF,inq[i] = false;
    d[0] = 0;
    memset(vis, 0, sizeof(vis));
    q.push(0);
    inq[0] = true;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        inq[u] = false;
        for(int i = 0; i < (int)G[u].size(); i++)
        {
            int v = G[u][i];
            if(d[v] > d[u] + weight[u][v])
            {
                d[v] = d[u] + weight[u][v];
                if(!inq[v])
                {
                    inq[v] = true;
                    vis[v++];
                    if(vis[u] >= n) return true;
                    q.push(v);
                }
            }
        }
    }
    return false;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i = 0; i < n; i++) G[i].clear();
        for(int i = 0 ; i < m; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            G[u].push_back(v);
            weight[u][v] = w;
        }
        if(Bellman_Ford()) puts("possible");
        else puts("not possible");
    }
    return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
using namespace std; const int N = 2005;
const int INF = 0xffffff; struct Edge
{
int u,v,w;
} edge[N]; int n,m;
int d[N]; bool Bellman_Ford()
{
for(int i = 0; i < n; i++) d[i] = INF;
d[0] = 0;
bool flag;
for(int i = 0; i < n; i++)
{
flag=false;
for(int j = 0; j < m; j++)
{
if(d[edge[j].v] > d[edge[j].u]+edge[j].w)
{
d[edge[j].v] = d[edge[j].u]+edge[j].w;
flag=true;
}
}
if(!flag)
break;
}
for(int j = 0; j < m; j++)
if(d[edge[j].v] > d[edge[j].u]+edge[j].w)
return true;
return false;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(edge,0,sizeof(edge));
scanf("%d%d",&n,&m);
for(int i = 0; i < m; i++)
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
if(Bellman_Ford())
puts("possible");
else
puts("not possible");
}
return 0;
}

uva 558 Bellman_Ford的更多相关文章

  1. UVA 558 判定负环,spfa模板题

    1.UVA 558 Wormholes 2.总结:第一个spfa,好气的是用next[]数组判定Compilation error,改成nexte[]就过了..难道next还是特殊词吗 题意:科学家, ...

  2. uva 558 - Wormholes(Bellman Ford判断负环)

    题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...

  3. UVA 558 Wormholes

    要问是否存在一个总权重为负数的环,用dfs即可解决. time:33ms #include <cstdio> #include <cstring> #define N 3000 ...

  4. UVA 558 Wormholes 【SPFA 判负环】

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  5. uva 558 tree(不忍吐槽的题目名)——yhx

    You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...

  6. UVA - 558 Wormholes (SPEA算法模板题)

    先给出题面:https://vjudge.net/problem/UVA-558 题意描述:给你含n个点以及m条边的图,让你判断在这个图中是否存在负权回路. 首先,我们来介绍什么是SPEA算法 SPF ...

  7. UVA 558 SPFA 判断负环

    这个承认自己没看懂题目,一开始以为题意是形成环路之后走一圈不会产生负值就输出,原来就是判断负环,用SPFA很好用,运用队列,在判断负环的时候,用一个数组专门保存某个点的访问次数,超过了N次即可断定有负 ...

  8. UVa 11090 Going in Cycle!!【Bellman_Ford】

    题意:给出n个点m条边的加权有向图,求平均值最小的回路 自己想的是用DFS找环(真是too young),在比较找到各个环的平均权值,可是代码实现不了,觉得又不太对 后来看书= =好巧妙的办法, 使用 ...

  9. UVa 11090 Going in Cycle!! (Bellman_Ford)

    题意:给定一个加权有向图,求平均权值最小的回路. 析:先十分答案,假设答案是 ans,那么有这么一个回路,w1+w2+w3+...+wk < k*ans,这样就是答案太大,然后移项可得,(w1- ...

随机推荐

  1. sql中nvarchar(max)长度测试

    nvarchar(max)长度测试:在使用convert强制类型转化之后 文本长度可以突破8000的上限.并且nvarchar(max)的最大长度可达到2^31以下为验证SQL: Declare @A ...

  2. Agile.Net 组件式开发平台 - 数据访问组件

    Agile.DataAccess.dll 文件为系统平台数据访问支持库,基于FluentData扩展重写,提供高效的性能与风格简洁的API,支持多种主流数据库访问. 当前市面上的 ORM 框架,如 E ...

  3. DOS批处理命令-注释

    注释是每个程序中不可或缺的(不是对计算机来说,而是对我们这些程序员阅读代码来说) 语法: ①rem 这是批处理的注释命令,rem后面的内容全部是注释 例:rem 这是一行注释 ②:: 批处理遇到以冒号 ...

  4. 第七篇、使用UIView的animateWithDuration方法制作简易动画

    import UIKit class LolitaCircleButton: UIButton { private var color: UIColor private var imageURL: S ...

  5. 使用C#通过调用minitab的COM库自动化生成报表

    本文介绍通过C#调用minitab com组建自动化生成报表的方法. 首先需要在minitab中通过手动配置的方式生成报表来得到该报表的命令行,过程如下 选择菜单“编辑器”->“启用命令”启用命 ...

  6. 专家解说IT行业存在哪些安全风险

    本人为原创作品:e良师益友 ,转载是并且注明 网络带动了IT行业的发展,同时也带来了安全风险,国外的专家分析2014年IT行业存在的11个安全隐患,随着越来越多技术的不断开发,为方便人们记忆账号,产生 ...

  7. 你可以用OpenCV来干什么

    本文翻译自开源图书“OpenCV by Example”中第1章中的“What can you do with OpenCV?”小节. 使用OpenCV,你几乎可以做任何你能够想到的计算机视觉任务.现 ...

  8. php curl 伪造IP来源的代码分享

    php curl 可以模仿用户登录,还可以模仿用户IP地址.伪造IP来源. 1,curl发出请求的文件fake_ip.php: <?php $ch = curl_init(); $url = & ...

  9. PHP中session的使用

    1.初始化(使用session前都要使用,一个页面用一个就可以了) session_start(); 2.保存 $_SESSION[$sessionName]=$value; (value可以是dou ...

  10. V9任何页面GET调用内容分页的说明

    如标题,很多人想要在网站首页或其他的页面实现分页效果,说明如下: 一般特殊页面实现分页是通过GET语句的(论坛很多牛人用修改PHPCMS系统函数来实现,个人不推荐,因为你改了系统文件,不利于官方下一步 ...