#include <iostream>
#include<string>
#include<vector>
using namespace std;
struct link{
string node1;
string node2;
int weighted;
};
void split(string, vector<link>*);
int count_node(vector<link>*, vector<string>*);
int main()
{
vector<link>input; //原始input
vector<string>n_list; //node的list
vector<string>new_n_list; //node的list
vector<link>Candidate;
vector<link>answer; //最後要輸出的結果
string command; //每一行輸入
int size, edge_count = 0, node_count;
while (true)
{
getline(cin, command);
if (command == "exit")
break;
split(command, &input);
}
node_count = count_node(&input, &n_list); //算有幾個點 & 做出點的list
/////prim//////
int total_weighted = 0;
link tmp;
new_n_list.push_back(n_list[0]); //以第一個當作起點
n_list.erase(n_list.begin());
for (int i = 0; edge_count != (node_count - 1); i++)
{
string n1, n2;
for (int j = 0; j < new_n_list.size(); j++) //需要想法QAQ
{
n1 = new_n_list[j];
for (int k = 0; k < n_list.size(); k++)
{
n2 = n_list[k];
for (int l = 0; l < input.size(); l++)
{
if (n1==input[l].node1 && n2==input[l].node2)
{
Candidate.push_back(input[l]);
}
else if (n1==input[l].node2 && n2==input[l].node1)
{
Candidate.push_back(input[l]);
}
}
}
}
tmp = Candidate[0];
for (int j = 0; j < Candidate.size(); j++) //挑最小的
{
if (tmp.weighted>Candidate[j].weighted)
tmp = Candidate[j];
}
answer.push_back(tmp);
edge_count++;
bool flag = 0; //scan node1 in or not in
for (int j = 0; j < new_n_list.size(); j++)
{
if (tmp.node1 == new_n_list[j])
{
flag = 1;
break;
}
}
if (!flag)
{
new_n_list.push_back(tmp.node1);
for (int j = 0; j < n_list.size(); j++) //刪除另一個set裡面的東西
{
if (tmp.node1 == n_list[j])
{
n_list.erase(n_list.begin() + j);
break;
}
}
}
flag = 0; //scan node2 in or not in
for (int j = 0; j < new_n_list.size(); j++)
{
if (tmp.node2 == new_n_list[j])
{
flag = 1;
break;
}
}
if (!flag)
{
new_n_list.push_back(tmp.node2);
for (int j = 0; j < n_list.size(); j++)
{
if (tmp.node2 == n_list[j])
{
n_list.erase(n_list.begin() + j);
break;
}
}
}
for (int j = 0; j < input.size(); j++)
{
if ((tmp.node1 == input[j].node1) && (tmp.node2 == input[j].node2) && (tmp.weighted == input[j].weighted))
{
input.erase(input.begin() + j);
break;
}
}
Candidate.clear();
}
size = answer.size(); //印答案
for (int i = 0; i < size; i++)
{
cout << answer[i].node1 << "-" << answer[i].node2 << " " << answer[i].weighted << endl;
total_weighted = total_weighted + answer[i].weighted;
}
cout << "total weighted = " << total_weighted << endl;
return 0;
}
void split(string input, vector<link>* data)
{
int dash_pos, space_pos;
int str_len = input.length();
link push_back;
string tmp = "";
for (int i = 0; i < str_len; i++) //找 - 位置
{
if (input[i] == '-')
{
dash_pos = i;
break;
}
}
for (int i = dash_pos + 1; i < str_len; i++)//找 空白 位置
{
if (input[i] == ' ')
{
space_pos = i;
break;
}
}
for (int j = 0; j < dash_pos; j++)
tmp = tmp + input[j];
push_back.node1 = tmp;
tmp = "";
for (int j = dash_pos + 1; j < space_pos; j++)
tmp = tmp + input[j];
push_back.node2 = tmp;
tmp = "";
for (int j = space_pos + 1; j < str_len; j++)
tmp = tmp + input[j];
push_back.weighted = atoi(tmp.c_str());
data->push_back(push_back);
}
int count_node(vector<link>* data, vector<string>* node_set) //算點數 同時做出點的list
{
int count = 0;
int size = data->size();
int len;
for (int i = 0; i < size; i++)
{
link tmp = (*data)[i];
string node1 = tmp.node1;
string node2 = tmp.node2;
bool node1_flag = 0, node2_flag = 0;
len = node_set->size();
for (int j = 0; j < len; j++)
{
if (node1 == (*node_set)[j])
{
node1_flag = 1;
break;
}
}
if (node1_flag == 0)
{
(*node_set).push_back(node1);
count++;
}
len = (*node_set).size();
for (int k = 0; k < len; k++)
{
if (node2 == (*node_set)[k])
{
node2_flag = 1;
break;
}
}
if (node2_flag == 0)
{
(*node_set).push_back(node2);
count++;
}
}
return count;
}
沒有留言:
張貼留言