uva 558 Bellman_Ford
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的更多相关文章
- UVA 558 判定负环,spfa模板题
1.UVA 558 Wormholes 2.总结:第一个spfa,好气的是用next[]数组判定Compilation error,改成nexte[]就过了..难道next还是特殊词吗 题意:科学家, ...
- uva 558 - Wormholes(Bellman Ford判断负环)
题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...
- UVA 558 Wormholes
要问是否存在一个总权重为负数的环,用dfs即可解决. time:33ms #include <cstdio> #include <cstring> #define N 3000 ...
- UVA 558 Wormholes 【SPFA 判负环】
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
- 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 ...
- UVA - 558 Wormholes (SPEA算法模板题)
先给出题面:https://vjudge.net/problem/UVA-558 题意描述:给你含n个点以及m条边的图,让你判断在这个图中是否存在负权回路. 首先,我们来介绍什么是SPEA算法 SPF ...
- UVA 558 SPFA 判断负环
这个承认自己没看懂题目,一开始以为题意是形成环路之后走一圈不会产生负值就输出,原来就是判断负环,用SPFA很好用,运用队列,在判断负环的时候,用一个数组专门保存某个点的访问次数,超过了N次即可断定有负 ...
- UVa 11090 Going in Cycle!!【Bellman_Ford】
题意:给出n个点m条边的加权有向图,求平均值最小的回路 自己想的是用DFS找环(真是too young),在比较找到各个环的平均权值,可是代码实现不了,觉得又不太对 后来看书= =好巧妙的办法, 使用 ...
- UVa 11090 Going in Cycle!! (Bellman_Ford)
题意:给定一个加权有向图,求平均权值最小的回路. 析:先十分答案,假设答案是 ans,那么有这么一个回路,w1+w2+w3+...+wk < k*ans,这样就是答案太大,然后移项可得,(w1- ...
随机推荐
- Agile.Net 组件式开发平台 - 数据访问组件
Agile.DataAccess.dll 文件为系统平台数据访问支持库,基于FluentData扩展重写,提供高效的性能与风格简洁的API,支持多种主流数据库访问. 当前市面上的 ORM 框架,如 E ...
- C# 保存PictureBox中的图片到数据库,并从数据库读取图片显示到PictrueBox,解决报错 “无效参数”
下面是两段关键代码: /// <summary> /// 将一张图片转换为字节 /// </summary> /// <param name="img" ...
- ubuntu下mysql的环境搭建及使用
ubuntu下mysql的环境搭建及使用 环境安装 使用如下命令分别安装服务端程序,客户端程序,及客户端依赖库 sudo apt-get install mysql-server sudo apt-g ...
- C# 线程--第二线程方法
概述 上一章节中和大家分享了线程的基础使用方法.在这一章中来和大家分享线程的一些常用方法. 主要包括:线程阻塞,线程终止,线程锁三方面. Thread 的 Sleep 和 Join 方法 Thread ...
- 急缺【jQuery】人才,要求熟悉jQuery,熟悉Js,熟悉前端开发
是一份兼职 是与jQuery相关的写作任务,有写作兴趣的欢迎站短(有blog者优先). 要求就是熟悉js和jquery,项目经验丰富(项目经验一定要丰富). 钱不多,不到1W,如果月薪超过1W的,我想 ...
- [GeekBand] 探讨C++新标准之新语法——C++ 11~14
一. 可变参数模板(Variadic Templates) 在C++11中,出现了参数数目可变的模板,这部分在之前C++高级编程的时候就有学习到. 其实,在C中就有类似的设定.最常用的printf() ...
- JQuery 解决 鼠标快速滑过后,会执行多次滑出的问题
如果用slideToggle,鼠标快速滑过后,滑进滑出很多次,要解决这个问题,用stop(false,true) $(".Nav_L").hover(function () { $ ...
- 【转】JS函数的定义与调用方法
JS函数调用的四种方法:方法调用模式,函数调用模式,构造器调用模式,apply,call调用模式 1.方法调用模式:先定义一个对象,然后在对象的属性中定义方法,通过myobject.property来 ...
- c#winform,制作可编辑html编辑器
大神勿喷,新手记笔记 材料 网上下载kindeditor,动手在写个htmldome,图中的e.html.然后全部扔到了bin/debug下面,(x86是要扔到bin/x86/debug) 中间bod ...
- 为云饰数据库添加Index
Asset Collection: 1. _id_ 2. CategoryId_1_Date_-1 3. CategoryId_1_Id_1 4. CategoryId_1_Name_1 5. Cat ...