U E D R , A S I H C RSS

1R/2016_10_14 (rev. 1.1)

1R/2016_10_14



1. 오늘의 문제

2. 참가자

  • 15이원준

3. 코드

3.1. 15이원준

#include<iostream>
#include<vector>
#include<utility>
using namespace std;

int d[501] = {0,};
int check[501] = {0,};

vector<vector<pair<int, int> > > vec;


int main(){
  vec.resize(501);

  int n,m;
  cin>>n>>m;
  for(int i=0; i<m; i++){
    int tmp1, tmp2, tmp3;
    scanf("%d %d %d", &tmp1, &tmp2, &tmp3);
    vec[tmp1].push_back(make_pair(tmp2,tmp3));
  }

  d[1] = 0;
  check[1] = 1;
  bool update = true;

  for(int i = 0; i<=n && update; i++){
    if(i == n){
      cout<<-1 <<endl;
      return 0;
    }
    update = false;
    for(int j = 1; j<=n; j++){
      if(!check[j]){
        continue;
      }
      for(auto it = vec[j].begin(); it != vec[j].end(); it++){
        if( !check[it->first] || d[it->first] > d[j] + it->second){
          update = true;
          check[it->first] = 1;
          d[it->first] = d[j] + it->second;
        }
      }
    }
  }
  for(int i = 2; i<=n; i++){
    if(!check[i]){
      cout<<-1<<endl;
    }
    else{
      cout<<d[i]<<endl;
    }
  }
}

4. 아이디어

4.1. 15이원준

  • 벨만 포드(이하생략)
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:08
Processing time 0.0188 sec