您的位置:首页 > 其它

hdu 2121(没有固定根的最小树形图)

2011-09-05 19:48 211 查看

Ice_cream’s world II

[b]Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 659 Accepted Submission(s): 125

[/b]

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

HDU 2007-10 Programming Contest_WarmUp



Recommend

威士忌


题目:http://acm.hdu.edu.cn/showproblem.php?pid=2121
分析:这题没给出根,网上讲解很多转一下傻崽大神的题解
解法:

由于没有根,所以我们可以虚拟一个根,到每个点的权值很大很大(所有权值+1即可),权值很大可以保证最后只有一个点连这个虚拟根,然后最大答案减去这个很大的权值,而要输出最小的根的话则有点恶心- -由于我的做法会改变点的ID号,所以要保存原来的ID,如果和最初的跟节点连得话,那么更新一下最小的ID(也可不用保存,根据数据的储存顺序直接算出来)

这题边的范围没有给出,试了一下貌似很大,所以只好用快排了,不过这样做我的算法就不给力了,都垫底了。。。下次再给出另一种解法的代码吧
代码:
#include<cstdio>
using namespace std;
const int mm=11111;
const int mn=1111;
struct edge
{
    int s,t,w;
} g[mm];
int p[mn],q[mn],mark[mn],fp[mn],from[mn],vis[mn],in[mn],w[mn];
int i,j,k,n,m,e,r,me,ans,sum,anss;
bool huan;
inline void addedge(int u,int v,int c)
{
    g[e].s=u,g[e].t=v,g[e++].w=c;
}
void swap(int i,int j)
{
    edge c;
    c=g[i],g[i]=g[j],g[j]=c;
}
void mysort(int l,int r)
{
    int i=l,j=r,m=g[(l+r)>>1].t,w=g[(l+r)>>1].w;
    while(i<=j)
    {
        while(g[i].t<m||(g[i].t==m&&g[i].w<w))++i;
        while(g[j].t>m||(g[j].t==m&&g[j].w>w))--j;
        if(i<=j)swap(i++,j--);
    }
    if(i<r)mysort(i,r);
    if(l<j)mysort(l,j);
}
int main()
{
    while(scanf("%d%d",&n,&m)!=-1)
    {
        e=0,me=1;;
        while(m--)
        {
            scanf("%d%d%d",&i,&j,&k),++i,++j,++p[j];
            if(i!=j)addedge(i,j,k),me+=k;
        }
        for(i=1; i<=n; ++i)addedge(0,i,me);
        mysort(0,e-1);
        for(i=0; i<=n; ++i)fp[i]=p[i]=-1,in[i]=vis[i]=0,mark[i]=i;
        for(i=0; i<e; ++i)
            if(p[g[i].t]<0)p[g[i].t]=i;
        huan=1,ans=sum=0;
        while(huan)
        {
            huan=0;
            for(i=1; i<=n; ++i)
                if(fp[j=mark[i]]>=0)
                {
                    if(fp[i]<0)in[i]+=w[j],mark[i]=mark[mark[i]];
                    else
                    {
                        in[i]+=w[i],ans+=w[i];
                        if(g[++p[fp[i]]].t!=fp[i])p[fp[i]]=-1;
                    }
                }
            for(i=0; i<=n; ++i)fp[i]=-1,vis[i]=0;
            for(i=1; i<=n; ++i)
                if(p[i]>=0)
                {
                    if(fp[j=mark[i]]<0||(fp[j]>=0&&w[j]>g[p[i]].w-in[i]))
                        w[j]=g[p[i]].w-in[i],fp[j]=i,from[j]=mark[g[p[i]].s];
                }
            for(sum=0,i=1; i<=n; ++i)
                if(fp[i]>=0)
                {
                    sum+=w[i];
                    if(from[i]==0)anss=g[p[fp[i]]].t;
                }
            for(i=1; i<=n; ++i)
                if(!vis[i])
                {
                    r=0,j=i;
                    while(j>0&&vis[j]>=0)
                    {
                        if(vis[j]>0)
                        {
                            huan=1;
                            while(q[--r]!=j)mark[q[r]]=j,vis[q[r]]=-1;
                            vis[j]=-1;
                        }
                        else if(!vis[j])vis[q[r++]=j]=1;
                        if(fp[j]>=0)j=from[j];
                        else j=-1;
                    }
                    while(r--)vis[q[r]]=fp[q[r]]=-1;
                }
        }
        ans=ans+sum-me;
        if(ans>me)printf("impossible\n\n");
        else
        {
            printf("%d %d\n\n",ans,anss-1);
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: