码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-07 22:52:52      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:


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

建立一个虚拟的根节点,向所有节点连边,权值为其他所有边的权值和+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;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:

原文地址:http://blog.csdn.net/ck_boss/article/details/46793641

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!