백준 알고리즘 2219번 : 보안 시스템 설치

링크

https://www.acmicpc.net/problem/2219

sol1) 플로이드-와샬

#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define fastio cin.tie(0)->sync_with_stdio(0)
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define sz(v) (int)(v).size()
#define all(v) v.begin(), v.end()
#define rall(v) (v).rbegin(), (v).rend()
#define compress(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define OOB(x, y) ((x) < 0 || (x) >= n || (y) < 0 || (y) >= m)
#define debug(x) cout << (#x) << ": " << (x) << '\n'
using namespace std;
using ll = long long;
using ull = unsigned long long;
using dbl = double;
using ldb = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vs = vector<string>;
using tii = tuple<int,int,int>;
template<typename T> using wector = vector<vector<T>>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const ll LNF = 1e18 + 7;

wector<int> dist(201,vector<int>(201,INF));

int main() {
	fastio;
  int n,m; cin >> n >> m;
  for(int i = 1; i <= n; i++) dist[i][i] = 0;
  for(int i = 0; i < m; i++){
    int a,b,c; cin >> a >> b >> c;
    dist[a][b] = min(dist[a][b], c);
    dist[b][a] = min(dist[b][a], c);
  }
  // floyd
  for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++){
    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
  }
  int mn = INF,ans = 0;
  for(int i = 1; i <= n; i++){
    int sum = 0;
    for(int j = 1; j <= n; j++){
      if(dist[i][j]==INF) break;
      sum += dist[i][j];
    }
    if(mn > sum){
      mn = sum;
      ans = i;
    }
  }
  cout << ans << "\n";
  return 0;
}

복습(2021-11-10)

#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define fastio cin.tie(0)->sync_with_stdio(0)
#define sz(v) (int)(v).size()
#define all(v) v.begin(), v.end()
#define rall(v) (v).rbegin(), (v).rend()
#define compress(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define OOB(x, y) ((x) < 0 || (x) >= n || (y) < 0 || (y) >= m)
#define debug(x) cout << (#x) << ": " << (x) << '\n'
using namespace std;
using ll = long long;
using ull = unsigned long long;
using dbl = double;
using ldb = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vs = vector<string>;
using tii = tuple<int,int,int>;
template<typename T> using wector = vector<vector<T>>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const ll LNF = 1e18 + 7;

wector<int> dist(201,vector<int>(201, INF));

int main() {
  fastio;
  int n,m; cin >> n >> m;
  for(int i = 1; i <= n; i++) dist[i][i] = 0;
  for(int i = 0; i < m; i++){
    int a,b,c; cin >> a >> b >> c;
    dist[a][b] = min(dist[a][b], c);
    dist[b][a] = min(dist[b][a], c);
  }
  // floyd
  for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++){
    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
  }
  int mn = INF,ans = 0;
  for(int i = 1; i <= n; i++){
    int cnt = 0;
    for(int j = 1; j <= n; j++){
      cnt += dist[i][j];
    }
    if(mn > cnt) ans = i,mn = cnt;
  }
  cout << ans << "\n";
  return 0;
}

좋은 웹페이지 즐겨찾기