hdu 2121 Ice_cream’s world II

Ice_cream’s world II

http://acm.hdu.edu.cn/showproblem.php?pid=2121

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

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
 
 
威士忌   |   We have carefully selected several similar problems for you:  2120 2122 1222 4009 2064 
 
 
没有根的最小树形图
新建虚拟节点,向所有点连权值为 边权和+1的边
最后再减去这条边
 
记录根节点时,记录虚拟边的编号,不要记录虚拟边指向的点,因为缩环之后点的编号有所改变
 
#include<cstdio>
#include<cstring>
#define N 1005
#define M 10005
#define inf 2e9
using namespace std;
struct node
{
    int u,v,w;
}e[M+N];
int in[N],pre[N],vis[N],col[N],id[N];
int ROOT;
int n,m;
int directed_MST()
{
    int tot=n+1,root=0,ans=0,cirnum=0,to;
    while(1)
    {
        for(int i=0;i<tot;i++) in[i]=inf; 
        for(int i=1;i<=m;i++)
            if(e[i].u!=e[i].v && in[e[i].v]>e[i].w) 
            {
                in[e[i].v]=e[i].w;
                pre[e[i].v]=e[i].u;
                if(e[i].u==root) ROOT=i;
            }
        cirnum=0;
        memset(vis,-1,sizeof(vis));
        memset(col,-1,sizeof(col));
        in[root]=0;
        for(int i=0;i<tot;i++)
        {
            ans+=in[i];
            to=i;
            while(vis[to]!=i && col[to]==-1 && to!=root)
            {
                vis[to]=i;
                to=pre[to];
            }
            if(to!=root && col[to]==-1)
            {
                for(int nt=pre[to];nt!=to;nt=pre[nt])
                    col[nt]=cirnum;
                col[to]=cirnum++;
            }
        }
        if(!cirnum) return ans;
        for(int i=0;i<tot;i++) 
            if(col[i]==-1) col[i]=cirnum++;
        for(int i=1;i<=m;i++)
        {
            to=e[i].v;
            e[i].u=col[e[i].u];
            e[i].v=col[e[i].v];
            if(e[i].u!=e[i].v) e[i].w-=in[to];
        }
        tot=cirnum;
        root=col[root];
    }
    return ans;
}
int main()
{
    int tot,sum;
    int u,v,w,ans,tmp;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        tot=sum=0;
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            if(u!=v)
            {
                u++; v++;
                e[++tot].u=u; e[tot].v=v; e[tot].w=w;
                sum+=w;
            }
        }
        tmp=tot;
        for(int i=1;i<=n;i++) 
        {
            e[++tot].u=0; e[tot].v=i; e[tot].w=sum+1;
        }
        m=tot;
        ans=directed_MST();
        if(ans>=2*(sum+1)) printf("impossible

");
        else printf("%d %d

",ans-(sum+1),ROOT-tmp-1);
    }
}
原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/7435508.html