problemscpp
A collection of my answers to algorithm problems in c++.
函数
acwing::acwing852 命名空间参考

  1. spfa判断负环
更多...

函数

int main (istream &cin, ostream &cout)
 
bool spfa (const vector< unordered_map< int, int > > &g, int, int n)
 
 TEST (acwing852, case1)
 
 TEST (acwing852, case2)
 

详细描述

  1. spfa判断负环

函数说明

◆ main()

int acwing::acwing852::main ( istream &  cin,
ostream &  cout 
)

在文件 acwing.cpp7281 行定义.

7281 {
7282 int n;
7283 int m;
7284 cin >> n >> m;
7285 vector<unordered_map<int, int>> g(n + 1);
7286 vector cnt(n + 1, 0);
7287 vector shortest(n + 1, 0x3f3f3f);
7288 for(int i = 0; i < m; i++) {
7289 int x;
7290 int y;
7291 int z;
7292 cin >> x >> y >> z;
7293 if(g[x][y] == 0) {
7294 g[x][y] = z;
7295 } else {
7296 g[x][y] = min(g[x][y], z);
7297 }
7298 }
7299 for(int i = 1; i <= n; i++) {
7300 g[0][i] = 0;
7301 }
7302 if(spfa(g, m, n)) {
7303 cout << "Yes";
7304 } else {
7305 cout << "No";
7306 }
7307 return 0;
7308 }
bool spfa(const vector< unordered_map< int, int > > &g, int, int n)
Definition: acwing.cpp:7310

引用了 spfa().

被这些函数引用 TEST().

◆ spfa()

bool acwing::acwing852::spfa ( const vector< unordered_map< int, int > > &  g,
int  ,
int  n 
)

在文件 acwing.cpp7310 行定义.

7310 {
7311 vector cnt(n + 1, 0);
7312 vector shortest(n + 1, 0x3f3f3f);
7313 queue<int> q;
7314 unordered_set<int> us;
7315 q.push(0);
7316 us.insert(0);
7317 shortest[0] = 0;
7318 while(!q.empty()) {
7319 int node = q.front();
7320 q.pop();
7321 us.erase(node);
7322 for(auto [next_node, d]: g[node]) {
7323 if(shortest[next_node] > shortest[node] + d) {
7324 shortest[next_node] = shortest[node] + d;
7325 cnt[next_node] = cnt[node] + 1;
7326 if(cnt[next_node] >= n + 1) {
7327 return true;
7328 }
7329 if(!us.contains(next_node)) {
7330 q.push(next_node);
7331 us.insert(next_node);
7332 }
7333 }
7334 }
7335 }
7336 return false;
7337 }

被这些函数引用 main().

◆ TEST() [1/2]

acwing::acwing852::TEST ( acwing852  ,
case1   
)

在文件 acwing_test.cpp3412 行定义.

3412 {
3413 istringstream in("3 3\n"
3414 "1 2 -1\n"
3415 "2 3 4\n"
3416 "3 1 -4");
3417 auto out = ostringstream();
3418 main(in, out);
3419 const auto ans = out.str();
3420 ASSERT_EQ("Yes", ans);
3421 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().

◆ TEST() [2/2]

acwing::acwing852::TEST ( acwing852  ,
case2   
)

在文件 acwing_test.cpp3423 行定义.

3423 {
3424 istringstream in("5 4\n"
3425 "1 2 1\n"
3426 "3 4 -1\n"
3427 "4 5 2\n"
3428 "5 3 -4");
3429 auto out = ostringstream();
3430 main(in, out);
3431 const auto ans = out.str();
3432 ASSERT_EQ("Yes", ans);
3433 }

引用了 main().