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 ?
  • 相关阅读:
    集合
    第五天
    第四天
    第二天
    ieee 期刊缩写(转载)
    JAVA学习笔记03
    JAVA学习笔记2
    JAVA学习笔记01
    Latex 图片排版
    非奇异终端滑模
  • 原文地址:https://www.cnblogs.com/hlmark/p/4292829.html
Copyright © 2011-2022 走看看