luogu3385 负环 (spfa)

我在做spfa的时候,如果有一个点被更新了超过N次,证明这个图里是有负环的。

(神TM输出YE5和N0

 1 #include<bits/stdc++.h>
 2 #define pa pair<int,int>
 3 #define CLR(a,x) memset(a,x,sizeof(a))
 4 using namespace std;
 5 typedef long long ll;
 6 const int maxn=2020,maxm=3030;
 7
 8 inline ll rd(){
 9     ll x=0;char c=getchar();int neg=1;
10     while(c<‘0‘||c>‘9‘){if(c==‘-‘) neg=-1;c=getchar();}
11     while(c>=‘0‘&&c<=‘9‘) x=x*10+c-‘0‘,c=getchar();
12     return x*neg;
13 }
14
15 struct Edge{
16     int a,b,l,ne;
17 }eg[maxm*2];
18 int egh[maxn],ect;
19 int N,M;
20 int cnt[maxn],dis[maxn];
21 queue<int> q;
22 bool inq[maxn];
23
24 inline void adeg(int a,int b,int l){
25     eg[++ect].a=a;eg[ect].b=b;eg[ect].l=l;eg[ect].ne=egh[a];egh[a]=ect;
26 }
27
28 bool spfa(int s){
29
30     while(!q.empty()) q.pop();
31     dis[s]=0;q.push(s);cnt[s]=1;
32     while(!q.empty()){
33         int p=q.front();inq[p]=0;
34         // printf("%d %d\n",p,dis[p]);
35         q.pop();
36         for(int i=egh[p];i;i=eg[i].ne){
37             int b=eg[i].b;
38             if(dis[b]>dis[p]+eg[i].l){
39                 dis[b]=dis[p]+eg[i].l;
40                 if(inq[b]) continue;
41                 if(++cnt[b]>N) return 1;
42                 q.push(b);
43                 inq[b]=1;
44             }
45         }
46     }return 0;
47 }
48
49 int main(){
50     //freopen("","r",stdin);
51     int i,j,k;
52     for(int T=rd();T;T--){
53         ect=0;CLR(egh,0);
54         N=rd(),M=rd();
55         for(i=1;i<=M;i++){
56             int a=rd(),b=rd(),c=rd();
57             adeg(a,b,c);
58             if(c>=0) adeg(b,a,c);
59         }
60         bool ans=0;
61         CLR(cnt,0);CLR(dis,127);CLR(inq,0);
62         for(i=1;i<=N&&!ans;i++){
63             if(!cnt[i]) ans|=spfa(i);
64         }
65         if(ans) printf("YE5\n");
66         else printf("N0\n");
67     }
68     return 0;
69 }

原文地址:https://www.cnblogs.com/Ressed/p/9782106.html

时间: 2024-08-30 12:11:41

luogu3385 负环 (spfa)的相关文章

p3385 【模板】负环(spfa)

题目描述 毒瘤数据要求判负环 分析: 还是融合了不少题解的思想的. 负环定义: 权值和为负的环 //在网络上并没有找到一个官方定义,暂且这么理解. SPFA: 支持负边权的情况. spfa是最短路算法.如果一个环上的边权有负的,我们可以重复走这条路来获得更小的边权,所以这可以作为我们使用spfa判断负环的根据 //如果一个位置入队次数不小于n次,那它一定位于环上,所以这可以作为我们的判断标准. 听说STL的队列比较慢,换掉! 但是如果手打队列的话 队尾指针队首指针一直++根本停不下来怎么办? 我

UVA 558 Wormholes 【SPFA 判负环】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=499 题意:就是判断图中有无负环 SPFA,某个节点入队次数大于n就是有负环. 代码: #include <iostream> #include <stdio.h> #include <string.h> #include <algori

(简单) LightOJ 1074 Extended Traffic,SPFA+负环。

Description Dhaka city is getting crowded and noisy day by day. Certain roads always remain blocked in congestion. In order to convince people avoid shortest routes, and hence the crowded roads, to reach destination, the city authority has made a new

POJ 3259 虫洞旅行 spfa判负环

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31425   Accepted: 11431 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way p

【模板】负环(spfa)

洛谷——P3385 [模板]负环 题目描述 暴力枚举/SPFA/Bellman-ford/奇怪的贪心/超神搜索 输入输出格式 输入格式: 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M,表示图有N个顶点,M条边 接下来M行,每行三个整数a b w,表示a->b有一条权值为w的边(若w<0则为单向,否则双向) 输出格式: 共T行.对于每组数据,存在负环则输出一行"YE5"(不含引号),否则输出一行"N0"(不含引号). 输入输出样例

ACM: POJ 3259 Wormholes - SPFA负环判定

POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way pa

bzoj1690:[Usaco2007 Dec]奶牛的旅行(分数规划+spfa判负环)

前段时间准备省选没更,后段(?)时间省选考砸没心情更,最近终于开始恢复刷题了... 题目大意:有n个点m条有向边的图,边上有花费,点上有收益,点可以多次经过,但是收益不叠加,边也可以多次经过,但是费用叠加.求一个环使得收益和/花费和最大,输出这个比值. 显然这就是经典的分数规划题啊,就是最优比率环,那么就二分答案,将所有边(u,v)的边权改为[v的点权-(u,v)原边权*mid],这可以算是最优比率环的公式了吧,然后判一下是否有正环,有的话就说明答案可行.判正环有够别扭的,那就全部改成相反数然后

lightoj 1074 spfa判断负环

Extended Traffic Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1074 Appoint description:  System Crawler  (2016-05-03) Description Dhaka city is getting crowded and noisy day by day. Certain

SPFA(负环) LightOJ 1074 Extended Traffic

题目传送门 题意:收过路费.如果最后的收费小于3或不能达到,输出'?'.否则输出到n点最小的过路费 分析:关键权值可为负,如果碰到负环是,小于3的约束条件不够,那么在得知有负环时,把这个环的点都标记下,DFS实现. #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int N = 2e2 + 5; cons