您的位置:首页 > 其它

Ice_cream’s world II - HDU 2121 朱刘算法

2015-06-04 10:21 344 查看


Ice_cream’s world II

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

Total Submission(s): 3398 Accepted Submission(s): 812



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




题意:求花费最小的最小树形图。

思路:在朱刘算法的基础上,建立一个虚点,让虚点指向所有的点,其权值为sum+1,然后用这个虚点去做朱刘算法的根节点。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
struct node
{
    int u,v;
    ll w;
}edge[20010];
int n,m,pre[1010],id[1010],vis[1010],INF=1e18,pos;
ll in[1010];
ll Directed_MST(int root,int V,int E)
{
    ll ret=0;
    int i,j,k,u,v,cnt;
    while(true)
    {
        for(i=0;i<V;i++)
           in[i]=INF;
        for(i=0;i<E;i++)
        {
            u=edge[i].u;
            v=edge[i].v;
            if(edge[i].w<in[v] && u!=v)
            {
                pre[v]=u;
                in[v]=edge[i].w;
                if(u==root)
                  pos=i;
            }
        }
        for(i=0;i<V;i++)
           if(i!=root &&in[i]==INF)
             return -1;
        cnt=0;
        memset(id,-1,sizeof(id));
        memset(vis,-1,sizeof(vis));
        in[root]=0;
        for(i=0;i<V;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]=cnt;
                id[v]=cnt++;
            }
        }
        if(cnt==0)
          break;
        for(i=0;i<V;i++)
           if(id[i]==-1)
             id[i]=cnt++;
        for(i=0;i<E;i++)
        {
            u=edge[i].u;
            v=edge[i].v;
            edge[i].u=id[u];
            edge[i].v=id[v];
            if(id[u]!=id[v])
              edge[i].w-=in[v];
        }
        V=cnt;
        root=id[root];
    }
    return ret;
}
int main()
{
    int i,j,k,u,v;
    ll ans,sum;
    while(~scanf("%d%d",&n,&m))
    {
        sum=1;
        for(i=0;i<m;i++)
        {
            scanf("%d%d%I64d",&edge[i].u,&edge[i].v,&edge[i].w);
            edge[i].u++;edge[i].v++;
            sum+=edge[i].w;
        }
        for(i=1;i<=n;i++)
        {
            edge[m+i-1].u=0;edge[m+i-1].v=i;edge[m+i-1].w=sum;
        }
        ans=Directed_MST(0,n+1,n+m);
        if(ans==-1 || ans>=2*sum)
          printf("impossible\n\n");
        else
          printf("%I64d %d\n\n",ans-sum,pos-m);
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: