HDU 2121 Ice_cream’s world II(不定根最小树形图)

本文探讨了一种特殊的图论问题——在不确定根的情况下寻找最小成本的树形图,并确定其根节点。通过引入虚拟根节点并调整边权值,解决了这一难题。文中提供了一个具体的算法实现,包括关键步骤和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ice_cream’s world II

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


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
 

Source
 

题意:
就是不确定根求最小树形图,输出根。

POINT:
原图中所有权值和和x,建一个虚根连上所有点,长度为sum=x+1.

注意sum不能不加一,我们可以假设存在一种情况有两个点,0条边,那么此时会有两条从超级源点出发的长度为0的边,那么此时无法判断是不是满足条件(0>2*0不满足,会认为是满足条件的,实际上0条边是不满足条件的)

然后ans>=sum*2即无解。然后不用删边,用来记录真根。


#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <string>
typedef long long LL;
using namespace std;
const LL maxn = 1111;
const LL maxm = 11111;
const LL inf = 999999999999999;

struct edge
{
    LL u,v,w;
}len[maxm];
LL in[maxn],pre[maxn],vis[maxn],id[maxn];
LL pos;
LL zhuliu(LL root,LL n,LL m)//[0-n]点 虚根为0
{
    LL res=0,u,v;
    while(1)
    {
        LL cn=0;
        for(LL i=0;i<=n;i++) in[i]=inf;
        for(LL i=1;i<=m;i++)
        {
            u=len[i].u,v=len[i].v;
            if(u!=v&&in[v]>len[i].w)
            {
                in[v]=len[i].w;
                pre[v]=u;
                if(u==root)
                    pos=i;
            }
        }
        in[root]=0;
        for(LL i=0;i<=n;i++) if(in[i]==inf) return inf;
        memset(id,-1,sizeof id);
        memset(vis,-1,sizeof vis);
        for(LL 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(id[v]==-1&&v!=root)
            {
                for(u=pre[v];u!=v;u=pre[u])
                {
                    id[u]=cn;
                }
                id[u]=cn;
                cn++;
            }
        }
        if(cn==0) break;
        for(LL i=0;i<=n;i++) if(id[i]==-1) id[i]=cn++;
        for(LL i=1;i<=m;i++)
        {
            v=len[i].v;
            len[i].u=id[len[i].u];
            len[i].v=id[len[i].v];
            if(len[i].u!=len[i].v)
            {
                len[i].w-=in[v];
            }
        }
        n=cn-1;
        root=id[root];
    }
    return res;
}
int main()
{
    LL n,m;
    while(~scanf("%lld %lld",&n,&m))
    {
        LL sum=1;
        for(LL i=1;i<=m;i++)
        {
            LL u,v,w;scanf("%lld %lld %lld",&u,&v,&w);
            u++,v++;
            len[i].u=u,len[i].v=v,len[i].w=w;
            sum+=w;
        }
        for(LL i=1;i<=n;i++)
        {
            len[i+m].u=0,len[i+m].v=i,len[i+m].w=sum;
        }
        LL ans=zhuliu(0,n,n+m);
        if(ans>=2*sum)
        {
            printf("impossible\n");
        }
        else
        {
            printf("%lld %lld\n",ans-sum,pos-m-1);
        }
        printf("\n");
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值