-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
61-tgyuuAn #212
61-tgyuuAn #212
Conversation
์คํธ ์ด๋ฒ์ ํ๊ต ๊ฐ์ ์ค ์๊ณ ๋ฆฌ์ฆ (์ฌ)์๊ฐ์ ํ๊ฒ ๋์ด์ ์ ๋์จ ํ์ธ๋ ์๊ณ ๋ฆฌ์ฆ์ ๋ฐฐ์ฐ๊ณ C์ธ์ด๋ก ๊ตฌํ์ ํ์๋๋ฐ C์ธ์ด ์ ๋์จ ํ์ธ๋#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_NODE 1000
#define MAX_EDGES 10000
typedef struct {
int start;
int end;
int weight;
} Edge;
int adjacency_matrix[MAX_NODE][MAX_NODE];
int result_matrix[MAX_NODE][MAX_NODE];
Edge edges[MAX_EDGES]; // ๊ฐ์ ๋ฐฐ์ด
Edge result_edge[MAX_EDGES];
int parent[MAX_NODE];
int size[MAX_NODE];
int node_count = 0;
int edge_count = 0; // ํ์ฌ ๊ฐ์ ์ ๊ฐ์
int result_count = 0; // ํ์ฌ ๊ฐ์ ์ ๊ฐ์
// ํ์ผ input ๋ฐ ๊ฐ์ ๋ง๋ค๊ธฐ
int file_input(char *argv[]){
FILE* fp = fopen(argv[1], "r");
int max_node = 0;
if (fp == NULL) {
printf("File Not Found\n");
exit(1);
}
char buffer[MAX_NODE];
// ํ์ผ ์ฝ์ด์ค๊ธฐ
while (fgets(buffer, MAX_NODE, fp)) {
char* p = strtok(buffer, " ");
int col = 0;
while (p) {
adjacency_matrix[node_count][col++] = atoi(p);
p = strtok(NULL, " ");
}
node_count++;
}
return 1;
}
// ์ ๋ ฌ ํจ์
int static compare (const void* first, const void* second)
{
if (((Edge *)first)->weight > ((Edge *)second)->weight)
return 1;
else if (((Edge *)first)->weight < ((Edge *)second)->weight)
return -1;
else
return 0;
}
void edge_setting(){
// ํ์ผ ์ฝ์ด์จ ๋ถ๋ถ Edge๋ก ๋ง๋ค์ด์ฃผ๊ธฐ
for(int i = 0; i < node_count; ++i) {
for(int j = i + 1; j < node_count; ++j) {
if(adjacency_matrix[i][j] == -1){
continue;
}
edges[edge_count].start = i;
edges[edge_count].end = j;
edges[edge_count].weight = adjacency_matrix[i][j];
edge_count++;
}
}
// weight๋ณ๋ก ์ ๋ ฌํด์ฃผ๊ธฐ
qsort(edges, edge_count, sizeof(Edge), compare);
// // ์ ๋ ฌํ ๊ฐ์ ์ถ๋ ฅํ๊ธฐ
// for (int i = 0; i < edge_count; ++i) {
// printf("๊ฐ์ %d: start=%d, end=%d, weight=%d\n", i+1, edges[i].start, edges[i].end, edges[i].weight);
// }
for(int i = 0; i < edge_count; i++){
parent[i] = i;
}
}
// ๋ถ๋ชจ ์ฐพ๊ธฐ
int find(int node){
while(node != parent[node]){
parent[node] = parent[parent[node]];
node = parent[node];
}
return parent[node];
}
// cycle ์ธ์ง ํ์ธํ๊ธฐ(๋ถ๋ชจ๊ฐ ๊ฐ์์ง)
int is_cycle(int node1, int node2){
if (find(node1) == find(node2)){
return 1;
} else {
return 0;
}
}
// union , find
void unite(int node1, int node2){
int parent1 = find(node1);
int parent2 = find(node2);
// ํ์ชฝ์ ๋ชฐ๋ฆฌ์ง ์๊ธฐ ์ํด dept ์ฌ์ฉ
if(size[parent1] > size[parent2]){
parent[parent2] = parent1;
size[parent1] += size[parent2];
} else {
parent[parent1] = parent2;
size[parent2] += size[parent1];
}
}
// krusckal ์คํ(๋ชจ๋ ์คํํ๋ฉด์ ์ฌ์ดํด์ธ์ง ํ์ธํ๊ธฐ)
void krusckal(){
for(int i = 0; i < edge_count; i++){
int start = edges[i].start;
int end = edges[i].end;
int weight = edges[i].weight;
if(!is_cycle(start,end)){
unite(start, end);
result_edge[result_count++] = edges[i];
}
}
}
// ๊ฒฐ๊ณผ ์ถ๋ ฅ ํจ์
void result_print(){
// ๋ฉํธ๋ฆญ์ค ๋ง๋ค๊ธฐ
memset(result_matrix, -1, sizeof(result_matrix));
for (int i = 0; i < node_count; i++){
result_matrix[i][i] = 0;
}
for (int i = 0; i < result_count; i++){
int start = result_edge[i].start;
int end = result_edge[i].end;
result_matrix[start][end] = result_edge[i].weight;
result_matrix[end][start] = result_edge[i].weight;
}
for(int i = 0; i < node_count; i++){
printf("\t%c", 'A' + i);
}
printf("\n");
for(int i = 0; i < node_count; i++){
printf("%c ", 'A' + i); // ๋
ธ๋ ์ถ๋ ฅ
for(int j = 0; j < node_count; j++){
printf("\t%d", result_matrix[i][j]);
}
printf("\n");
}
}
int main(int argc, char *argv[]) {
file_input(argv);
edge_setting();
krusckal();
// // krusckal๋ก ๋ง๋ค์ด์ง ๊ฐ์ ์ถ๋ ฅํ๊ธฐ
// for (int i = 0; i < result_count; ++i) {
// printf("๊ฐ์ %d: start=%d, end=%d, weight=%d\n", i+1, result_edge[i].start, result_edge[i].end, result_edge[i].weight);
// }
result_print();
return 0;
} ์์ง ํ์ด์ฌ์ผ๋ก ์ ๋์จํ์ธ๋ ์์ฑ์ ํด๋ณธ ์ ์ ์๋๋ฐ ๋ฐ๋ก ๋ฐ์์ง ์๊ฒ ์๊ฐ์ด ๋๋ฉด(์๋ง ๋ค์์ฃผ์ฏค..)์ ๋ ธํธ๋ ์ ๋ฆฌ๋ฅผ ํ๋ฒ ํด์ผ๊ฒ ๊ตฐ์ ์์ด๋ฒ๋ฆฌ์ง ์๊ฒ!! |
def calculate_distance(first, second): | ||
return (abs(first[0] - second[0])**2 + abs(first[1] - second[1])**2)**0.5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด ๋ถ๋ถ์ ์ขํ๋ค์ ์ ๊ณฑ ํฉ์ ๊ตฌํ๊ณ ๋ฃจํธ ์์์ ํผํ๊ณ ๋ผ์ค ์ ๋ฆฌ ์ด์ฉํด์ ๋์ ์ฌ์ด์ ๊ฑฐ๋ฆฌ ๊ตฌํ๋ ๋ฐฉ๋ฒ์ธ๊ฐ์???
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ง์๋๋ค!!!!!!!!!!!!!!!!!!!!!!!
๐ ๋ฌธ์ ๋งํฌ
์ฐ์ฃผ์ ๊ณผ์ ๊ต๊ฐ
โ๏ธ ์์๋ ์๊ฐ
30๋ถ
โจ ์๋ ์ฝ๋
๋์ค์ฝ๋ ์ถ์ฒ๋ฌธ์ ์ฑ๋ ์์ ๋์๋
๋ณ์๋ฆฌ ๊ทธ๋ฆฌ๊ธฐ ๋ฌธ์ ๋ ๋งค์ฐ ๋งค์ฐ ๊ฐ์ต๋๋ค!
์ด.. ์ด ๋ฌธ์ ๋ฅผ ํธ๋ ค๋ฉด ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ์ ๋ํด์ ์์์ผ ํ๋๋ฐ์..
์ต์ ์คํจ๋ ํธ๋ฆฌ๋ผ๊ณ ๋ถ๋ฅด๊ธฐ๋ ํด์..
๊ทผ๋ฐ ์ ์๊ณ ๋ฆฌ์ฆ์ ์๋ ค๊ณ ํ๋ค๋ฉด ์ ๋์จ ํ์ธ๋ ์๊ณ ๋ฆฌ์ฆ์ ๋ํด์ ์์์ผ ํด์...
ํ ... ์กฐ๋ง๊ฐ ์๊ณ ๋ฆฌ์ฆ ๋ ธํธ ํ๋ ๋ง๋ค๊ฒ ์ต๋๋ค...........!
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ