spfa负环判断
2024-10-18 06:48:29
正常spfa中加入time数组,循环判断一个点是否入队并更新了n次以上注意是 > n!!其余的没有什么问题
扩展的还有,寻找所有负环上的点,这个可以在spfa中time 发现负环的时候,对那个点进行dfs操作,找到所有的负环上的点即可
void dfs(int u)
{
cir[u] = 1;
for(int i = id[u];~i;i = edge[i].pre)
{
if(!cir[edge[i].to])
dfs(edge[i].to);
}
}
一下负权回路代码以poj3259为例(poj又挂了,暂时还没发交题测试,欢迎大家来找bug)
/*
spfa负环的判断
*/
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <algorithm>
#define inf (1 << 29)
using namespace std;
typedef long long ll;
const int maxn = 600;
const int maxm = 3e3;
int id[maxn],cnt;
struct node{
int to,cost,pre;
}e[maxm * 2];
int dis[maxn];
int vis[maxn];
int time[maxn]; queue<int> q;
void init()
{
while(q.size())q.pop();
memset(time,0,sizeof(time));
memset(vis,0,sizeof(vis));
memset(id,-1,sizeof(id));
cnt = 0;
}
void add(int from,int to,int cost)
{
e[cnt].to = to;
e[cnt].cost = cost;
e[cnt].pre = id[from];
id[from] = cnt++;
}
int spfa(int s,int n)
{
for(int i= 0;i <= n;i++)
dis[i] = inf;
q.push(s);
vis[s] = 1;
time[s] = 1;
dis[s] = 0; while(q.size())
{
int now = q.front();
q.pop();
vis[now] = 0;
for(int i = id[now];~i;i = e[i].pre)
{
int to = e[i].to;
int cost = e[i].cost;
if(dis[to] > dis[now] + cost)
{
dis[to] = dis[now] + cost;
if(!vis[to])
{
vis[to] = 1;
q.push(to);
if(++time[to] > n)
return 0;
}
}
}
}
return 1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
init();
int a,b,x;
for(int i = 1;i <= m;i++)
{
scanf("%d%d%d",&a,&b,&x);
add(a,b,x);
add(b,a,x);
}
for(int i = 1;i <= k;i++)
{
scanf("%d%d%d",&a,&b,&x);
add(a,b,-x);
}
int op = spfa(1,n); if(op)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}
最新文章
- 《ES6基础教程》之 Call 方法和 Apply 方法
- 洛谷CON1041 NOIP模拟赛一试
- X64下MmIsAddressValid的逆向及内存寻址解析
- 市委组织部考核项目——利用EasyUi中可编辑的DataGrid控件对多行数据进行编辑并提交
- IOS 提交审核,遇到Missing Push Notification Entitlement 问题。
- ExtJs之单选及多选框
- 浅析ado.net获取数据库元数据信息 DeriveParameters
- ext中嵌入flash
- 改进基于Boost.Asio的聊天服务
- 【NOIP2013】华容道 广搜+spfa
- Learn Lua in 15 Minutes
- 如何正确使用Java序列化?
- P61 实践作业
- ubuntu16.04中设置python3
- scipy的一些函数名
- JVM 内部原理(四)— 基本概念之 JVM 结构
- editcap的使用
- sql server性能查询,连接数
- matlab中norm函数的用法
- English trip V1 - 4.Do you have it? Teacher:Patrick Key: have - has doesn&#39;t have
热门文章
- PAT 1032 挖掘机技术哪家强(20)(有测试样例)
- APP强制退出
- 关于nodejs 假设httpserver,会发现一次网页打开,服务端会响应两次的问题;
- MySQL学习笔记-MySQL体系结构总览
- [AI]AI章2 框架比较
- 记录点复习题目和linux学习
- 转录组表达量计RPKM、FPKM、TPM说明
- java script入门之知识
- fiddler抓包时显示Tunnel to......443是怎么回事
- openstack之flavor管理