C++ C++;:求循环边最小和的快速算法
我有一个无向加权图,想找到所有圈边的最小和。 这意味着如果我没有周期,答案是0。 如果我有一个循环,答案是该循环的最小边重量。 如果我有一个以上的循环,它是这些最小权重的总和 我实现的算法,使用了某种Prims算法。 我只添加最重的边,当形成一个循环时,权重将被求和为答案值 我认为它是正确的,因为我的所有测试用例都显示了正确的答案 但肯定有什么地方出错了,我就是找不到C++ C++;:求循环边最小和的快速算法,c++,algorithm,graph,cycle,depth-first-search,C++,Algorithm,Graph,Cycle,Depth First Search,我有一个无向加权图,想找到所有圈边的最小和。 这意味着如果我没有周期,答案是0。 如果我有一个循环,答案是该循环的最小边重量。 如果我有一个以上的循环,它是这些最小权重的总和 我实现的算法,使用了某种Prims算法。 我只添加最重的边,当形成一个循环时,权重将被求和为答案值 我认为它是正确的,因为我的所有测试用例都显示了正确的答案 但肯定有什么地方出错了,我就是找不到 struct connection { int a, b, cost; bool operator<(c
struct connection {
int a, b, cost;
bool operator<(const connection rhs) const {
return cost < rhs.cost || (cost == rhs.cost && (a < rhs.a || (a == rhs.a && b < rhs.b)));
}
};
int n, m, researchers; // Amount of vertices, edges
std::list<connection> *adj; // Array of adjancency lists
std::list<int> *used;
std::set<connection> priorityQ;
void addEdge(int v, int w, int cost) {
connection temp;
temp.a = v;
temp.b = w;
temp.cost = cost;
adj[v].push_back(temp);
temp.a = w;
temp.b = v;
adj[w].push_back(temp);
}
bool isUsed(int u, int v) {
for (std::list<int>::iterator it = used[u].begin(); it != used[u].end(); ++it) {
int te = *it;
if (te == v) return true;
}
return false;
}
void expand(int u) {
for (std::list<connection>::iterator it = adj[u].begin(); it != adj[u].end(); ++it) {
connection v = *it;
if (isUsed(u, v.b)) continue;
used[v.b].push_back(u);
used[u].push_back(v.b);
priorityQ.insert(v);
}
}
void PrimR(int u, bool added[]) {
added[u] = true;
expand(u);
}
// Prim algorithm
void Prim(int u, bool added[]) {
added[u] = true;
expand(u);
while (priorityQ.size() > 0) {
connection now = *priorityQ.rbegin();
priorityQ.erase(*priorityQ.rbegin());
if (added[now.b]) {
researchers += now.cost;
}
else {
PrimR(now.b, added);
}
}
}
int main()
{
int t;
// loop over all test cases
scanf("%d ", &t);
for (int i = 1; i <= t; i++) {
// read input nodes n, connections m
scanf("%d %d", &n, &m);
adj = new std::list<connection>[n];
//read connections and save them
for (int j = 0; j < m; j++) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
addEdge(a - 1, b - 1, c);
}
researchers = 0;
// Use of prim with heaviest edges first
bool *added = new bool[n];
used = new std::list<int>[n];
for (int j = 0; j < n; j++) {
added[j] = false;
}
for (int j = 0; j < n; j++) {
if (!added[j]) {
Prim(j, added);
}
}
// print desired output
printf("Case #%d: %d\n", i, researchers);
delete[] adj;
delete[] added;
delete[] used;
}
return 0;
}
struct连接{
INTA,b,成本;
布尔运算符(0){
立即连接=*priorityQ.rbegin();
priorityQ.erase(*priorityQ.rbegin());
如果(添加[now.b]){
研究人员+=现在成本;
}
否则{
PrimR(现在添加了.b);
}
}
}
int main()
{
int t;
//循环所有测试用例
scanf(“%d”、&t);
对于(int i=1;i我没有考虑,在两个节点之间可能有多个连接。
下面的代码解决了这个问题:
struct connection {
int a, b, cost, id;
bool operator<(const connection rhs) const {
return cost < rhs.cost || (cost == rhs.cost && id < rhs.id);
}
};
int n, m, researchers; // Amount of vertices, edges
std::list<connection> *adj; // Array of adjancency lists
std::set<connection> priorityQ;
void addEdge(int v, int w, int cost, int id) {
connection temp;
temp.a = v;
temp.b = w;
temp.cost = cost;
temp.id = id;
adj[v].push_back(temp);
temp.a = w;
temp.b = v;
adj[w].push_back(temp);
}
void deleteEdge(int v, int w, int id) {
for (std::list<connection>::iterator it = adj[v].begin(); it != adj[v].end(); ++it) {
if ((*it).id == id) {
adj[v].erase(it);
break;
}
}
for (std::list<connection>::iterator it = adj[w].begin(); it != adj[w].end(); ++it) {
if ((*it).id == id) {
adj[w].erase(it);
break;
}
}
}
void expand(int u) {
for (std::list<connection>::iterator it = adj[u].begin(); it != adj[u].end(); ++it) {
connection v;
v.a = (*it).a < (*it).b ? (*it).a : (*it).b;
v.b = (*it).a < (*it).b ? (*it).b : (*it).a;
v.cost = (*it).cost;
v.id = (*it).id;
priorityQ.insert(v);
}
}
void PrimR(int u, bool added[]) {
added[u] = true;
expand(u);
}
// Prim algorithm
void Prim(int u, bool added[]) {
added[u] = true;
expand(u);
while (priorityQ.size() > 0) {
connection now = *priorityQ.rbegin();
priorityQ.erase(*priorityQ.rbegin());
deleteEdge(now.a, now.b, now.id);
if (added[now.b] && added[now.a]) {
researchers += now.cost;
}
else if (added[now.b]) {
PrimR(now.a, added);
}
else if (added[now.a]) {
PrimR(now.b, added);
}
}
}
int main()
{
int t;
// loop over all test cases
scanf("%d ", &t);
for (int i = 1; i <= t; i++) {
// read input nodes n, connections m
scanf("%d %d", &n, &m);
adj = new std::list<connection>[n];
//read connections and save them
for (int j = 0; j < m; j++) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
addEdge(a - 1, b - 1, c, j);
}
researchers = 0;
// Use of prim with heaviest edges first
bool *added = new bool[n];
for (int j = 0; j < n; j++) {
added[j] = false;
}
for (int j = 0; j < n; j++) {
if (!added[j]) {
Prim(j, added);
}
}
// print desired output
printf("Case #%d: %d\n", i, researchers);
delete[] adj;
delete[] added;
}
return 0;
}
struct连接{
int a、b、成本、id;
布尔运算符(0){
立即连接=*priorityQ.rbegin();
priorityQ.erase(*priorityQ.rbegin());
deleteEdge(now.a、now.b、now.id);
if(添加了[now.b]&添加了[now.a]){
研究人员+=现在成本;
}
else if(添加[now.b]){
PrimR(现在添加了.a);
}
else if(添加[now.a]){
PrimR(现在添加了.b);
}
}
}
int main()
{
int t;
//循环所有测试用例
scanf(“%d”、&t);
对于(inti=1;i可以使用算法
Floyd Warshall算法可在所有顶点对之间找到最短路径
这个到(u,u)->(u,u)
的最短路径就是你在考虑了每个可能的顶点u
后找到的答案
算法的运行时间是O(n^3)
基本上,问题是找到循环,所以请参见Tarjan等。