hdu2121 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): 5120    Accepted Submission(s): 1271

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

题目大意:女王想在自己的国家选择建首都的城市,要求该城市能够到达国家的所有城市,并且距离最短,要是有多个合适地点,选择编号最小的城市,要是不存在这样的城市,输出impossible

AC代码:

# include <cstdio>
# include <cstring>
# include <math.h>

# define MAXN 105 * 1000
# define INF 10000000

typedef int type;

struct POINT
{
    int start;
    int end;
    type dist;
};

POINT pt[MAXN];
type in[MAXN];
int pre[MAXN];
int vis[MAXN];
int Node[MAXN];
int root;


type Directed_MST(int node, int nv, int ne)
{
    int i;

    type sum = 0;
    while (true)
    {
        //找结点最小入边
        for (i = 0; i < nv; i++)
        {
            in[i] = INF;
        }
        for (i = 0; i < ne; i++)
        {
            int start = pt[i].start;
            int end = pt[i].end;
            if (pt[i].dist < in[end] && start != end)
            {
                pre[end] = start; //记录前驱结点
                in[end] = pt[i].dist;
                if (start == node)
                {
                    root = i;
                }
            }
        }
        //判断是否能够构造成为最小生成树
        for (i = 0; i < nv; i++)
        {
            if (i == node)
            {
                continue;
            }
            if (in[i] == INF)
            {
                return -1;
            }
        }
        //找环
        int cntnode = 0;
        memset(vis, -1, sizeof(vis));
        memset(Node, -1, sizeof(Node));
        in[node] = 0;
        for (i = 0; i < nv; i++)
        {
            sum += in[i];
            int v = i;
            while (vis[v] != i && v != node)
            {
                vis[v] = i;
                v = pre[v];
            }
            if (v != node && -1 == Node[v])
            {
                for (int u = pre[v]; u != v; u = pre[u])
                {
                    Node[u] = cntnode;
                }
                Node[v] = cntnode++;
            }
        }

        if (cntnode == 0) //无环
        {
            break;
        }
        for (i = 0; i < nv; i++)
        {
            if (-1 == Node[i])
            {
                Node[i] = cntnode++;
            }
        }

        //收缩 构造新的树
        for (i = 0; i < ne; i++)
        {
            int v = pt[i].end;
            pt[i].start = Node[pt[i].start];
            pt[i].end = Node[pt[i].end];
            if (pt[i].start != pt[i].end)
            {
                pt[i].dist -= in[v];
            }
        }
        nv = cntnode;
        node = Node[node];
    }
    return sum;
}

int main(void)
{
    int n, m;
    int i;
    while (~scanf("%d %d", &n, &m))
    {
        type maxw = 0;
        for (i = 0; i < m; i++)
        {
            scanf("%d %d %d", &pt[i].start, &pt[i].end, &pt[i].dist);
            maxw += pt[i].dist;
        }
        maxw++;
        for (i = 0; i < n; i++) //添加一个虚拟点
        {
            pt[i + m].start = n;
            pt[i + m].end = i;
            pt[i + m].dist = maxw;
        }
        int result = Directed_MST(n, n + 1, m + n); //从虚拟点开始查找最小树形图
        if (-1 == result || result - maxw >= maxw)
        {
            printf("impossible\n\n");
        }
        else
        {
            printf("%d %d\n\n", result - maxw, root - m);
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值