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): 5200    Accepted Submission(s): 1293


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

题意:给你n个城市的编号  和m条边  求最小树形图,如果有多个,那就输出根节点最小的那个。

由于刚开始并不能确定在哪里建造首都,这个时候我们就知道是无定根的了,这种情况我们就需要加一个超级节点(节点0)了,这个节点是原图中节点以外的点,并且这个超级节点到各个点的权值要比原图中权值和大,比如sum是原图中所有权值的和,那么我们就另超级节点到其他点的权值为sum + +,这样我们最后求得的结果为ans = sum + sum - 1;如果ans - sum >= sum 那么此时就是说从超级节点0出发的边不止一条,也就是说原图中有多个孤立点,这样的话是不能不构成最小树形图的。
这道题还要输出根节点(这点是真的恶心。。。。), 我们在找最小入边的时候把有一个点的最小入边是由超级节点到的,那么我们就把这个边记录下来,而这个点就是原图中的起点,由于我们是通过边来找的起点的入边,那么最后我们再减去刚开始边的个数就是答案。

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAX 1005
using namespace std;
int n, m;
struct Edge
{
    int u, v, w;
}edge[MAX * MAX];
int in[MAX], st;
int pre[MAX], id[MAX], vis[MAX];
int ZL(int root, int nodenum, int edgenum)
{
    int ret = 0;

    while(true)
    {
        ///找最小入边
        for(int i = 0; i < nodenum; i++) ///every node is INF
            in[i] = INF;
        for(int i = 0; i < edgenum; i++) ///遍历每条边
        {
            int u = edge[i].u;
            int v = edge[i].v;
            if(edge[i].w < in[v]&&u != v)
            {
                pre[v] = u;
                in[v] = edge[i].w;
                if(root == u)  ///如果一个点是由超级节点到达的 那这个点就是原图中的起点
                        st = i;
            }
        }

        ///由于超级节点到各点的权值为sum 是小于INF, 那么图中所有点的入度都不可能为INF 所以下面这个for循环可要可不要
        for(int i = 0; i < nodenum; i++) ///判断是否有最小树形图
        {
            if(i == root)
                continue;
            if(in[i]==INF)
                return -1;
        }
        ///找环
        int cnt = 0;
        memset(id, -1, sizeof(id));
        memset(vis, -1, sizeof(vis));
        in[root] = 0;
        for(int i = 0; i < nodenum; i++)
        {
            ret += in[i];
            int v = i;
            while(vis[v] != i&&id[v] == -1&&v != root) ///每个点寻找其前序点,要么最终寻找至根部,要么找到一个环  
            {
                vis[v] = i;
                v = pre[v];
            }
            if(v != root&&id[v] == -1) ///缩点  把同处于一个环中的各个点都标为人工节点cnt 
            {
                for(int u = pre[v]; u != v; u = pre[u])
                    id[u] = cnt;
                id[v] = cnt++;
            }
        }
        if(cnt==0) break; ///无环  则break
        ///建新图
        for(int i = 0; i < nodenum; i++)
        {
            if(id[i]==-1)
                id[i] = cnt++;
        }
        for(int i = 0; i < edgenum; i++)
        {
            int u = edge[i].u;
            int v = edge[i].v;
            edge[i].u = id[u];
            edge[i].v = id[v];
            if(id[u] != id[v])
                edge[i].w -= in[v];
        }
        nodenum = cnt;
        root = id[root];
    }
    return ret;
}
int main()
{
    while(~scanf("%d%d",&n, &m))
    {
        int sum = 0;
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d",&edge[i].u, &edge[i].v, &edge[i].w);
            edge[i].u++;
            edge[i].v++;
            sum+=edge[i].w;
        }
        sum++;
        ///在加入超级节点后  变为n + 1个节点  由于超级节点到每个点都有边,也就是n条边 那么总边数就变为m + n
        ///前m条边是超级节点到原图中各个点的权值 后面n - 1个边才是原图中的边
        for(int i = m; i < m + n; i++)
        {
            edge[i].u = 0;
            edge[i].v = i - m + 1;
            edge[i].w = sum;
        }
        int ans = ZL(0, n + 1, m + n);
        ///n+1为总结点数,m+n为总边数  
        ///ans代表以超级节点0为根的最小树形图的总权值,  
        ///将ans减去sum,如果差值小于sum,说明节点0的出度只有1,说明原图是连通图  
        ///如果差值>=sum,那么说明节点0的出度不止为1,说明原图不是连通图  
        if(ans == -1|| ans - sum >= sum)
            printf("impossible\n\n");
        else
            printf("%d %d\n\n",ans - sum, st - m);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值