hdu 2121 最小树形图 朱刘算法

Ice_cream’s world II

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


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
 

 

Recommend
威士忌
 给定有向图,求最小树形图和根,用LRJ的模板WA了N次。下面AC代码照敲自:http://www.cnblogs.com/nanke/archive/2012/04/11/2441725.html
自己据理解加了注释:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#include<string>
#include<cmath>
#include<fstream>
#include<iomanip>
#include<climits>
#include<cfloat>

using namespace std;

#define MAX_INT 0x7fffffff
#define MAX_LL 0x7fffffffffffffff
#define ULL unsigned long long
#define LL long long
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define MIN(x,y) ((x) < (y) ? (x) : (y))

#define MAXN 1111
#define MAXM 11111
#define INF MAX_INT
int pre[MAXN],id[MAXN],visit[MAXN];
int inv[MAXN];
struct edge{
    int u,v,d;
}e[MAXM];
int roote;

int mdst(int s, int n, int m){
    int u,v,i,d;
    int ret=0;
    while(true){            //找出每个点的最小入弧
        for(i=0; i<n; i++)
            inv[i]=INF;
        for(i=0; i<m; i++){
            u=e[i].u;
            v=e[i].v;
            d=e[i].d;
            if(d<inv[v] && u!=v){
                inv[v]=d;   pre[v]=u;
                if(u==s) roote=i;        //要求的根
            }
        }                               //当前点集合中有点不可达,则图不连通
        for(i=0; i<n; i++) if(inv[i]==INF && i!=s)
            return -1;
        inv[s]=0;
        int cnt=0;
        memset(id, -1, sizeof(id));
        memset(visit, -1, sizeof(visit));
        for(i=0; i<n; i++){                 //缩圈
            ret+=inv[i];
            v=i;                            //如果v还没标记且非虚根
            while(visit[v]!=i && id[v]==-1 && v!=s){
                visit[v]=i;     v=pre[v];
            }                               //这里表明上步visit[v]=i,即有圈
            if(v!=s && id[v]==-1){
                for(u=pre[v]; u!=v; u=pre[u])
                    id[u]=cnt;
                id[v]=cnt++;
            }
        }
        if(!cnt) break;                     //如果没圈
        for(i=0; i<n; i++) if(id[i]==-1)
            id[i]=cnt++;
        for(i=0; i<m; i++){                 //更新缩圈后各边
            u=e[i].u;   v=e[i].v;   d=e[i].d;
            e[i]=(edge){id[u], id[v], (u==v ? d : d-inv[v])};
        }
        n=cnt;
        s=id[s];
    }
    return ret;
}

int main(){
    //freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
    int n,m;
    while(scanf(" %d %d",&n,&m)==2){
        int i,u_,v_,w_,s=1;
        const int m1=m;
        for(i=0; i<m; i++){
            scanf(" %d %d %d",&u_,&v_,&w_);
            e[i]=(edge){u_,v_,w_};
            s+=w_;
        }
        for(i=0; i<n; i++)
            e[m++]=(edge){n,i,s};
        int ans=mdst(n, 1+n, m);
        if(ans==-1 || ans-s>=s) printf("impossible\n\n");
        else printf("%d %d\n\n",ans-s, roote-m1);
    }
    return 0;
}

 为什么最后每条非自环都要减去最小入弧权值inv[v]? ==因为缩圈时ret已经加上了inv[v],

对所有e(u, v), e.d = e.d - inv[v]可保证下一次迭代为点v选出正确的最小入弧(inv[v]).

转载于:https://www.cnblogs.com/ramanujan/p/3274057.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值