HDOJ 2121 Ice_cream’s world II 最小树形图无根树


朱刘算法 最小树形图无根树:

建立一个虚拟的根节点,向所有节点连边,权值为其他所有边的权值和+1

在求最小树形图的时候,记录和虚拟的根相连的是哪个节点


在这题中,边是从小往大加的所以直接记录的是相连的是第几号边....



Ice_cream’s world II

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


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
 


/* ***********************************************
Author        :CKboss
Created Time  :2015年07月07日 星期二 16时55分48秒
File Name     :HDOJ2121.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn=1100;

int n,m;

struct Edge
{
	int u,v,cost;
}edge[maxn*maxn];

int pre[maxn],id[maxn],vis[maxn];
int in[maxn];
int Root;

int zhuliu(int root,int n,int m,Edge edge[])
{
	int res=0;
	int v;
	while(true)
	{

		for(int i=0;i<n;i++) in[i]=INF;
		for(int i=0;i<m;i++)
		{
			if(edge[i].u!=edge[i].v&&edge[i].cost<in[edge[i].v])
			{
				pre[edge[i].v]=edge[i].u;
				in[edge[i].v]=edge[i].cost;
				if(edge[i].u==root) Root=i;
			}
		}
		for(int i=0;i<n;i++)
			if(i!=root&&in[i]==INF) return -1;
		int tn=0;
		memset(id,-1,sizeof(id));
		memset(vis,-1,sizeof(vis));
		in[root]=0;
		for(int i=0;i<n;i++)
		{
			res+=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(int u=pre[v];u!=v;u=pre[u]) id[u]=tn;
				id[v]=tn++;
			}
		}
		if(tn==0) break;
		for(int i=0;i<n;i++)
		{
			if(id[i]==-1) id[i]=tn++;
		}

		for(int i=0;i<m;i++)
		{
			v=edge[i].v;
			edge[i].u=id[edge[i].u];
			edge[i].v=id[edge[i].v];
			if(edge[i].u!=edge[i].v) edge[i].cost-=in[v];
		}

		n=tn;
		root=id[root];
	}
	return res;
}

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int sum=0;
		int en=0;
		Root=INF;

		for(int i=0;i<m;i++)
		{
			scanf("%d%d%d",&edge[en].u,&edge[en].v,&edge[en].cost);
			sum+=edge[en++].cost;
		}

		sum++;

		/// link with virtual root
		for(int i=0;i<n;i++)
		{
			edge[en].u=n; edge[en].v=i; edge[en].cost=sum; 
			en++;
		}

		int lens = zhuliu(n,n+1,en,edge);

		if(lens>=2*sum||lens==-1) puts("impossible");
		else printf("%d %d\n",lens-sum,Root-m);
		
		putchar(10);
	}
    
    return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值