コード例 #1
0
ファイル: Kruskal.cpp プロジェクト: shubhpy/Cpp-Programs
// Driver program to test above functions
int main()
{
    /* Let us create following weighted graph
             10
        0--------1
        |  \     |
       6|   5\   |15
        |      \ |
        2--------3
            4       */
    int V = 4;  // Number of vertices in graph
    int E = 5;  // Number of edges in graph
    struct Graph* graph = createGraph(V, E);


    // add edge 0-1
    graph->edge[0].src = 0;
    graph->edge[0].dest = 1;
    graph->edge[0].weight = 10;

    // add edge 0-2
    graph->edge[1].src = 0;
    graph->edge[1].dest = 2;
    graph->edge[1].weight = 6;

    // add edge 0-3
    graph->edge[2].src = 0;
    graph->edge[2].dest = 3;
    graph->edge[2].weight = 5;

    // add edge 1-3
    graph->edge[3].src = 1;
    graph->edge[3].dest = 3;
    graph->edge[3].weight = 15;

    // add edge 2-3
    graph->edge[4].src = 2;
    graph->edge[4].dest = 3;
    graph->edge[4].weight = 4;

    KruskalMST(graph);

    return 0;
}
コード例 #2
0
int main()
{
    int E,V,K,i;
    int found=0;

    scanf("%d %d %d\n", &V, &E, &K);

    struct Graph* graph = createGraph(V, E);
    
    for (i=0; i<E; i++){
        scanf("%d %d %lld\n", &graph->edge[i].src, &graph->edge[i].dest, &graph->edge[i].weight);
        graph->edge[i].src--; graph->edge[i].dest--;
    }
    
    KruskalMST(graph);
    
    for (i=0; i<E; i++){
        if(graph->edge[i].tag==1)
            found++;
    }
    if (found <= K) printf("infinity\n");
    else{
        K++;
        for (i=E-1; i>=0; i--){
           if(graph->edge[i].tag==1){
               K--;
               
           }
           if (K==0){
                         printf("%lld\n", graph->edge[i].weight);
                         break;
           }
        }
    }
    return 0;
}