USACO Section 3.1: Agri-Net

时间:2023-03-08 16:55:37

minimal spanning tree的经典题

 /*
 ID: yingzho1
 LANG: C++
 TASK: agrinet
 */
 #include <iostream>
 #include <fstream>
 #include <string>
 #include <map>
 #include <vector>
 #include <set>
 #include <algorithm>
 #include <stdio.h>
 #include <queue>
 #include <cstring>
 #include <cmath>
 #include <list>

 using namespace std;

 ifstream fin("agrinet.in");
 ofstream fout("agrinet.out");

 ;

 int N;
 ][];
 ];
 ;

 void prim() {
     ; i < N; i++) dis[i] = inf;
     ] = {false};
     ;
     int min_w;
     int flag;
     visit[s] = true;

     ; i < N; i++) {
         min_w = inf;
         ; j < N; j++) {
             if (!visit[j] && dis[j] > dist[s][j]) dis[j] = dist[s][j];
             if (!visit[j] && min_w > dis[j]) {
                 min_w = dis[j];
                 //cout << min_w << endl;
                 flag = j;
             }
         }
         s = flag;
         visit[s] = true;
         total_dist += min_w;
     }
 }

 int main()
 {
     fin >> N;
     ; i < N; i++) {
         ; j < N; j++) {
             fin >> dist[i][j];
         }
     }
     prim();
     fout << total_dist << endl;

     ;
 }