poj3259Wormholes (Bellman_Ford/SPFA/Floyed算法判断是否存在负环)
题目链接:http://poj.org/problem?id=3259
题目大意:一个图,有n个顶点,其中有m条边是双向的且权值为为正,w条边是单向的且权值为负,判断途中是否存在负环,如果有输出YES,没有输出NO。
Sample Input
2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8
Sample Output
NO
YES 解题思路:套用Bellman_Ford算法判断图是否存在负环
具体详见代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int inf=0x3f3f3f3f;
int n,m,w,dist[],tot;
struct node{
int from,to,d;
}edge[];
bool Bellman_Ford()
{
for(int i=;i<=n;i++) dist[i]=inf; //初始化
dist[]=;
for(int i=;i<n;i++)
{
bool flag=; //判断该轮是否可以松弛
for(int j=;j<tot;j++)
{
if(dist[edge[j].to]>dist[edge[j].from]+edge[j].d)
{ //进行松弛操作
dist[edge[j].to]=dist[edge[j].from]+edge[j].d;
flag=;
}
}
if(flag) return false; //当轮没有松弛表示没有负环
}
for(int i=;i<tot;i++)
{
if(dist[edge[i].to]>dist[edge[i].from]+edge[i].d) //仍然可以松弛,表示有负环
return true;
}
return false;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&w);
tot=;
for(int i=;i<=m;i++) //双向边
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
edge[tot].from=a;
edge[tot].to=b;
edge[tot].d=c;
tot++;
edge[tot].from=b;
edge[tot].to=a;
edge[tot].d=c;
tot++;
}
for(int i=;i<=w;i++) //单向负边
{
scanf("%d%d%d",&edge[tot].from,&edge[tot].to,&edge[tot].d);
edge[tot].d=-edge[tot].d;
tot++;
}
if(Bellman_Ford())
printf("YES\n");
else
printf("NO\n");
}
return ;
}
套用SPFA算法判断图是否存在负环
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<cmath>
#include<list>
#include<deque>
#include<cstdlib>
#include<bitset>
#include<stack>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int maxn=;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
const int dir[][]={{,},{-,},{,},{,-}};
ll n,m,p;
int head[maxn],vis[maxn],InQueue[maxn],dis[maxn];
int tot;
struct Edge{
int to,w,next;
}edge[maxn*];
void Add_Edge(int u,int v,int w)
{
edge[tot].to=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
bool spfa()
{
queue<int> que;
while(que.size())que.pop();
que.push();
vis[]=;
InQueue[]++;
dis[]=;
while(que.size())
{
int u=que.front();
que.pop();
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].w)
{
dis[v]=dis[u]+edge[i].w;
if(!vis[v])
{
vis[v]=;
InQueue[v]++;
if(InQueue[v]>=n)return true;
que.push(v);
}
}
}
}
return false;
}
int main()
{
int t;
cin>>t;
while(t--)
{
tot=;
cin>>n>>m>>p;
for(int i=;i<=n;i++)
{
vis[i]=InQueue[i]=;
dis[i]=INF;
head[i]=-;
}
int u,v,w;
for(int i=;i<m;i++)
{
cin>>u>>v>>w;
Add_Edge(u,v,w);
Add_Edge(v,u,w);
}
for(int i=;i<p;i++)
{
cin>>u>>v>>w;
w=-w;
Add_Edge(u,v,w);
}
if(spfa()) puts("YES");
else puts("NO");
}
return ;
}
Floyed算法判断负环:
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<cmath>
#include<list>
#include<deque>
#include<cstdlib>
#include<bitset>
#include<stack>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int maxn=;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
const int dir[][]={{,},{-,},{,},{,-}};
int n,m,p,mp[][];
bool Floyed()
{
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(mp[i][j]>mp[i][k]+mp[k][j])
mp[i][j]=mp[i][k]+mp[k][j];
}
if(mp[i][i]<)return true;
}
}
return false;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n>>m>>p;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(i==j)mp[i][j]=;
else mp[i][j]=INF;
}
int u,v,w;
for(int i=;i<m;i++)
{
cin>>u>>v>>w;
if(w<mp[u][v]) mp[u][v]=mp[v][u]=w;
}
for(int i=;i<p;i++)
{
cin>>u>>v>>w;
mp[u][v]=-w;
}
if(Floyed())puts("YES");
else puts("NO");
}
return ;
}
poj3259Wormholes (Bellman_Ford/SPFA/Floyed算法判断是否存在负环)的更多相关文章
- 使用spfa算法判断有没有负环
如果存在最短路径的边数大于等于点数,就有负环 给定一个n个点m条边的有向图,图中可能存在重边和自环, 边权可能为负数. 请你判断图中是否存在负权回路. 输入格式 第一行包含整数n和m. 接下来m行每行 ...
- POJ 3259 Wormholes(bellman_ford,判断有没有负环回路)
题意:John的农场里field块地,path条路连接两块地,hole个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前 ...
- vijos1053 用spfa判断是否存在负环
MARK 用spfa判断是否存在负环 判断是否存在负环的方法有很多, 其中用spfa判断的方法是:如果存在一个点入栈两次,那么就存在负环. 细节想想确实是这样,按理来说是不存在入栈两次的如果边权值为正 ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- [ACM] 最短路算法整理(bellman_ford , SPFA , floyed , dijkstra 思想,步骤及模板)
以杭电2544题目为例 最短路 Problem Description 在每年的校赛里,全部进入决赛的同学都会获得一件非常美丽的t-shirt. 可是每当我们的工作人员把上百件的衣服从商店运回到赛场的 ...
- bellman-ford算法(判断有没有负环)
#include <iostream> #include <vector> #include<string> #include<cstring> usi ...
- SPFA - Luogu 3385 【模板】负环
[模板]负环 描述 找负环 输入 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M,表示图有N个顶点,M条边 接下来M行,每行三个整数a b w,表示a->b有一条权值为w ...
- poj 3259 Wormholes : spfa 双端队列优化 判负环 O(k*E)
/** problem: http://poj.org/problem?id=3259 spfa判负环: 当有个点被松弛了n次,则这个点必定为负环中的一个点(n为点的个数) spfa双端队列优化: 维 ...
- poj3259 Wormholes【Bellman-Ford或 SPFA判断是否有负环 】
题目链接:poj3259 Wormholes 题意:虫洞问题,有n个点,m条边为双向,还有w个虫洞(虫洞为单向,并且通过时间为倒流,即为负数),问你从任意某点走,能否穿越到之前. 贴个SPFA代码: ...
随机推荐
- 使用node操作mongodb
let mongodb = require('mongodb'); let MongodbClient = mongodb.MongoClient; MongodbClient.connect('mo ...
- [转帖]Windows 内核说明
来源:https://zhidao.baidu.com/question/398191459.html 自己的理解. windows 的内核文件 是在 c:\windows\system32 目录下面 ...
- Mybatis 配置resultMap一对多关联映射
resultMap配置: 引用: PO类: 接口: 测试: public class UserMapperTest { private SqlSessionFactory sqlSessionFact ...
- C# Note7:MVVM模式之数据绑定
一.资源说明 (1)本文参考自: 一步步走进WPF的MVVM模式(二):数据绑定 WPF之数据绑定总结 二.正文 数据绑定 (Data Binding)是WPF最重要的特性之一,也是实现 MVVM( ...
- C# Note3:大话Ninject
前言 之所以研究Ninject,是因为初入职在开发XX项目的ComponentService部分时用到了它,一下子发现了它的强大.渐渐地发现在项目中,有时会用到优秀的第三方开源库,这些都是前人智慧的结 ...
- Linux基础学习(11)--Shell编程
第十一章——Shell编程 一.基础正则表达式 1.正则表达式与通配符(*,?,[ ]): 2.基础正则表达式: 二.字符截取命令 1.cut字段提取命令: 空格分割时,不知道空格有多少个,无法分割行 ...
- mysql参数优化记录
服务器参数16G内存,4核CPUvim /etc/my.cnf 原: back_log=170 max_connections=600 max_user_connections=0 thread_co ...
- 常见IT工具软件总结
1. 阿里云在线迁移服务 2.智能媒体管理 格式转换 业务域名管理 1. 每个业务有一个英文单词, 1. 每个 git 的命名应该是 chgg-业务英文-种类 2. 例如 chgg-plant-api ...
- idea使用破解版mybatis plugin插件失败,idea打不开的解决方案
记一次错误解决方案 打开 idea.vmoptions (Help -> Edit Custom VM Options...) ,在这里进行了修改 加了破解jar包的路径,但是之前的路径中有中文 ...
- solr配置ik中文分词(二)
上一篇文章主要介绍了solr的安装与配置,这篇文章主要记录如何使用ik分词器对中文进行分词. 步骤: 1.下载ik分词jar包:ik-analyzer-solr5-5.x.jar. 2.将下载的jar ...