| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:原创:nlog(n)算法In Reply To:原创:nlog(n)算法 Posted by:cavatina2016 at 2017-09-28 23:33:53 #include <stdio.h>
#include <queue>
using namespace std;
#define MAX_N 100
int n, m;
int weights[MAX_N + 1][MAX_N + 1];
int edgecounts[MAX_N + 1];
int edges[MAX_N + 1][MAX_N + 1];
int childcounts[2][MAX_N + 1];
int children[2][MAX_N + 1][MAX_N + 1];
bool states[MAX_N + 1];
struct Less
{
bool operator()(const pair<int, int>& a, const pair<int, int>& b)
{
if (weights[a.first][a.second] > weights[b.first][b.second]) return true;
if (weights[a.first][a.second] < weights[b.first][b.second]) return false;
return a < b;
}
};
int calc1()
{
memset(states, 0, sizeof(states));
memset(childcounts[0], 0, sizeof(childcounts[0]));
priority_queue<pair<int, int>, vector<pair<int, int> >, Less> q;
int s = 1;
states[s] = true;
for (int i = 0; i < edgecounts[s]; ++i) {
q.push(make_pair(s, edges[s][i]));
}
int cost = 0;
for (int i = 0; i < n - 1; ++i) {
bool found = false;
while (!q.empty()) {
pair<int, int> p = q.top();
q.pop();
int x = p.first, y = p.second;
if (states[x] && states[y]) {
continue;
}
else if (states[x]) {
s = y;
children[0][x][childcounts[0][x]++] = y;
}
else {
s = x;
children[0][y][childcounts[0][y]++] = x;
}
cost += weights[p.first][p.second];
found = true;
break;
}
if (!found) {
return INT_MAX;
}
states[s] = true;
for (int j = 0; j < edgecounts[s]; ++j) {
q.push(make_pair(s, edges[s][j]));
}
}
return cost;
}
int calc2()
{
memset(states, 0, sizeof(states));
memset(childcounts[1], 0, sizeof(childcounts[1]));
priority_queue<pair<int, int>, vector<pair<int, int> >, Less> q;
int s = 1;
states[s] = true;
for (int i = 0; i < edgecounts[s]; ++i) {
q.push(make_pair(s, edges[s][i]));
}
int buffer[MAX_N + 1];
int buflen = 0;
int prev = 0;
bool first = true;
for (int i = 0; i < n - 1; ) {
buflen = 0;
prev = 0;
first = true;
while (!q.empty()) {
pair<int, int> p = q.top();
int x = p.first, y = p.second;
if (!first && weights[x][y] != prev) {
break;
}
q.pop();
if (states[x] && states[y]) {
continue;
}
else if (states[x]) {
s = y;
children[1][x][childcounts[1][x]++] = y;
}
else {
s = x;
children[1][y][childcounts[1][y]++] = x;
}
buffer[buflen++] = s;
prev = weights[x][y];
first = false;
}
if (!buflen) {
return INT_MAX;
}
for (int k = 0; k < buflen; ++k) {
int s = buffer[k];
if (states[s]) {
return INT_MAX;
}
states[s] = true;
for (int j = 0; j < edgecounts[s]; ++j) {
q.push(make_pair(s, edges[s][j]));
}
}
i += buflen;
}
return 0;
}
bool compare(int node)
{
if (childcounts[0][node] != childcounts[1][node]) {
return false;
}
sort(children[0][node], children[0][node] + childcounts[0][node]);
sort(children[1][node], children[1][node] + childcounts[1][node]);
for (int i = 0; i < childcounts[0][node]; ++i) {
if (children[0][node][i] != children[1][node][i]) {
return false;
}
if (!compare(children[0][node][i])) {
return false;
}
}
return true;
}
int calc()
{
int ret1 = calc1();
int ret2 = calc2();
if (ret1 == INT_MAX || ret2 == INT_MAX) {
return INT_MAX;
}
if (!compare(1)) {
return INT_MAX;
}
return ret1;
}
int main()
{
int t; scanf_s("%d", &t);
for (int i = 0; i < t; ++i) {
scanf_s("%d %d", &n, &m);
memset(edgecounts, 0, sizeof(edgecounts));
for (int j = 0; j < m; ++j) {
int x, y, w; scanf_s("%d %d %d", &x, &y, &w);
weights[x][y] = weights[y][x] = w;
edges[x][edgecounts[x]++] = y;
edges[y][edgecounts[y]++] = x;
}
int res = calc();
if (res != INT_MAX) {
printf("%d\n", res);
}
else {
printf("Not Unique!\n");
}
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator