zoukankan      html  css  js  c++  java
  • hdu 2121 最小树形图 朱刘算法

    Ice_cream’s world II

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2119    Accepted Submission(s): 502


    Problem Description
    After awarded lands to ACMers, the queen want to choose a city be her capital. This is an important event in ice_cream world, and it also a very difficult problem, because the world have N cities and M roads, every road was directed. Wiskey is a chief engineer in ice_cream world. The queen asked Wiskey must find a suitable location to establish the capital, beautify the roads which let capital can visit each city and the project’s cost as less as better. If Wiskey can’t fulfill the queen’s require, he will be punishing.
     
    Input
    Every case have two integers N and M (N<=1000, M<=10000), the cities numbered 0…N-1, following M lines, each line contain three integers S, T and C, meaning from S to T have a road will cost C.
     
    Output
    If no location satisfy the queen’s require, you must be output “impossible”, otherwise, print the minimum cost in this project and suitable city’s number. May be exist many suitable cities, choose the minimum number city. After every case print one blank.
     
    Sample Input
    3 1 0 1 1 4 4 0 1 10 0 2 10 1 3 20 2 3 30
     
    Sample Output
    impossible 40 0
     
    Author
    Wiskey
     
    Source
     
    Recommend
    威士忌
     给定有向图,求最小树形图和根,用LRJ的模板WA了N次。下面AC代码照敲自:http://www.cnblogs.com/nanke/archive/2012/04/11/2441725.html
    自己据理解加了注释:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<set>
    #include<queue>
    #include<string>
    #include<cmath>
    #include<fstream>
    #include<iomanip>
    #include<climits>
    #include<cfloat>
    
    using namespace std;
    
    #define MAX_INT 0x7fffffff
    #define MAX_LL 0x7fffffffffffffff
    #define ULL unsigned long long
    #define LL long long
    #define MAX(x,y) ((x) > (y) ? (x) : (y))
    #define MIN(x,y) ((x) < (y) ? (x) : (y))
    
    #define MAXN 1111
    #define MAXM 11111
    #define INF MAX_INT
    int pre[MAXN],id[MAXN],visit[MAXN];
    int inv[MAXN];
    struct edge{
        int u,v,d;
    }e[MAXM];
    int roote;
    
    int mdst(int s, int n, int m){
        int u,v,i,d;
        int ret=0;
        while(true){            //找出每个点的最小入弧
            for(i=0; i<n; i++)
                inv[i]=INF;
            for(i=0; i<m; i++){
                u=e[i].u;
                v=e[i].v;
                d=e[i].d;
                if(d<inv[v] && u!=v){
                    inv[v]=d;   pre[v]=u;
                    if(u==s) roote=i;        //要求的根
                }
            }                               //当前点集合中有点不可达,则图不连通
            for(i=0; i<n; i++) if(inv[i]==INF && i!=s)
                return -1;
            inv[s]=0;
            int cnt=0;
            memset(id, -1, sizeof(id));
            memset(visit, -1, sizeof(visit));
            for(i=0; i<n; i++){                 //缩圈
                ret+=inv[i];
                v=i;                            //如果v还没标记且非虚根
                while(visit[v]!=i && id[v]==-1 && v!=s){
                    visit[v]=i;     v=pre[v];
                }                               //这里表明上步visit[v]=i,即有圈
                if(v!=s && id[v]==-1){
                    for(u=pre[v]; u!=v; u=pre[u])
                        id[u]=cnt;
                    id[v]=cnt++;
                }
            }
            if(!cnt) break;                     //如果没圈
            for(i=0; i<n; i++) if(id[i]==-1)
                id[i]=cnt++;
            for(i=0; i<m; i++){                 //更新缩圈后各边
                u=e[i].u;   v=e[i].v;   d=e[i].d;
                e[i]=(edge){id[u], id[v], (u==v ? d : d-inv[v])};
            }
            n=cnt;
            s=id[s];
        }
        return ret;
    }
    
    int main(){
        //freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int n,m;
        while(scanf(" %d %d",&n,&m)==2){
            int i,u_,v_,w_,s=1;
            const int m1=m;
            for(i=0; i<m; i++){
                scanf(" %d %d %d",&u_,&v_,&w_);
                e[i]=(edge){u_,v_,w_};
                s+=w_;
            }
            for(i=0; i<n; i++)
                e[m++]=(edge){n,i,s};
            int ans=mdst(n, 1+n, m);
            if(ans==-1 || ans-s>=s) printf("impossible
    
    ");
            else printf("%d %d
    
    ",ans-s, roote-m1);
        }
        return 0;
    }
    

     为什么最后每条非自环都要减去最小入弧权值inv[v]? ==因为缩圈时ret已经加上了inv[v],

    对所有e(u, v), e.d = e.d - inv[v]可保证下一次迭代为点v选出正确的最小入弧(inv[v]).

  • 相关阅读:
    springboot上传文件并检查图片大小与格式
    java判断传进来的是否是图片
    Spring boot设置文件上传大小限制
    Mybatis 中遍历map 参数中的 list 和 array 属性
    更优雅地关闭资源
    解决springboot druid 数据库批量更新错误问题
    EditPlus行首行尾批量添加字符 以及其它常用正则
    mybatis传入List实现批量更新的坑
    【libreoffice】libreoffice实现office转pdf、html、jpg等格式数据
    【tomcat】sessionId学习(未完待续)
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3274057.html
Copyright © 2011-2022 走看看