特殊最小生成树

时间:2018-10-14 06:37:17

标签: c++14

有一个节点只能获得一条线,我同时使用kruskal和prim,但是评委说TLE(超过时限)。 然后我将描述这个问题。有很多计算机,我们需要将它们全部连接起来,我们给出了不同计算机之间的连接成本,还给出了只能通过一条线连接的特殊数量的计算机。最后,我们保证有一个答案,没有自环,但是在同一节点之间可能有多个边具有不同的权重。 这是我的kruskal代码,是TLE。

#include <iostream>
#include <algorithm>

using namespace std;

typedef struct edge{
    int start;
    int end;
    int weight;
}Edge;

int special = 0;
int edgenum;
Edge _edge[600005];
int i, j, k;
int counter = 0;

bool Cmp(const edge &a, const edge &b){
    return a.weight < b.weight;
}

int getEnd(int vends[], int i){
    while(vends[i] != 0)
        i = vends[i];
    return i;
}

void kruskal(){
    int p1, p2, m, n, ans = 0;
    int vends[10005] = {0};
    sort(_edge, _edge+counter, Cmp);
    for(i = 0; i < edgenum; ++i){
        p1 = _edge[i].start;
        p2 = _edge[i].end;
        if ((p1 == k || p2 == k) && special)
            continue;

        m = getEnd(vends, p1);
        n = getEnd(vends, p2);

        if(m != n){
            if (p1 == k || p2 == k)
                special = 1;
            vends[m] = n;
            ans += _edge[i].weight;
        }
    }
    cout << ans << endl;
}

int main(){
    int n, m;
    cin >> n >> m >> k;
    edgenum = m;
    while(m--){
        int a, b, c;
        cin >> a >> b >> c;
        _edge[counter].start = a;   //Get the Edge
        _edge[counter].weight = c;
        _edge[counter++].end = b;
//        _edge[counter].start = b;
//        _edge[counter].weight = c;
//        _edge[counter++].end = a;
    }
    kruskal();
}

这是我的Prim,也是TLE:

#include <iostream>

using namespace std;

typedef char VertexType;

typedef struct node{
    int adjvex = 0;
    int weight = INT32_MAX;
    struct node *next = NULL;
}Node;

typedef struct vnode{
    VertexType data;
    Node *firstnode = NULL;
}Vnode;

Vnode node[10005];
int VNUM;
int n, m, k;
int lowcost[10005] = {0};
int addvnew[10005];  //未加入最小生成树表示为-1,加入则为0
int adjecent[10005] = {0};
bool is_special = false;
int flag;

void prim(int start){
    long long sumweight = 0;
    int i, j;
    Node *p = node[start].firstnode;

    for (i = 1; i <= VNUM; ++i) {   //重置
        addvnew[i] = -1;
    }

    while (p->next != NULL){
        if (lowcost[p->adjvex] == 0 || p->weight < lowcost[p->adjvex])
            lowcost[p->adjvex] = p->weight;
        p = p->next;
    }
    if (lowcost[p->adjvex] == 0 || p->weight < lowcost[p->adjvex])
        lowcost[p->adjvex] = p->weight;

    addvnew[start] = 0;
//    adjecent[start] = start;
    if (start == k) {
        is_special = true;
        flag = 1;
    }

    for (i = 1; i < VNUM; ++i) {
        int min = INT32_MAX;
        int v=-1;
        for (j = 1; j <= VNUM; ++j) {   //Find the min
            if (addvnew[j] == -1 && lowcost[j] < min && lowcost[j] != 0){
                min = lowcost[j];
                v = j;
            }
        }
        if (v != -1){   //if min is found
            if (flag == 1){
                for (int l = 0; l < 10005; ++l) {
                    lowcost[l] = 0;
                }
                flag = 0;
            }

            addvnew[v] = 0;

            sumweight += min;

            p = node[v].firstnode;
            while(p->next != NULL){
                if (is_special && p->adjvex == k){  //If find the special node
                    p = p->next;
                    continue;
                }
                if(addvnew[p->adjvex] == -1 && (lowcost[p->adjvex] == 0 || p->weight < lowcost[p->adjvex])){    //如果该点未连接
                    lowcost[p->adjvex] = p->weight;
                }
                p = p->next;
            }
            if (!(is_special && p->adjvex == k))
                if(addvnew[p->adjvex] == -1 && (lowcost[p->adjvex] == 0 || p->weight < lowcost[p->adjvex])){
                    lowcost[p->adjvex] = p->weight;
                }
        }
    }
    cout << sumweight << endl;
}

int main(){
    cin >> n >> m >> k;
    VNUM = n;
    while (m--){
        int a, b, c;
        cin >> a >> b >> c;
        Node *p = (Node*)malloc(sizeof(Node));
        p->adjvex = b;
        p->weight = c;
        p->next = node[a].firstnode;
        node[a].firstnode = p;
        Node *q = (Node*)malloc(sizeof(Node));
        q->adjvex = a;
        q->weight = c;
        q->next = node[b].firstnode;
        node[b].firstnode = q;
    }
    prim(k);
}

我不知道如何修改这两个代码,我尽力了,谢谢

0 个答案:

没有答案