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

  1. spfa求最短路
更多...

函数

int main (istream &cin, ostream &cout)
 
 TEST (acwing851, case1)
 

详细描述

  1. spfa求最短路

函数说明

◆ main()

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

在文件 acwing.cpp7232 行定义.

7232 {
7233 int n;
7234 int m;
7235 cin >> n >> m;
7236 vector<unordered_map<int, int>> g(n + 1);
7237 vector reachable(n + 1, false);
7238 vector shortest(n + 1, 0x3f3f3f);
7239 for(int i = 0; i < m; i++) {
7240 int x;
7241 int y;
7242 int z;
7243 cin >> x >> y >> z;
7244 if(g[x][y] == 0) {
7245 g[x][y] = z;
7246 } else {
7247 g[x][y] = min(g[x][y], z);
7248 }
7249 }
7250 queue<int> q;
7251 unordered_set<int> us;
7252 us.insert(1);
7253 q.push(1);
7254 shortest[1] = 0;
7255 reachable[1] = true;
7256 while(!q.empty()) {
7257 int node = q.front();
7258 q.pop();
7259 us.erase(node);
7260 for(auto [next_node, d]: g[node]) {
7261 reachable[next_node] = true;
7262 if(shortest[next_node] > shortest[node] + d) {
7263 shortest[next_node] = shortest[node] + d;
7264 if(!us.contains(next_node)) {
7265 q.push(next_node);
7266 us.insert(next_node);
7267 }
7268 }
7269 }
7270 }
7271 if(!reachable[n]) {
7272 cout << "impossible";
7273 } else {
7274 cout << shortest[n];
7275 }
7276 return 0;
7277 }

被这些函数引用 TEST().

◆ TEST()

acwing::acwing851::TEST ( acwing851  ,
case1   
)

在文件 acwing_test.cpp3399 行定义.

3399 {
3400 istringstream in("3 3\n"
3401 "1 2 5\n"
3402 "2 3 -3\n"
3403 "1 3 4");
3404 auto out = ostringstream();
3405 main(in, out);
3406 const auto ans = out.str();
3407 ASSERT_EQ("2", ans);
3408 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().