Skip to content

Instantly share code, notes, and snippets.

@moo1o
Last active April 14, 2017 09:05
Show Gist options
  • Save moo1o/0698f3c1db6e37ee86502d8ffc0a5c94 to your computer and use it in GitHub Desktop.
Save moo1o/0698f3c1db6e37ee86502d8ffc0a5c94 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
int main(void) {
int n, ans, sum = 0;
cin >> n;
vector<int> d(n);
int w[20][20];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &w[i][j]);
}
}
for (int i = 0; i < n; i++) {
d[i] = i;
}
ans = 9999999;
do {
bool ok = true;
sum = 0;
for (int i = 0; i < n - 1; i++) {
if (w[d[i]][d[i + 1]] == 0)
ok = false;
else {
sum += w[d[i]][d[i + 1]];
}
}
if (ok == true && w[d[n - 1]][d[0]] != 0) {
sum += w[d[n - 1]][d[0]];
if (ans > sum) ans = sum;
}
} while (next_permutation(d.begin(), d.end()));
printf("%d\n", ans);
return 0;
}
//
// 2156.cpp
// AlgorithmTest
//
// Created by 김무열 on 2017. 4. 14..
// Copyright © 2017년 김무열. All rights reserved.
//
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> d(100001);
vector<int> g(100001);
int m = 0;
int sum;
void calc(int n, int index, int cnt){
if(index == 0){
sum = g[index];
sum += g[index+1];
calc(n, index+1, cnt+1);
sum = g[index+1];
sum += g[index+2];
calc(n, index+2, cnt+1);
}
else{
if(cnt == 3){
if(index +2 < n) {
sum += g[index+2];
calc(n, index+2, 0);
}
}
if(index >= n){
if(sum > m){
m = sum;
return;
}
}
else{
sum += g[index+1];
calc(n, index+1, cnt +1);
}
}
}
int main(void){
int n;
cin >> n;
for(int i = 0 ; i<n; i++){
cin >> g[i];
}
return 0;
}
//
// 9465.cpp
// AlgorithmTest
//
// Created by 김무열 on 2017. 4. 14..
// Copyright © 2017년 김무열. All rights reserved.
//
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
vector<int> row1(n);
vector<int> row2(n);
for(int i=0 ; i<2; i++){
for(int j=0 ; j<n ; j++){
if(j==0)
cin >> row1[j];
else
cin >> row2[j];
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment