首页 > 其他 > 详细

HDU2121 Ice_cream’s world II 【最小树形图】+【不定根】

时间:2014-08-08 21:20:26      阅读:887      评论:0      收藏:0      [点我收藏+]

Ice_cream’s world II

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


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
题意:给定n个点m条边的起点、终点及权值,不给定根节点,求最小树形图。

题解:求不定根的最小树形图实际上可以虚拟出一个根节点,它到原图的任意一点都有一条单向边,权值为原图的总权值+1==maxval,然后求以此虚拟节点为根的最小树形图,若最后求得的权值>=2*maxval说明至少有两个实际点没有入边,即最小树形图不存在。至于求编号最小的实际根节点,由于实际根节点必定跟虚拟节点相连,只需找到第一个跟虚拟点相连的实际点即可。倘若原图形成了一个环,如:

3 3

0 1 1

1 2 1

2 0 1

此时,由于环在进行缩点后会重新寻找最短入边集,此时前三条边形成自环,ans = 3;第4条边使得ansNum被标记为3,in[0] = 3,由于无环,ans += 3, ans == 6,算法结束,最终结果ans= 6 - 4 = 2, ansNum = 3 - 3 == 0。

#include <stdio.h>
#include <limits.h>
#include <string.h>
#define maxn 1002
#define maxm 12000

struct Node{
	int u, v, cost;
} E[maxm];
int pre[maxn], vis[maxn], in[maxn], hash[maxn], ansNum;
int ans;

bool ZL_MST(int root, int nv, int ne)
{
	ans = 0;
	int i, u, v, cntnum, tmp;
	while(true){
		for(i = 0; i < nv; ++i) in[i] = INT_MAX;
		for(i = tmp = 0; i < ne; ++i){
			u = E[i].u; v = E[i].v;
			if(E[i].cost < in[v] && v != u){
				in[v] = E[i].cost; pre[v] = u;
				if(u == root) ansNum = i;
			}
		}
		in[root] = 0;
		for(i = 0; i < nv; ++i)
			if(in[i] == INT_MAX) return false;

		cntnum = 0;
		memset(vis, -1, sizeof(vis));
		memset(hash, -1, sizeof(hash));
		for(i = 0; i < nv; ++i){
			ans += in[i]; v = i;
			while(vis[v] != i && v != root && hash[v] == -1){
				vis[v] = i; v = pre[v];
			}
			if(v != root && hash[v] == -1){
				for(u = pre[v]; u != v; u = pre[u])
					hash[u] = cntnum;
				hash[v] = cntnum++;
			}
		}
		if(cntnum == 0) return true;
		
		for(i = 0; i < nv; ++i)
			if(hash[i] == -1) hash[i] = cntnum++;

		for(i = 0; i < ne; ++i){
			v = E[i].v;
			E[i].u = hash[E[i].u];
			E[i].v = hash[E[i].v];
			if(E[i].u != E[i].v) E[i].cost -= in[v];
		}

		nv = cntnum; root = hash[root];
	}
	return true;
}

int main()
{
	int n, m, a, b, c, i, maxVal;
	while(scanf("%d%d", &n, &m) == 2){
		for(i = maxVal = 0; i < m; ++i){
			scanf("%d%d%d", &a, &b, &c);
			E[i].u = a; E[i].v = b; 
			E[i].cost = c; maxVal += c;
		}
		//添加虚拟节点以及n条虚拟边
		for(++maxVal, i = 0; i < n; ++i){
			E[m+i].u = n; E[m+i].v = i;
			E[m+i].cost = maxVal;
		}
		if(!ZL_MST(n, n + 1, n + m) || ans >= 2 * maxVal) 
			printf("impossible\n\n");
		else printf("%d %d\n\n", ans - maxVal, ansNum - m);
	}
	return 0;
}


HDU2121 Ice_cream’s world II 【最小树形图】+【不定根】,布布扣,bubuko.com

HDU2121 Ice_cream’s world II 【最小树形图】+【不定根】

原文:http://blog.csdn.net/chang_mu/article/details/38444883

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!