zoukankan      html  css  js  c++  java
  • UVA 10462 —— Is There A Second Way Left?——————【最小生成树、kruskal、重边】

    Nasa, being the most talented programmer of his time, can’t think things to be so simple. Recently all his neighbors have decided to connect themselves over a network (actually all of them want to share a broadband internet connection :-)). But he wants to minimize the total cost of cable required as he is a bit fastidious about the expenditure of the project. For some unknown reasons, he also wants a second way left. I mean, he wants to know the second best cost (if there is any which may be same as the best cost) for the project. I am sure, he is capable of solving the problem. But he is very busy with his private affairs(?) and he will remain so. So, it is your turn to prove yourself a good programmer. Take the challenge (if you are brave enough)...

    Input:

    Input starts with an integer t ≤ 1000 which denotes the number of test cases to handle. Then follows t datasets where every dataset starts with a pair of integers v (1 ≤ v ≤ 100) and e (0 ≤ e ≤ 200). v denotes the number of neighbors and e denotes the number of allowed direct connections among them. The following e lines contain the description of the allowed direct connections where each line is of the form ‘start end cost’, where start and end are the two ends of the connection and cost is the cost for the connection. All connections are bi-directional and there may be multiple connections between two ends.

    Output:

    There may be three cases in the output

    1. No way to complete the task,

    2. There is only one way to complete the task,

    3. There are more than one way.

       Output ‘No way’ for the first case, ‘No second way’ for the second case and an integer c for the third case where c is the second best cost. Output for a case should start in a new line.

    Sample Input:

    4

    5 4

    1 2 5

    3 2 5

    4 2 5

    5 4 5

    5 3

    1 2 5

    3 2 5

    5 4 5

    5 5

    1 2 5

    3 2 5

    4 2 5

    5 4 5

    4 5 6

    1 0

    Sample Output:


    Case #1 : No second way

    Case #2 : No way

    Case #3 : 21

    Case #4 : No second way

    题目大意:给你n个顶点,m条边。如果图是不连通的,输出No way,如果没有次小生成树,输出No second way,如果有次小生成树,输出次小生成树的值。有重边。

    解题思路:对于有重边的情况,我们可以用kruskal做,枚举删除最小生成树上的边,进行n-1次枚举,更新出次小生成树。

    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<queue>
    #include<vector>
    using namespace std;
    const int maxn = 110;
    const int INF = 0x3f3f3f3f;
    struct Edge{
        int from,to,dist;
    }edges[maxn*maxn];
    struct Set{
        int pa,rela;
    }sets[maxn];
    int store[maxn];
    bool cmp(Edge a,Edge b){
        return a.dist < b.dist;
    }
    void init(int n){
        for(int i = 0; i <= n; i++){
            sets[i].pa = i;
        }
    }
    int Find(int x){
        if(x == sets[x].pa){
            return x;
        }
        int tmp = sets[x].pa;
        sets[x].pa = Find(tmp);
        return sets[x].pa;
    }
    int num =0;
    int Kruskal(int n,int m){
        init(n);
        int rootx,rooty,x,y;
        int retsum = 0;
        num = 0;
        for(int i = 0; i < m; i++){
            Edge & e = edges[i];
            x = e.from, y = e.to;
            rootx = Find(x);
            rooty = Find(y);
            if(rootx != rooty){
                store[num++] = i;
                retsum += edges[i].dist;
                sets[rooty].pa = rootx;
            }
        }
        if(num < n-1){
            return -1;
        }else{
            return retsum;
        }
    }
    int SecKruskal(int n,int m,int mark){
        init(n);
        int rootx,rooty,x,y;
        int retsum = 0, cnt = 0;
        for(int i = 0; i < m; i++){
            if(mark == i) continue;
            Edge & e = edges[i];
            x = e.from, y = e.to;
            rootx = Find(x);
            rooty = Find(y);
            if(rootx != rooty){
                cnt++;
                retsum += edges[i].dist;
                sets[rooty].pa = rootx;
            }
        }
        if(cnt < n-1){
            return INF;
        }else{
            return retsum;
        }
    }
    int main(){
        int T,n,m,cas = 0;
        scanf("%d",&T);
        while(T--){
            scanf("%d%d",&n,&m);
            int a,b,c;
            for(int i = 0; i < m; i++){
                scanf("%d%d%d",&a,&b,&c);
                a--,b--;
                edges[i].from = a;
                edges[i].to = b;
                edges[i].dist = c;
            }
            sort(edges,edges+m,cmp);
            int mst = Kruskal(n,m);
            printf("Case #%d : ",++cas);
            if(mst == -1){
                puts("No way");
                continue;
            }
            int ans = INF;
            for(int i = 0; i < num; i++){
                int tmp = SecKruskal(n,m,store[i]);
                ans = min(ans,tmp);
            }
            if(ans == INF){
                puts("No second way");
            }else{
                printf("%d
    ",ans);
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    Laravel update某一字段值为另一字段值
    Layui 批量
    CI 框架多表关联查询
    PHP数组函数
    PHP固定长度字符串
    PHP常用方法汇总
    CI 框架批量添加数据(如果数据库有就更新数据)
    使用nginx-http-concat优化网站响应
    mysql自动化安装
    SQL迁移到ORACLE实例
  • 原文地址:https://www.cnblogs.com/chengsheng/p/4924780.html
Copyright © 2011-2022 走看看