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

  1. Prim算法求最小生成树
更多...

函数

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

详细描述

  1. Prim算法求最小生成树

函数说明

◆ main()

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

在文件 acwing.cpp7379 行定义.

7379 {
7380 unsigned n;
7381 unsigned m;
7382 cin >> n >> m;
7383 vector g(n + 1, vector(n + 1, INT_MAX));
7384 vector dist(n + 1, INT_MAX);
7385 for(unsigned i = 0; i < m; i++) {
7386 int u;
7387 int v;
7388 int w;
7389 cin >> u >> v >> w;
7390 w = min(w, g[u][v]);
7391 w = min(w, g[v][u]);
7392 g[u][v] = w;
7393 g[v][u] = w;
7394 }
7395 unordered_set<int> us;
7396 int ans = 0;
7397 for(int i = 0; i < n; i++) {
7398 int t = -1;
7399 for(int j = 1; j <= n; j++) {
7400 if(!us.contains(j) && (t == -1 || dist[t] > dist[j])) {
7401 t = j;
7402 }
7403 }
7404 if(i > 0 && dist[t] == INT_MAX) {
7405 cout << "impossible";
7406 return 0;
7407 }
7408 if(i > 0) {
7409 ans += dist[t];
7410 }
7411 for(int j = 1; j <= n; j++) {
7412 dist[j] = min(dist[j], g[t][j]);
7413 }
7414 us.insert(t);
7415 }
7416 cout << ans;
7417 return 0;
7418 }

被这些函数引用 TEST().

◆ TEST() [1/2]

acwing::acwing858::TEST ( acwing858  ,
case1   
)

在文件 acwing_test.cpp3502 行定义.

3502 {
3503 istringstream in("4 5\n"
3504 "1 2 1\n"
3505 "1 3 2\n"
3506 "1 4 3\n"
3507 "2 3 2\n"
3508 "3 4 4");
3509 auto out = ostringstream();
3510 main(in, out);
3511 const auto ans = out.str();
3512 ASSERT_EQ("6", ans);
3513 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().

◆ TEST() [2/2]

acwing::acwing858::TEST ( acwing858  ,
case2   
)

在文件 acwing_test.cpp3515 行定义.

3515 {
3516 istringstream in("5 10\n"
3517 "1 2 8\n"
3518 "2 2 7\n"
3519 "2 1 1\n"
3520 "3 4 3\n"
3521 "4 4 -10\n"
3522 "1 3 -9\n"
3523 "5 2 -4\n"
3524 "3 1 0\n"
3525 "1 4 8\n"
3526 "4 4 7");
3527 auto out = ostringstream();
3528 main(in, out);
3529 const auto ans = out.str();
3530 ASSERT_EQ("-9", ans);
3531 }

引用了 main().