【作業存檔】最小生成樹的練習

記,一次非常順利的作業。

說是順利,但其實也用了一個晚上,一邊聽公共關係學的課程,一邊參與小組討論,一邊磕磕絆絆地寫完了。當然沒有一次通過所有測試點,反而是例子都沒過,不過看了看代碼發現了只是粗心的鍋,自己的思路沒大問題。

改了一下後運行超時+一個點沒過,降低了時間複雜度後還是有一個點沒過,最後看了一下別人的作業也順利地改了過來。


目標是用函數實現,在一個圖中去掉一個點後,記錄下來其他點獲得最小生成樹需要將多少不在使用的邊連通的權重最大的那個點,已知一組邊的兩個頂點、權重、是否在使用。

等價關係算法可以直接實現非常簡單,主要是問題沒有考慮會有不能連通的情況,


下爲代碼

#include<stdio.h>
#include<stdlib.h>

#define INF 0x7fffffff

typedef struct Road road;//直接把邊存下來
struct Road{
    int V1,V2;
    int weight;
    int isUse;
};

int Init(road Graph1[],road Graph0[],int V,int E);
int FindCity(road Graph1[],road Graph0[], int finalV[], int V, int E1, int E0);

int Find(int V,int List[]);
void Union(int V1, int V2, int List[]);

int main(){
    int V, E, count = 0, i, E1;
    int *finalV;//因爲V的個數不確定,所以用了動態分配內存
    road *Graph1, *Graph0;
    
    scanf("%d%d",&V,&E);
    Graph1 = (road*)malloc(sizeof(struct Road)*E);//存放目前在用的道路
    Graph0 = (road*)malloc(sizeof(struct Road)*E);//存放目前不在用的道路
    finalV = (int*)malloc(sizeof(int)*V);
    
    E1 = Init(Graph1, Graph0, V, E);//初始化道路
    
    count = FindCity(Graph1, Graph0, finalV, V, E1, E- E1);//找到最需要保護的城市
    
    for(i = 0; i < count - 1; i++){
        printf("%d ", finalV[i]+1);
    }
    if(count == 0)
        printf("0");
    else
        printf("%d", finalV[i]+1);
    
    free(finalV);
    free(Graph1);
    free(Graph0);
    return 0;
}

int Init(road Graph1[],road Graph0[],int V,int E){
    int i, E1 = 0, E0 = 0;
    int tmpV1,tmpV2, tmpWeight,isUse;
    for(i = 0; i < E; i++){
        scanf("%d%d%d%d", &tmpV1, &tmpV2, &tmpWeight, &isUse);
        if(isUse){
            Graph1[E1].V1 = tmpV1;
            Graph1[E1].V2 = tmpV2;
            Graph1[E1].weight = tmpWeight;
            Graph1[E1].isUse = isUse;
            E1++;
        }
        else{
            Graph0[E0].V1 = tmpV1;
            Graph0[E0].V2 = tmpV2;
            Graph0[E0].weight = tmpWeight;
            Graph0[E0].isUse = isUse;
            E0++;
        }
    }
    return E1;
}


int Find(int V,int List[]){//找到和V等價的那一堆數的代表點
    int tmpV = List[V];
    if (tmpV < 0)
        return V;
    while(List[tmpV] > 0){
        List[V] = List[tmpV];
        V = List[V];
        tmpV = List[tmpV];
    }
    return tmpV;
}

void Union(int V1, int V2, int List[]){//將包含V1和V包含2的兩堆數合併
    int V1head = Find(V1, List), V2head = Find(V2,List);//先找到頂點
    if(List[V1head] > List[V2head]){//把size小的接到size大的上,代表點指向的內容爲-size
        List[V2head] += List[V1head];
        List[V1head] = V2head;
    }
    else{
        List[V1head] += List[V2head];
        List[V2head] = V1head;
    }
}


int FindCity(road Graph1[],road Graph0[], int finalV[], int V, int E1, int E0){
    int i, tmpV, tmpE, costSum = 0, maxCost = 0, City = V;
    int *ConnectList;//用來記錄聯通情況
    int count = 0;
    
    //初始化聯通情況
    ConnectList = (int*)malloc(sizeof(int)*V);
    
    for(tmpV = 0; tmpV < V; tmpV++){
        City = V - 1;//City用來判斷是否獲得最小生成樹
        for(i = 0; i < V; i++)
            ConnectList[i] = -1;
        costSum = 0;
        for(tmpE = 0; tmpE < E1; tmpE++){
            if((Graph1[tmpE].V1 != (tmpV + 1)) && (Graph1[tmpE].V2 != (tmpV + 1))){//在這裏出過錯,忘了把自己去掉
                if(Find(Graph1[tmpE].V1 - 1, ConnectList) != Find(Graph1[tmpE].V2 - 1,ConnectList)){
                    Union(Graph1[tmpE].V1 - 1, Graph1[tmpE].V2 - 1, ConnectList);
                    City--;
                }
            }
        }
        for(tmpE = 0; tmpE < E0; tmpE++){
            if((Graph0[tmpE].V1 != (tmpV + 1)) && (Graph0[tmpE].V2 != (tmpV + 1)))
                if(Find(Graph0[tmpE].V1 - 1, ConnectList) != Find(Graph0[tmpE].V2 - 1,ConnectList)){
                    Union(Graph0[tmpE].V1 - 1, Graph0[tmpE].V2 - 1, ConnectList);
                    costSum += Graph0[tmpE].weight;
                    City--;
                }
        }
        if(City > 1)
            costSum = INF;
    
        if(costSum > maxCost){
            maxCost = costSum;
            count = 0;
            finalV[count] = tmpV;
            count++;
        }
        else if(costSum &&costSum == maxCost){
            finalV[count] = tmpV;
            count++;
        }

        
    }
    free(ConnectList);
        return count;
    
}

Battle Over Cities

It is vitally important to have all the cities connected by highways in a war. If a city is conquered by the enemy, all the highways from/toward that city will be closed. To keep the rest of the cities connected, we must repair some highways with the minimum cost. On the other hand, if losing a city will cost us too much to rebuild the connection, we must pay more attention to that city.

Given the map of cities which have all the destroyed and remaining highways marked, you are supposed to point out the city to which we must pay the most attention.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N (\le500), and M, which are the total number of cities, and the number of highways, respectively. Then M lines follow, each describes a highway by 4 integers: City1 City2 Cost Status where City1 and City2 are the numbers of the cities the highway connects (the cities are numbered from 1 to N), Cost is the effort taken to repair that highway if necessary, and Status is either 0, meaning that highway is destroyed, or 1, meaning that highway is in use.

Note: It is guaranteed that the whole country was connected before the war.

Output Specification:

For each test case, just print in a line the city we must protest the most, that is, it will take us the maximum effort to rebuild the connection if that city is conquered by the enemy.

In case there is more than one city to be printed, output them in increasing order of the city numbers, separated by one space, but no extra space at the end of the line. In case there is no need to repair any highway at all, simply output 0.

Sample Input 1:

4 5
1 2 1 1
1 3 1 1
2 3 1 0
2 4 1 1
3 4 1 0

Sample Output 1:

1 2

Sample Input 2:

4 5
1 2 1 1
1 3 1 1
2 3 1 0
2 4 1 1
3 4 2 1

Sample Output 2:

0


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章