hdu 2121 (不定根的最小树形图)

Ice_cream’s world II

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


 

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

题意: 这个国家n个城市,现在要选出一个城市作为首都,要求这个城市到其他城市的总花费最小(其实就是有向图的最小生成树) 。

思路: 我们可以用朱刘算法来跑最小树形图,但是这里有一个问题就是如何确立根节点呢? 我们可以设置一个超级源点,而其他所有城市到超级源点的距离肯定要大于等于 所有路径的和。

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll inf =1e18+5;
const int N =1e3+5;
const int M =2e4+5;

struct Edge
{
    int u,v;
    ll w;
}edge[M];

ll in[N];
int pre[N],id[N],vis[N];
int n,m;
int pos;// 记录实际根节点的位置

int zhuliu(int root,int V,int E)
{
    ll res=0;
    while(1)
    {
        for(int i=0;i<V;i++) in[i]=inf;

        for(int i=0;i<E;i++)
        {
            int u=edge[i].u; int 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(int i=0;i<V;i++)  // 判断是否存在树形图
        {
            if(i!=root&&in[i]==inf) return -1;
        }

        int cnt=0; // 环的个数
        memset(id,-1,sizeof(id));
        memset(vis,-1,sizeof(vis));
        in[root]=0;

        for(int i=0;i<V;i++)
        {
            res+=in[i];
            int v=i;
            while(vis[v]!=i&&id[v]==-1&&v!=root){  //对于环的处理
                vis[v]=i;
                v=pre[v];
            }

            if(v!=root&&id[v]==-1){
                for(int u=pre[v];u!=v;u=pre[u]) id[u]=cnt;
                id[v]=cnt++;
            }

        }

        if(cnt==0){
            break; // 没有环跳出
        }

        for(int i=0;i<V;i++)
        {
            if(id[i]==-1){
                id[i]=cnt++;
            }
        }

        for(int i=0;i<E;i++)
        {
            int u=edge[i].u;
            int v=edge[i].v;
            edge[i].u=id[u];
            edge[i].v=id[v];
            if(id[u]!=id[v]){  // 如果一个点位于环内 一个点位于环外
                //那么就要将环外的点到环内的点的费用减掉环内最小的费用,
                //也就相当于删除指向环内的点的边 而从环外u点指向环内
                edge[i].w-=in[v];
            }
        }

        V=cnt;
        root=id[root];
    }

    return res;
}

int main()
{
    while(cin>>n>>m)
    {
        ll sum=0;
        for(int i=0;i<m;i++){
            scanf("%d %d %lld",&edge[i].u,&edge[i].v,&edge[i].w);
            edge[i].u++; edge[i].v++;  sum+=edge[i].w;
        }

        sum++;
        for(int i=m;i<n+m;i++)
        {
            edge[i].u=0;
            edge[i].v=i-m+1;
            edge[i].w=sum;
        }

        int ans=zhuliu(0,n+1,n+m);
        if(ans-sum>=sum||ans==-1){
            printf("impossible\n");
        }
        else{
            printf("%lld %d\n",ans-sum,pos-m);
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yjt9299/article/details/81080072
今日推荐