1005번 - ACM Craft
#include <iostream>
#include <algorithm>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef struct list_node* list_pointer;
typedef struct list_node {
int data;
list_pointer link;
};
int indegree[1001];
int main(void)
{
int t;
int n;
int k;
char c;
int x;
int number;
int d1;
int d2;
int pop;
int find;
int answer = -987654321;
int sum;
queue <pair<int,int>> qe;
vector<int> vt;
list_node topological_Sort[1001];
cin >> t;
getchar();
for (int i = 0; i < t; i++)
cin >> n >> k;
number = 1;
while ((c = cin.get()) != '\n')
x = 0;
while (c == ' ')
cin.get(c);
while (c >= '0' && c <= '9')
x = 10 * x + (c - '0');
}
topological_Sort[number].data = x;
topological_Sort[number].link = NULL;
indegree[number++] = 0;
if (c == '\n')
break;
for (int i = 0; i < k; i++)
list_pointer a;
list_pointer temp = new list_node;
cin >> d1 >> d2;
a = &topological_Sort[d1];
++indegree[d2];
temp->link = NULL;
temp->data = d2;
while (a->link)
a = a->link;
a->link = temp;
cin >> find;
answer = -987654321;
for (int i = 1; i < n+1; i++)
if (!indegree[i])
qe.push(make_pair(i, 0));
while (!qe.empty())
sum = qe.front().second;
pop = qe.front().first;
if (pop == find)
answer = max(answer, sum + topological_Sort[pop].data);
qe.pop();
else
a = &topological_Sort[pop];
sum = topological_Sort[pop].data + sum;
qe.push(make_pair(a->data,sum));
vt.push_back(answer);
for (int i = 1; i < n + 1; i++)
list_pointer t;
a = &topological_Sort[i];
t = a->link;
while (t)
a = t->link;
delete t;
t = a;
for (int i = 0; i < vt.size(); i++)
cout << vt[i] << endl;
댓글을 작성하려면 로그인해야 합니다.
rlaehddus302 3년 전
#include <iostream>
#include <algorithm>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef struct list_node* list_pointer;
typedef struct list_node {
int data;
list_pointer link;
};
int indegree[1001];
int main(void)
{
int t;
int n;
int k;
char c;
int x;
int number;
int d1;
int d2;
int pop;
int find;
int answer = -987654321;
int sum;
queue <pair<int,int>> qe;
vector<int> vt;
list_node topological_Sort[1001];
cin >> t;
getchar();
for (int i = 0; i < t; i++)
{
cin >> n >> k;
getchar();
number = 1;
while ((c = cin.get()) != '\n')
{
x = 0;
while (c == ' ')
cin.get(c);
while (c >= '0' && c <= '9')
{
x = 10 * x + (c - '0');
cin.get(c);
}
topological_Sort[number].data = x;
topological_Sort[number].link = NULL;
indegree[number++] = 0;
if (c == '\n')
{
break;
}
}
for (int i = 0; i < k; i++)
{
list_pointer a;
list_pointer temp = new list_node;
cin >> d1 >> d2;
getchar();
a = &topological_Sort[d1];
++indegree[d2];
temp->link = NULL;
temp->data = d2;
while (a->link)
{
a = a->link;
}
a->link = temp;
}
cin >> find;
getchar();
answer = -987654321;
for (int i = 1; i < n+1; i++)
{
if (!indegree[i])
{
qe.push(make_pair(i, 0));
}
}
while (!qe.empty())
{
list_pointer a;
sum = qe.front().second;
pop = qe.front().first;
if (pop == find)
{
answer = max(answer, sum + topological_Sort[pop].data);
qe.pop();
}
else
{
a = &topological_Sort[pop];
while (a->link)
{
sum = qe.front().second;
a = a->link;
sum = topological_Sort[pop].data + sum;
qe.push(make_pair(a->data,sum));
}
qe.pop();
}
}
vt.push_back(answer);
for (int i = 1; i < n + 1; i++)
{
list_pointer a;
list_pointer t;
a = &topological_Sort[i];
t = a->link;
while (t)
{
a = t->link;
delete t;
t = a;
}
}
}
for (int i = 0; i < vt.size(); i++)
{
cout << vt[i] << endl;
}
}