luogu P3385 模板 負環

2022-05-05 16:24:06 字數 1294 閱讀 8797

暴力列舉/spfa/bellman-ford/奇怪的貪心/超神搜尋

輸入格式:

第一行乙個正整數t表示資料組數,對於每組資料:

第一行兩個正整數n m,表示圖有n個頂點,m條邊

接下來m行,每行三個整數a b w,表示a->b有一條權值為w的邊(若w<0則為單向,否則雙向)

輸出格式:

共t行。對於每組資料,存在負環則輸出一行"ye5"(不含引號),否則輸出一行"n0"(不含引號)。

輸入樣例#1:

2

3 41 2 2

1 3 4

2 3 1

3 1 -3

3 31 2 3

2 3 4

3 1 -8

輸出樣例#1:

n0

ye5

n\leq 2000n≤2000m\leq 3000m≤3000-10000\leq w\leq 10000−10000≤w≤10000t\leq 10t≤10

建議複製輸出格式中的字串。

//

t到mmp...實測inf不設成10000絕對t

//深吸一口氧

#include#include

#include

#include

#include

#include

using

namespace

std;

const

int inf = 10000

;const

int maxn = 30000

;int

t, n, m;

int head[maxn * 2], tot = 0

;int

dis[maxn], cnt[maxn];

bool

vis[maxn];

struct

edge e[maxn];

inline

intread()

while (isdigit(ch))

return num *f;

}void add_edge(int x, int y, int

w)

bool spfa(int

s) }} }

return

false;}

intmain()

if(spfa(1)) printf("

ye5\n");

else printf("

n0\n");

}return0;

}

Luogu P3385 模板 負環

暴力列舉 spfa bellman ford 奇怪的貪心 超神搜尋 輸入格式 第一行乙個正整數t表示資料組數,對於每組資料 第一行兩個正整數n m,表示圖有n個頂點,m條邊 接下來m行,每行三個整數a b w,表示a b有一條權值為w的邊 若w 0則為單向,否則雙向 輸出格式 共t行。對於每組資料,...

luogu P3385 模板 負環

給定乙個 n 個點的有向圖,請求出圖中是否存在從頂點 1 出發能到達的負環。負環的定義是 一條邊權之和為負數的迴路。這個就是 luogu 上正常的判斷負環,那我們就按這 spfa 判斷負環的方法做就可以了。include include include using namespace std str...

Luogu P3385 模板 負環

暴力列舉 spfa bellman ford 奇怪的貪心 超神搜尋 輸入格式 第一行乙個正整數t表示資料組數,對於每組資料 第一行兩個正整數n m,表示圖有n個頂點,m條邊 接下來m行,每行三個整數a b w,表示a b有一條權值為w的邊 若w 0則為單向,否則雙向 輸出格式 共t行。對於每組資料,...