zoukankan      html  css  js  c++  java
  • HDU 2121 Ice_cream’s world II(无定根最小树形图)

    Ice_cream’s world II

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


    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
     
    设一个虚根 , 给它到所有点弄一条边,权值足够大 。
    不进行删边操作,记录边作为ans2,然后就是 ans2 - m 就是最后要输出的答案了 。
     
     
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <vector>
    
    using namespace std ;
    typedef long long LL ;
    const double inf = 1e9+7;
    const int N = 1010;
    const int M = 40010;
    
    struct edge {
        int u , v ;
        LL w;
    }e[M];
    int n , m , ans2 ;
    int pre[N] , id[N] , vis[N] ;
    LL in[N] ;
    LL zhuliu( int root , int n , int m ) {
        LL res = 0 ; int u , v ;
        while(1) {
            for( int i = 0 ; i < n ; ++i ) in[i] = inf ;
    
            for( int i = 0 ; i < m ; ++i )
                if( e[i].u != e[i].v && e[i].w < in[e[i].v] ) {
                    pre[e[i].v] = e[i].u;
                    if( e[i].u == root ) ans2 = i ;
                    in[e[i].v] = e[i].w;
                }
            for( int i = 0 ; i < n ; ++i )
                if( i != root && in[i] == inf )
                    return -1 ;
            int tn = 0 ;
            memset( id , -1 , sizeof id );
            memset( vis , -1 , sizeof vis );
            in[root] = 0 ;
            for( int i = 0 ; i < n ; ++i ) {
                res += in[i];
                v = i ;
                while( vis[v] != i && id[v] == -1 && v != root ) {
                    vis[v] = i ;
                    v = pre[v] ;
                }
                if( v != root && id[v] == -1 ) {
                    for( int u = pre[v] ; u != v ; u = pre[u] )
                        id[u] = tn ;
                    id[v] = tn++ ;
                }
            }
            if( tn == 0 ) break ; // no circle
            for( int i = 0 ; i < n ; ++i ) if( id[i] == -1 ) {
                id[i] = tn++ ;
            }
            for( int i = 0 ; i < m ; ++i ){
                v = e[i].v;
                e[i].u = id[e[i].u];
                e[i].v = id[e[i].v];
                if( e[i].u != e[i].v )
                    e[i].w -= in[v];
            }
            n = tn ;
            root = id[root];
        }
        return res ;
    }
    int main ()  {
    //    freopen("in.txt","r",stdin);
        int _ , cas = 1 ;
        while( ~scanf("%d%d",&n,&m) ) {
            LL sum = 1 ;
            for( int i = 0 ; i < m ; ++i ) {
                scanf("%d%d%I64d",&e[i].u,&e[i].v,&e[i].w);
                sum += e[i].w;
            }
            int L = m ;
            for( int i = 0 ; i < n ; ++i ) {
                e[L].u = n , e[L].v = i , e[L++].w = sum ;
            }
            LL ans = zhuliu( n , n + 1 , L );
            if( ans == -1 || ans >= 2 * sum )
                puts("impossible");
            else {
                ans -= sum ;
                printf("%I64d %d
    ",ans,ans2 - m);
            }
            puts("");
        }
    }
    View Code
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    layui flow loading占位图实现方法
    layui弹出层layer的area过大被遮挡
    layui 复选框checkbox 实现全选全选
    axios 设置headers token
    elementUI vue this.$confirm 和el-dialog 弹出框 移动
    vue + axios + formdata 上传文件带参数的爬坑之路
    Java四舍五入时保留指定小数位数
    List containsKey 和Map contains 判断集合中是否包含某个值
    BigDecimal 基本使用 比较大小和加减乘除
    springMVC返回json数据乱码问
  • 原文地址:https://www.cnblogs.com/hlmark/p/4292829.html
Copyright © 2011-2022 走看看