zoukankan      html  css  js  c++  java
  • HDU 2121——Ice_cream’s world II——————【最小树形图、不定根】

    Ice_cream’s world II
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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
     
     
    题目大意:n个城市,m条有向边。问你是否存在一个城市,能通向其他所有城市且权值和最大。如果存在多个这样的城市,那么输出城市编号最小的那个。如果不存在,输出impossible。
     
    解题思路:不定根的最小树形图。我们考虑建立一个起点,这个起点跟所有其他的顶点连边,权值为城市中的所有边的权值和+1。然后就是用朱刘算法求解了。
     
     
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    #include<iostream>
    #include<vector>
    using namespace std;
    typedef long long INT;
    const int maxn = 1100;
    const int INF = 0x3f3f3f3f;
    struct Edge{
        int from,to;
        int dist;
    }edges[maxn*maxn];
    int pre[maxn],vis[maxn],ID[maxn];
    int In[maxn];
    int ansidx ;
    INT Zhuliu(int root,int n,int m){
        INT ret = 0;
        int u,v;
        while(true){
            for(int i = 0; i < n; i++){
                In[i] =  INF;
            }
            for(int i = 0; i < m; i++){
                Edge &e = edges[i];
                u = e.from; v = e.to;
                if(In[v] > e.dist && u != v){
                    pre[v] = u;
                    if(u == root){  //记录边的编号,这个编号-m就是点编号,因为我们加边是从顶点从小到大
                        ansidx = i;
                    }
                    In[v] = e.dist;
                }
            }
            for(int i = 0; i < n; i++){
                if(i == root) continue;
                if(In[i] == INF)
                    return -1;
            }
            In[root] = 0;
            int cntcir = 0;
            memset(vis,-1,sizeof(vis));
            memset(ID,-1,sizeof(ID));
            for(int i = 0; i < n; i++){
                ret += 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(u = pre[v]; u != v; u = pre[u]){
                        ID[u] = cntcir;
                    }
                    ID[v] = cntcir++;
                }
            }
            if(cntcir == 0){
                break;
            }
            for(int i = 0; i < n; i++){
                if(ID[i]==-1){
                    ID[i] = cntcir++;
                }
            }
            for(int i = 0; i < m; i++){
                v = edges[i].to;
                Edge & e = edges[i];
                e.from = ID[e.from];
                e.to = ID[e.to];
                if(e.from != e.to){
                    e.dist -= In[v];
                }
            }
            n = cntcir;
            root = ID[root];
        }
        return ret;
    }
    int main(){
        int n,m, T, cas = 0;
        while(scanf("%d%d",&n,&m)!=EOF){
            int a,b,c;
            INT sumd = 0;
            for(int i = 0; i < m; i++){
                scanf("%d%d%d",&a,&b,&c);
                a++,b++;
                edges[i].from = a;
                edges[i].to = b;
                if(a == b){
                    edges[i].dist = INF;
                    continue;
                }
                edges[i].dist = c;
                sumd += c;
            }
            for(int i = 0; i < n;i++){
                edges[m+i].from = 0;
                edges[m+i].to = i + 1;
                edges[m+i].dist = sumd + 1;
            }
            INT res = Zhuliu(0,n+1,m+n);
            if(res == -1||res > 2*sumd+1){
                puts("impossible");
            }else{
                printf("%lld %d
    ",res - sumd -1,ansidx - m);
            }puts("");
        }
        return 0;
    }
    
    /*
    3 2
    0 1 2
    1 2 3
    
    3 0
    
    */
    

      

     
  • 相关阅读:
    服务限流原理及算法
    Kafka 与 RabbitMQ 如何选择使用哪个?
    分布式事务之最终一致性实现方案
    如何在Ubuntu 14.04中使用systemctl?
    postgresql13 for window 安装及备份还原数据
    手把手教大家在mac上用VMWare虚拟机装Ubuntu
    在Mac平台上使用Multipass安装Ubuntu虚拟机
    如何在markdown中插入js和css
    HTML5标签
    linux
  • 原文地址:https://www.cnblogs.com/chengsheng/p/4933258.html
Copyright © 2011-2022 走看看